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
345b6be30650cb560b1fc2ff3b9b09f2
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
/* stream Butter! eggyHide eggyVengeance I need U xiao rerun when */ import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class x1545A { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); int[] oddcnt = new int[100005]; int[] evencnt = new int[100005]; for(int i=0; i < N; i+=2) evencnt[arr[i]]++; for(int i=1; i < N; i+=2) oddcnt[arr[i]]++; sort(arr); String res = "YES"; for(int i=0; i < N; i+=2) { //process evens if(evencnt[arr[i]] == 0) res = "NO"; else evencnt[arr[i]]--; } for(int i=1; i < N; i+=2) { //process odds if(oddcnt[arr[i]] == 0) res = "NO"; else oddcnt[arr[i]]--; } sb.append(res+"\n"); } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } } /* 1 2 2 4 3 */
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
dc286a39c8dafbd38ebe69147eee8f5a
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class AquaMoonAndStrangeSort { public static void main(String args[]) { try { 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().split(" "); int[] odd = new int[n/2]; int[] even = new int[(int)Math.ceil((double)n/2)]; int[] arr = new int[n]; int o = 0; int e = 0; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(str[i]); if (i % 2 == 0) { even[e] = arr[i]; e++; } else { odd[o] = arr[i]; o++; } } Arrays.sort(odd); Arrays.sort(even); boolean possible = true; int prev = -1; for (int i = 0; i < Math.ceil((double)n/2); i++) { if (i < even.length) { if (even[i] >= prev) { prev = even[i]; } else { possible = false; break; } } if (i < odd.length) { if (odd[i] >= prev) { prev = odd[i]; } else { possible = false; break; } } } if (possible) { System.out.println("YES"); } else { System.out.println("NO"); } } } catch (Exception e) { return; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
631de58a9f7e0ba82ac911451744004b
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { //////////////////////////////////////////////////////////////////////////////////// // THE MERGE SORT IS TAKEN FROM GEEKSFORGEEKS// /////////////////////////////////////////////////////////////////////////////////// public static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() public static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m =l+ (r-l)/2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } public static Stack<Pair> s = new Stack<>(); public static int sum = 0; public static int taken = 0; public static int N = 0; public static int[][] arr; public static Pair fin; public static boolean flag = false; // public static HashSet<String> hs = new HashSet<>(); public static Object[] a = new ArrayList<Object>().toArray(); public static Pair cp = new Pair(0, 0); public static boolean is_safe(int[][] arr, int row, int cln) { int i; int j; for (i = 0; i < cln; i++) if (arr[row][i] == 1) return false; for (i = row, j = cln; j >= 0 && i < N; i++, j--) if (arr[i][j] == 1) return false; for (i = row, j = cln; i >= 0 && j >= 0; i--, j--) if (arr[i][j] == 1) return false; return true; } public static boolean solveNQ(int[][] arr, int cln) { if (cln == N) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) System.out.print(arr[i][j] + " "); System.out.println(); } return true; } for (int i = 0; i < N; i++) { if (is_safe(arr, i, cln)) { arr[i][cln] = 1; solveNQ(arr, cln + 1); arr[i][cln] = 0; } } return false; } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner (System.in); // FileReader file = new FileReader("C:\\Users\\dell\\OneDrive\\Desktop\\code.txt"); // BufferedReader sc = new BufferedReader(file); // String s ; // while ((s = sc.readLine()) != null) { // StringTokenizer st = new StringTokenizer(s); // int t; // t = Integer.parseInt(st.nextToken()); // System.out.println(t); int t = sc.nextInt(); while (t-- >0 ) { ; // st = new StringTokenizer(sc.readLine()); // int n = Integer.parseInt(st.nextToken()); // st = new StringTokenizer(sc.readLine()); // if (c == 21) {pw.println(str);continue;} int max = 0 ; int n = sc.nextInt(); int [] original = new int[n]; int [] unsortedeven = new int[100001]; int [] unsortedodd = new int[100001]; int [] sortedodd = new int[100001]; int [] sortedeven = new int[100001]; for (int i = 0 ; i < n ; i ++ ) { int x = sc.nextInt(); max = Math.max(max,x); original[i] = x; if (i%2 == 0 ) { unsortedeven[x] = unsortedeven[x] +1 ; }else unsortedodd[x] = unsortedodd[x]+1; } sort(original,0,original.length-1); for (int i = 0 ; i < n ;i ++ ) { int x = original[i]; if (i%2 == 0 ) { sortedeven[x] = sortedeven[x]+1 ; }else sortedodd[x] = sortedodd[x]+1; } boolean flag = true; for (int j = 0 ; j<n ; j ++ ) { int i = original[j]; if (unsortedeven[i] != sortedeven[i] || unsortedodd[i] != sortedodd[i] ) { flag = false ; break ; } } pw.println((flag)? "YES" : "NO"); } pw.close(); } public static PrintWriter pw = new PrintWriter(System.out); static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } public 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 Integer(x).hashCode() * 31 + new Integer(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); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
1d6a1f6eb1af842bf45f6f527b54b75e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { MyScanner scan = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); for (int i = scan.nextInt(); i > 0; i--) { int[] arr = new int[scan.nextInt()]; for (int k = 0; k < arr.length; k++) { arr[k] = scan.nextInt(); } int[] arrSorted = Arrays.copyOf(arr, arr.length); Arrays.sort(arrSorted); int[] position = new int[arrSorted.length]; for (int k = 0; k < arr.length; k++) { int pos = Arrays.binarySearch(arrSorted, arr[k]); if(k % 2 == 1)position[pos]++; else position[pos]--; } boolean possible = true; for (int k = 0; k < arrSorted.length; k++) { int counter = 1; long sum1 = 0; long sum2 = 0; while(k+counter < arrSorted.length && arrSorted[k+counter] == arrSorted[k])counter++; for (int l = k; l < k + counter; l++) { if(l % 2 == 1) sum1++; else sum1--; sum2 += position[l]; } if(sum1 != sum2)possible = false; k += counter-1; } if (possible) out.println("YES"); else out.println("NO"); } out.close(); } public static PrintWriter out; 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
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
d6c6b1954cc3e57d023e65bc66a3bec6
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /* Name of the class has to be "Main" only if the class is public. */ public class Coder { static int n; static int a[]; static StringBuilder str = new StringBuilder(""); static int cnt[][] = new int[(int)1e5+5][2]; static void solve() { for(int i=0;i<n;i++) cnt[a[i]][i%2]++; Arrays.sort(a); for(int i=0;i<n;i++) cnt[a[i]][i%2]--; for(int i=0;i<n;i++){ if(cnt[a[i]][0] !=0 || cnt[a[i]][1]!=0){ str.append("NO\n"); return; } } str.append("YES\n"); } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(bf.readLine().trim()); while (t-- > 0) { n = Integer.parseInt(bf.readLine().trim()); String s[] = bf.readLine().trim().split("\\s+"); a = new int[n]; for(int i=0;i<n;i++) a[i]=Integer.parseInt(s[i]); for(int i=0;i<(int)1e5+5;i++) { cnt[i][0]=0; cnt[i][1]=0;} solve(); } System.out.print(str); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
9005e8e0a138eaa7ef1ae5491f689604
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; public class cff { static int mod=1000000007; public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int arr[][]=new int[100005][2]; int b[]=new int[n+1]; for(int i=1;i<=n;i++) { int a=s.nextInt(); b[i]=a; arr[a][i%2]++; } Arrays.sort(b); for(int i=0;i<=n;i++) { arr[b[i]][i%2]--; } String ans="YES"; for(int i=1;i<100005;i++) { if(arr[i][0]!=0 || arr[i][1]!=0) { ans="NO"; } } System.out.println(ans); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
dbc39404a55c787dd859445428a06975
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class pcarp{ static int max_value; static long mod = (long)1e9+7; static long ans = (long)1e15; static int MAXN = (int)1e5; static int[] spf = new int[MAXN+1]; public static void main(String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[] arr = new int[n]; int[][] cnt = new int[100001][2]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); cnt[arr[i]][i%2]++; } int flag=0; Arrays.sort(arr); for(int i=0;i<n;i++){ cnt[arr[i]][i%2]--; } for(int i=0;i<n;i++){ if(cnt[arr[i]][0]!=0 || cnt[arr[i]][1]!=0){ flag=1; break; } } if(flag==0){ System.out.println("YES"); } else{ System.out.println("NO"); } } } static void countPair(int arr[], int n, int k) { // intialize the count long cnt = 0; // making every element of arr in // range 0 to k - 1 for (int i = 0; i < n; i++) { arr[i] = (arr[i]) % k; } // create an array hash[] int hash[] = new int[k]; // store to count of element of arr // in hash[] for (int i = 0; i < n; i++) { hash[arr[i]]++; } // count the pair whose absolute // difference is divisible by k for (int i = 0; i < k; i++) { cnt += ((long)hash[i] * (long)(hash[i] - 1)) / 2; } // print the value of count System.out.print(cnt +"\n"); } public static int bs(long[] arr, long val){ int lo =0; int hi = arr.length-1; while(hi<=lo){ int mid = (lo+hi)/2; if(arr[mid]==val){ return mid; } else if(arr[mid]>val){ hi=mid-1; } else{ lo=mid+1; } } return -1; } public static boolean is(int[] arr, int val, int i, HashMap<String,Boolean> map){ String s = Integer.toString(val)+"$"+Integer.toString(i); if(val==0){ return true; } if(val<0 || i>=arr.length){ return false; } if(map.containsKey(s)){ return map.get(s); } boolean x= is(arr,val-arr[i],i+1,map) || is(arr,val,i+1,map); map.put(s,x); return x; } public static void update1(int[] bits, int[] x){ for(int i1=0;i1<32;i1++){ bits[i1]+=x[i1]; } } public static void update2(int[] bits, int[] x){ for(int i1=0;i1<32;i1++){ bits[i1]-=x[i1]; } } public static int fval(int[] bits){ int val1=0; for(int i1=0;i1<32;i1++){ if(bits[i1]!=0) val1+=(int)Math.pow(2,31-i1); } return val1; } public static int[] bitfunction(int n){ int[] arr = new int[32]; int i = 31; while(n>0){ arr[i--]=n%2; n/=2; } return arr; } public static long f_2(int[] arr, int start, int end, long[][] dp){ if(start>=arr.length || end<0){ return 0; } if(start==end){ return 0; } if(end-start==1){ return (long)(arr[end]-arr[start]); } if(dp[start][end]!=-1){ return dp[start][end]; } return dp[start][end]=(long)(arr[end]-arr[start])+Math.min(f_2(arr,start+1,end,dp),f_2(arr,start,end-1,dp)); } public static long f_1(int[] arr, int idx, int n, int g, long[][] dp){ if(idx==n){ if(g==1){ return 1; } else{ return 0; } } if(dp[idx][g]!=-1){ return dp[idx][g]; } dp[idx][g]=f_1(arr,idx+1,n,g,dp)+f_1(arr,idx+1,n,(int)gcd(arr[idx],g),dp); return dp[idx][g]; } static int getItemCount(int x){ int count=0; while(x!=1){ int temp = spf[x]; count++; while(x%temp==0){ x=x/temp; } } return count; } static void sieve() { spf[1] = 1; for (int i=2; i<=MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i=4; i<=MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<=MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j=i*i; j<=MAXN; j+=i) // marking spf[j] if it is not // previously marked if (spf[j]==j) spf[j] = i; } } } public static void print(long x){ System.out.println(x); } public static void print(String x){ System.out.println(x); } public static boolean isPal(String s, int i, int j){ if(i>j){ return false; } if(i==j){ return true; } if(j==i+1 && s.charAt(j)==s.charAt(i)){ return true; } if(j==i+1 && s.charAt(j)!=s.charAt(i)){ return false; } return s.charAt(i)==s.charAt(j) && isPal(s,i+1,j-1); } public static long getGcdSum(long n){ long ans=0; long a =n; while(n>0){ long x = n%10; ans+=x; n=n/10; } return gcd(a,ans); } // public static int f_h(int[] arr, int h){ // } public static int log2(int N) { int result = (int)(Math.log(N) / Math.log(2)); return result; } public static void reverse(int[] arr, int i, int j){ int left = i; int right = j; int temp; while (left < right) { temp=arr[left]; arr[left]=arr[right]; arr[right]=temp; left+=1; right-=1; } } static int recusive(String str, int x, int y, int pos, char prev,HashMap<String,Integer> map) { String s = Integer.toString(pos)+"$"+prev; if(pos == str.length()) return 0; if(map.containsKey(s)){ return map.get(s); } if (str.charAt(pos) == 'C') { if(prev == 'C'){ int a =recusive(str, x, y, pos+1, 'C',map); map.put(s,a); return a; } else{ int a = y + recusive(str, x, y, pos+1, 'C',map); map.put(s,a); return a; } } else if (str.charAt(pos) == 'J'){ if(prev == 'C'){ int a = x + recusive(str, x, y, pos+1, 'J',map); map.put(s,a); return a; } else{ int a= recusive(str, x, y, pos+1, 'J',map); map.put(s,a); return a; } } int op1 = Integer.MAX_VALUE/2; int op2 = Integer.MAX_VALUE/2; if(prev == 'C') { op1 = recusive(str, x, y, pos+1, 'C',map); op2 = x + recusive(str, x, y, pos+1, 'J',map); }else { op1 = y + recusive(str, x, y, pos+1, 'C',map); op2 = recusive(str, x, y, pos+1, 'J',map); } int b =Math.min(op1, op2); map.put(s,b); return b; } public static int f_min(String s, int i, int x, int y){ if(i==s.length()-1){ return 0; } if(s.charAt(i)=='C' || s.charAt(i)=='J'){ if(i+1<s.length() && s.charAt(i)=='C' && s.charAt(i+1)=='J'){ return x+f_min(s,i+1,x,y); } if(i+1<s.length() && s.charAt(i)=='J' && s.charAt(i+1)=='C'){ return y+f_min(s,i+1,x,y); } else{ return f_min(s,i+1,x,y); } } else { return Math.min((i-1>=0 && s.charAt(i-1)=='J')?y:0+(s.charAt(i+1)=='J'?x:0)+f_min(s.substring(0,i)+"C"+s.substring(i+1,s.length()),i+1,x,y),(i-1>=0 && s.charAt(i-1)=='C')?x:0+(s.charAt(i+1)=='C'?y:0)+f_min(s.substring(0,i)+"J"+s.substring(i+1,s.length()),i+1,x,y)); } } public static int minf(int[] arr, int i, int j){ int minidx=-1; int min=Integer.MAX_VALUE; for(int p=i;p<=j;p++){ if(arr[p]<min){ min=arr[p]; minidx=p; } } return minidx; } public static int fstr(String s1, String s2, HashMap<String,Integer> map){ String s = s1+"$"+s2; if((s1.length()==0 && s2.length()!=0)||(s1.length()!=0 && s2.length()==0)){ return 1000; } if(s1.equals(s2)){ return 0; } if(map.containsKey(s)){ return map.get(s); } int p =1+Math.min(fstr(s1,s2.substring(1,s2.length()),map),Math.min(fstr(s1,s2.substring(0,s2.length()-1),map),(Math.min(fstr(s1.substring(1,s1.length()),s2,map),fstr(s1.substring(0,s1.length()-1),s2,map))))); map.put(s,p); return p; } public static int binarySearch(Point[] arr, int i){ int lo=0; int hi = arr.length-1; while(lo<=hi){ int mid = lo+(hi-lo)/2; if(arr[mid].end<arr[i].start){ if(arr[mid+1].end<arr[i].start){ lo=mid+1; } else{ return mid; } } else{ hi = mid-1; } } return -1; } public static boolean fbool(int[][] a, int[][] b,int r, int c){ for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ if(a[i][j]!=b[i][j]){ return false; } } } return true; } public static long fmin(long n, long a, long b, long c, long e, long h){ if(n<=0){ return 0; } if(n<=e && n<=h){ ans=Math.min(ans,n*c); } if(2*n<=e){ ans=Math.min(ans,n*a); } if(3*n<=h){ ans=Math.min(ans,n*b); } if((h-n)/2>=1 && (h-n)/2>=(n-e)){ if(b<c){ long k=Math.min(n-1,(h-n)/2); ans=Math.min(ans,(b-c)*k+n*c); } else{ long k=Math.max(1,n-e); ans=Math.min(ans,(b-c)*k+n*c); } } if((e-n)>=1 && (e-n)>=(n-h)){ if(a<c){ long k=Math.min(n-1,(e-n)); ans=Math.min(ans,(a-c)*k+n*c); } else{ long k=Math.max(1,n-h); ans=Math.min(ans,(a-c)*k+n*c); } } if(e/2>=1 && e/2>=(3*n-h+2)/3){ if(a<b){ long k=Math.min(n-1,e/2); ans=Math.min(ans,(a-b)*k+n*b); } else{ long k=Math.max(1,(3*n-h+2)/3); ans=Math.min(ans,(a-b)*k+n*b); } } if(e>=3 && h>=4 && n>=3){ ans=Math.min(ans,(a+b+c)+fmin(n-3,a,b,c,e-3,h-4)); } return ans; } public static ArrayList<Integer> digits(int n){ ArrayList<Integer> arr = new ArrayList<>(); while(n>0){ arr.add(n%10); n/=10; } return arr; } public static void precompute(){ for(int i=0;i<=1e5;i++){ } } public static boolean valid(String s){ Stack<Character> st = new Stack<>(); for(int i=0;i<s.length();i++){ if(st.isEmpty() || s.charAt(i)=='('){ st.push(s.charAt(i)); } else if(st.peek()=='(' && s.charAt(i)==')'){ st.pop(); } } if(st.isEmpty()){ return true; } return false; } public static int minswap(int[] A, int[] B, boolean swap, int pos, int n){ if(pos>=n){ return 0; } int prev_a = swap==true?B[pos-1]:A[pos-1]; int prev_b = swap==true?A[pos-1]:B[pos-1]; int ans=Integer.MAX_VALUE; if(prev_a<=A[pos] && prev_b<=B[pos]){ ans=Math.min(ans,minswap(A,B,false,pos+1,n)); } if(prev_a<=B[pos] && prev_b<=A[pos]){ ans=Math.min(ans,1+minswap(A,B,true,pos+1,n)); } return ans; } public static int lcs(int[] arr, int n, int pos, int last, HashMap<Integer,Integer> map){ return 0; } public static int bg(int n, int pos, char last){ return 0; } public static int maxheight(int[] arr, int n){ int[] right = new int[n]; int[] left= new int[n]; Stack<Integer> st = new Stack<>(); for(int i=0;i<n;i++){ while(!st.isEmpty() && arr[i]<arr[st.peek()]){ right[st.pop()]=i; } st.push(i); } while(!st.isEmpty()){ right[st.pop()]=n; } for(int i=n-1;i>=0;i--){ while(!st.isEmpty() && arr[i]<arr[st.peek()]){ left[st.pop()]=i; } st.push(i); } while(!st.isEmpty()){ left[st.pop()]=-1; } int max=0; for(int i=0;i<n;i++){ max=Math.max(max,arr[i]*(right[i]-left[i]-1)); } return max; } public static void show(int[] arr){ int n = arr.length; for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } System.out.println(); } public static String pd(String s){ int start=0; int maxLength=1; int low,high; for(int i=1;i<s.length();i++){ low=i-1; high=i; while(low>=0 && high<s.length() && s.charAt(low)==s.charAt(high)){ if(high-low+1>maxLength){ start=low; maxLength=high-low+1; } low--; high++; } low=i-1; high=i+1; while(low>=0 && high<s.length() && s.charAt(low)==s.charAt(high)){ if(high-low+1>maxLength){ start=low; maxLength=high-low+1; } low--; high++; } } return s.substring(start,start+maxLength); // return String[]{s.substring(start,start+maxLength),s.substring(0,start)+s.substring(start+maxLength,s.length())}; } static boolean isPrime1(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int nextPrime(int N) { if (N <= 1) return 2; int prime = N; boolean found = false; while (!found) { prime++; if (isPrime1(prime)) found = true; } return prime; } public static int robber_house(int[] arr, int pos, int n, int[] dp){ if(pos>=n){ return 0; } if(dp[pos]!=-1){ return dp[pos]; } return dp[pos]=Math.max(arr[pos]+robber_house(arr,pos+2,n,dp),robber_house(arr,pos+1,n,dp)); } // public static long[] djk(int start,HashMap<Integer,List<Point>> map, int size ){ // Queue<Point> q1 = new PriorityQueue<>(); // long[] minCost = new long[size+1]; // boolean[] isVisited = new boolean[size+1]; // Arrays.fill(minCost,Long.MAX_VALUE); // minCost[start]=0; // isVisited[start]=true; // if(!map.containsKey(start)){ // return minCost; // } // q1.addAll(map.get(start)); // while(!q1.isEmpty()){ // Point p = q1.poll(); // if(isVisited[p.getKey()]){ // continue; // } // isVisited[p.getKey()]=true; // minCost[p.getKey()]=p.getValue(); // for(Point q : map.get(p.getKey())){ // long nextVal=0; // if(isVisited[q.getKey()]){ // continue; // } // if(minCost[p.getKey()]%q.getK()==0){ // nextVal=minCost[p.getKey()]+q.getValue(); // } // else{ // nextVal=minCost[p.getKey()]+(q.getK()-minCost[p.getKey()]%q.getK())+q.getValue(); // } // if(minCost[q.getKey()]>nextVal){ // minCost[q.getKey()]=nextVal; // q1.add(new Point(q.getKey(),nextVal,q.getK())); // } // } // } // return minCost; // } public static void permutation(char[] arr, int j){ if(j==arr.length-1){ print(arr); System.out.println(); } for(int i=j;i<arr.length;i++){ char temp = arr[i]; arr[i]=arr[j]; arr[j]=temp; permutation(arr,j+1); char temp1 = arr[i]; arr[i]=arr[j]; arr[j]=temp1; } } public static void print(char[] arr){ for(char c : arr){ System.out.print(c); } } public static long inc(String s){ char[] arr = s.toCharArray(); Arrays.sort(arr); String ans=""; for(int i=0;i<s.length();i++){ ans+=arr[i]; } return Long.parseLong(ans); } public static long dec(String s){ char[] arr = s.toCharArray(); Arrays.sort(arr); String ans=""; for(int i=s.length()-1;i>=0;i--){ ans+=arr[i]; } return Long.parseLong(ans); } public static long max(String n){ long max=0; for(int i=0;i<n.length();i++){ if(n.charAt(i)-'0'>max){ max=n.charAt(i)-'0'; } } return max; } public static long base(String n, long b){ long ans=0; for(int i=0;i<n.length();i++){ ans+=power(b,(long)(n.length()-1-i))*(n.charAt(i)-'0'); } return ans; } public static long power(long a, long b){ long res=1; a=a%mod; if(a==0){ return 0; } while(b>0){ if((b&1)==1){ res=res*a%mod; } b=b>>1; a=a*a%mod; } return res; } public static long value(int[] arr){ Arrays.sort(arr); return arr[arr.length/2]-arr[(arr.length-1)/2]+1; } static void function(int l, int r, int[] arr, int[] depth, int d){ if(r<l){ return; } if(r==l){ depth[l]=d; return; } int m = l; for(int i=l+1;i<=r;i++){ if(arr[m]<arr[i]){ m=i; } } depth[m]=d; function(l,m-1,arr,depth,d+1); function(m+1,r,arr,depth,d+1); } static boolean perfectCube(long N) { long cube_root; cube_root = (long)Math.round(Math.cbrt(N)); // If cube of cube_root is equals to N, // then print Yes Else print No if (cube_root * cube_root * cube_root == N) { return true; } return false; } public static int value(int n){ if(n==0){ return 0; } int cnt=0; while(n>0){ cnt++; n/=2; } return cnt; } public static int value1(int n){ int cnt=0; while(n>1){ cnt++; n/=2; } return cnt; } public static String function(String s){ if(check(s)){ return s; } if(s.length()==2 && s.charAt(0)==s.charAt(1)){ return ""; } if(s.charAt(0)==s.charAt(1)){ return function(s.substring(2,s.length())); } else{ return function(s.charAt(0)+function(s.substring(1,s.length()))); } } static boolean isPowerOfTwo(int n){ if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPerfectSquare(double x) { if (x >= 0) { double sr = Math.sqrt(x); return ((sr * sr) == x); } return false; } public static boolean isPerfect(int n){ int a = (int)Math.sqrt(n); if(a*a==n){ return true; } return false; } public static boolean check(String s){ if(s.length()==1){ return true; } for(int i=1;i<s.length();i++){ if(s.charAt(i)==s.charAt(i-1)){ return false; } } return true; } public static boolean isPrime(int n){ boolean flag=true; while(n%2==0){ n=n/2; flag=false; } for(int i=3;i<=Math.sqrt(n);i+=2){ if(n%i==0){ flag=false; while(n%i==0){ n=n/i; } } } return flag; } public static void dfst(ArrayList<ArrayList<Integer>> graph,int src, int deg,boolean ef, boolean of, boolean[] vis, boolean[] flip, int[] init, int[] goal){ if(vis[src]){ return; } vis[src]=true; if((deg%2==0 && ef) || (deg%2==1 && of)){ init[src]=1-init[src]; } if(init[src]!=goal[src]){ flip[src]=true; if(deg%2==0){ ef=!ef; } else{ of=!of; } } for(int i=0;i<graph.get(src).size();i++){ if(!vis[graph.get(src).get(i)]){ dfst(graph,graph.get(src).get(i),deg+1,ef,of,vis,flip,init,goal); } } } public static void dfs(ArrayList<ArrayList<Integer>> graph, int src, int val, boolean[] vis){ vis[src]=true; int cur_val =0; for(int i=0;i<graph.get(src).size();i++){ if(!vis[graph.get(src).get(i)]){ cur_val=val+1; dfs(graph,graph.get(src).get(i),cur_val,vis); } if(max_value<cur_val){ max_value=cur_val; } cur_val=0; } } public static ArrayList<Integer> pf(int n){ ArrayList<Integer> arr = new ArrayList<>(); boolean flag=false; while(n%2==0){ flag=true; n/=2; } if(flag){ arr.add(2); } for(int i=3;i<=Math.sqrt(n);i++){ if(n%i==0){ arr.add(i); while(n%i==0){ n/=i; } } } if(n>1){ arr.add(n); } return arr; } static long gcd(long a, long b){ if(b==0){ return a; } else{ return gcd(b,a%b); } } static boolean function(int n, int i, int[] arr, int sum){ if(sum==0){ return true; } if(i==n && sum!=0){ return false; } if(sum<arr[i]){ return function(n,i+1,arr,sum); } else{ return function(n,i+1,arr,sum-arr[i]) || function(n,i+1,arr,sum); } } public static long fact( long n, long mod){ long res =1; for(int i=1;i<=n;i++){ res%=mod; i%=mod; res=(res*i)%mod; } return res; } public static long nCk(long n,long k, long mod){ return (fact(n,mod)%mod*modular(fact(k,mod),mod-2,mod)%mod*modular(fact(n-k,mod),mod-2,mod)%mod)%mod; } public static long modular(long n, long e, long mod){ long res = 1; n%=mod; if (n == 0) return 0; while(e>0){ if((e&1)==1){ res=(res*n)%mod; } e=e>>1; n=(n*n)%mod; } return res; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } long[][] twoDArray(int n, int m){ long[][] arr = new long[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ arr[i][j]=nextLong(); } } return arr; } } } class Point implements Comparable<Point>{ long start; long end; long val; Point(long start, long end, long val){ this.start=start; this.end=end; this.val=val; } // int getKey(){ // return key; // } // long getValue(){ // return value; // } // int getK(){ // return k; // } @Override public int compareTo(Point p){ if(this.end-p.end>0){ return 1; } else if(this.end==p.end){ return 0; } else{ return -1; } } } class Node{ int x; int y; Node(int a,int b){ x=a; y=b; } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
3b94efcb3e9bc530ce54ac0e0c356606
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.util.ArrayList; import java.util.Collections; import java.util.Map; import java.util.Scanner; import java.util.LinkedHashMap; import java.util.List; public class AquaMoon_and_Strange_Sort { public static void main(String[] args) { Scanner input = new Scanner(System.in); Integer count = input.nextInt(); while(count-->0){ Integer map_size = input.nextInt(); Map<Integer, List<Integer> > input_map = new LinkedHashMap<Integer, List<Integer> >(); ArrayList<Integer> sort_array = new ArrayList<Integer>(map_size); //紀錄初始bool 和 值 for(int i =0;i<map_size;i++){ Integer friend = input.nextInt(); sort_array.add(friend); put_samekey( input_map, friend , i%2 ); } Map< Integer, List<Integer> > map = new TreeMap< Integer, List<Integer> >(input_map); //System.out.println(map.toString()); //key,value排序(小到大) Collections.sort(sort_array); Map<Integer, List<Integer> > input_sort_map = new LinkedHashMap<Integer, List<Integer> >(); for(int i =0;i<map_size;i++){ put_samekey( input_sort_map, sort_array.get(i) , i%2 ); } Map< Integer, List<Integer> > sort_map = new TreeMap< Integer, List<Integer> >(input_sort_map); //System.out.println(sort_map.toString()); //Map取values->2維陣列 List<List<Integer>> map_Array = new ArrayList<List<Integer>>(map.values()); List<List<Integer>> sort_map_Array = new ArrayList<List<Integer>>(sort_map.values()); for(int i=0 ; i<sort_map_Array.size() ; i++){ List<Integer> temp1 = map_Array.get(i); Collections.sort( temp1 , new Comparator<Object>(){ public int compare( Object l1, Object l2 ) { // 回傳值: -1 前者比後者小, 0 前者與後者相同, 1 前者比後者大 return l1.toString().toLowerCase().compareTo(l2.toString().toLowerCase()); } }); map_Array.set( i , temp1 ); List<Integer> temp2 = sort_map_Array.get(i); Collections.sort( temp2 , new Comparator<Object>(){ public int compare( Object l1, Object l2 ) { // 回傳值: -1 前者比後者小, 0 前者與後者相同, 1 前者比後者大 return l1.toString().toLowerCase().compareTo(l2.toString().toLowerCase()); } }); sort_map_Array.set( i , temp2 ); } // System.out.println(map_Array.toString()); // System.out.println(sort_map_Array.toString()); ArrayList<Integer> orignal_array = new ArrayList<Integer>(map_size); ArrayList<Integer> temp_sort_array = sort_array; // 篩選重複元素 sort_array.clear(); //2維轉一維 for(int i =0;i<map_Array.size();i++){ for(int j =0;j<map_Array.get(i).size();j++){ orignal_array.add(map_Array.get(i).get(j)); } } for(int i =0;i<sort_map_Array.size();i++){ for(int j =0;j<sort_map_Array.get(i).size();j++){ sort_array.add(sort_map_Array.get(i).get(j)); } } //確認兩邊陣列一致 int sum = 0; for(int i =0;i<map_size;i++){ sum += Math.abs(orignal_array.get(i)-sort_array.get(i)); } // System.out.println(orignal_array.toString()); // System.out.println(sort_array.toString()); // System.out.println(sum); if(sum==0){ System.out.println("YES"); } else System.out.println("NO"); map.clear(); sort_map.clear(); } } public static void put_samekey(Map<Integer, List<Integer> > map, Integer key, Integer value) { if (map.get(key) == null) { List<Integer> list = new ArrayList<>(); list.add(value); map.put(key, list); } else { map.get(key).add(value); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
d3fc58b7f2b25f759db4b43ee0a274a7
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
//package codeforces; import java.io.*; import java.util.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws java.lang.Exception { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fr.ni(); while(t-->0) { int n = fr.ni(); Map<Integer,Integer> odd = new HashMap<>(); Map<Integer,Integer> even = new HashMap<>(); int arr [] = new int[n]; for(int i = 0 ; i < n ; i++) { arr[i] = fr.ni(); if((i&1) == 0)even.put(arr[i], even.getOrDefault(arr[i], 0)+1); else odd.put(arr[i],odd.getOrDefault(arr[i], 0)+1); } Arrays.sort(arr); boolean ans = true; for(int i = 0 ; i < n ; i++) { if((i&1) == 0) { if(even.containsKey(arr[i])) { int value = even.get(arr[i]); if(value -1 == 0)even.remove(arr[i]); else even.put(arr[i], value-1); }else { ans = false; break; } }else { if(odd.containsKey(arr[i])) { int value = odd.get(arr[i]); if(value -1 == 0)odd.remove(arr[i]); else odd.put(arr[i], value-1); }else { ans = false; break; } } } if(ans)out.println("YES"); else out.println("NO"); } out.close(); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
0faff4072fc46411e2ccbd72700bfe12
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.Scanner; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; ++i) { a[i] = sc.nextInt(); } System.out.println(solve(a) ? "YES" : "NO"); } sc.close(); } static boolean solve(int[] a) { int[] evenSorted = IntStream.range(0, a.length) .filter(i -> i % 2 == 0) .map(i -> a[i]) .boxed() .sorted() .mapToInt(x -> x) .toArray(); int[] oddSorted = IntStream.range(0, a.length) .filter(i -> i % 2 != 0) .map(i -> a[i]) .boxed() .sorted() .mapToInt(x -> x) .toArray(); int[] combined = IntStream.range(0, a.length) .map(i -> (i % 2 == 0) ? evenSorted[i / 2] : oddSorted[i / 2]) .toArray(); return IntStream.range(0, combined.length - 1).allMatch(i -> combined[i] <= combined[i + 1]); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
7d440a380c9a0d7018709c057384aa8b
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class A1545 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t=0; t<T; t++) { int N = in.nextInt(); List<Integer> even = new ArrayList<>(); List<Integer> odd = new ArrayList<>(); for (int n=0; n<N; n++) { ((n % 2 == 0) ? even : odd).add(in.nextInt()); } Collections.sort(even); Collections.sort(odd); boolean sorted = true; for (int n=1; n<N; n++) { int prev; int curr; if (n%2 == 0) { prev = odd.get((n-1)/2); curr = even.get(n/2); } else { prev = even.get((n-1)/2); curr = odd.get(n/2); } if (prev > curr) { sorted = false; break; } } System.out.println(sorted ? "YES" : "NO"); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
feb5217a545fbfce8feb047a10420a18
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.security.KeyStore.Entry; import java.util.*; public class Odd_Selection { static InputStreamReader r = new InputStreamReader(System.in); static BufferedReader br = new BufferedReader(r); static PrintWriter p = new PrintWriter(System.out); static int n; public static void main(String[] args) throws Exception { int test = Integer.parseInt(br.readLine()); // int test = 1; me146: while (test-- > 0) { String str[]=splitString(); int n = Integer.parseInt(str[0]); int a[]=new int[n]; str=splitString(); for(int i=0;i<n;i++){ a[i]=Integer.parseInt(str[i]); } int[][] count = new int[100001][2]; for (int i = 0; i < n; i++) { count[a[i]][i % 2]++; } Arrays.sort(a); for (int i = 0; i < n; i++) { count[a[i]][i % 2]--; } boolean pr = true; for (int i = 0; i < n; i++) { if (count[a[i]][0] != 0 || count[a[i]][1] != 0) { pr = false; break; } } if (pr) p.println("YES"); else p.println("NO"); } p.flush(); } static int root(int union[],int i){ while(union[i]!=i){ union[i] = union[union[i]]; i=union[i]; } return i; } static int[] join(int union[],int a,int b){ int rootA=root(union, a); int rootB=root(union,b); union[rootA] = union[rootB]; return union; } static boolean find(int union[],int a,int b){ if(root(union, a)==root(union,b)) return true; else return false; } static String[] splitString() throws IOException { return br.readLine().trim().split(" "); } } class Obj{ int first; int last; public Obj(int first, int last) { this.first = first; this.last = last; } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
505ee7356f392db445639e8f69c92dbe
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; public class CodeForces { public static void main(String[] args) throws InterruptedException { // StringBuilder sbd = new StringBuilder(); // PrintWriter out = new PrintWriter("output.txt"); // File input = new File("input.txt"); // FastScanner fs = new FastScanner(input); // FastScanner fs = new FastScanner(); // Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int testNumber =fs.nextInt(); // ArrayList<Integer> arr = new ArrayList<>(); for (int T =0;T<testNumber;T++){ // StringBuffer sbf = new StringBuffer(); // char [] arr = fs.next().toCharArray()1; int n= fs.nextInt(); int []A = fs.readArray(n); int[]oddA=new int[(int)1e5+3]; int[]evA=new int[(int)1e5+3]; int[]oddB=new int[(int)1e5+3]; int[]evB=new int[(int)1e5+3]; for (int i=1;i<=n;i++){ if(i%2==1){ oddA[A[i-1]]++; }else { evA[A[i-1]]++; } } sort(A); for (int i=1;i<=n;i++){ if(i%2==1){ oddB[A[i-1]]++; }else { evB[A[i-1]]++; } } boolean ans =true; for(int i=0;i<=1e5;i++)if(evA[i]!=evB[i]||oddA[i]!=oddB[i]){ ans=false; break; } out.print(ans?"YES\n":"NO\n"); } out.flush(); } static int[] factorials(int max,int mod){ int [] ans = new int[max+1]; ans[0]=1; for (int i=1;i<=max;i++){ ans[i]=ans[i-1]*i; ans[i]%=mod; } return ans; } static String toBinary(int num,int bits){ String res =Integer.toBinaryString(bits); while(res.length()<bits)res="0"+res; return res; } static String toBinary(long num,int bits){ String res =Long.toBinaryString(bits); while(res.length()<bits)res="0"+res; return res; } static long LCM(int a,int b){ return a*b/gcd(a,b); } static long FastPower(long x,long p){ if(p==0)return 1; long ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static double FastPower(double x,int p){ if(p==0)return 1.0; double ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static int FastPower(int x,int p){ if(p==0)return 1; int ans =FastPower(x, p/2); ans*=ans; if(p%2==1)ans*=x; return ans; } static ArrayList<Vertex> vertices = new ArrayList<>(); static class Vertex { public ArrayList<Integer>edges = new ArrayList<>(); public boolean isEnd=false; public Vertex (){ for(int i=0;i<26;i++){ edges.set(i, -1); } } } static class Trie { private int root=0; public Trie(){ vertices.add(new Vertex()); } public void InsertWord(String s){ int current = root; for(char c:s.toCharArray()){ int pos = c-'a'; if(vertices.get(current).edges.get(pos)==-1){ vertices.add(new Vertex()); Vertex x = vertices.get(current); x.edges.set(pos,vertices.size()-1); vertices.set(current, x); } current=vertices.get(current).edges.get(pos); } } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } public FastScanner(File f){ try { br=new BufferedReader(new FileReader(f)); st=new StringTokenizer(""); } catch(FileNotFoundException e){ br=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); } } String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n) { long[] a =new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } public static long factorial(int n){ if(n==0)return 1; return (long)n*factorial(n-1); } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } static void sort (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sortReversed (int[]a){ ArrayList<Integer> b = new ArrayList<>(); for(int i:a)b.add(i); Collections.sort(b,new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static void sort (long[]a){ ArrayList<Long> b = new ArrayList<>(); for(long i:a)b.add(i); Collections.sort(b); for(int i=0;i<b.size();i++){ a[i]=b.get(i); } } static ArrayList<Integer> sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } ArrayList<Integer> ans = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (prime[i] == true) ans.add(i); } return ans; } static int binarySearchSmallerOrEqual(int arr[], int key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } static int binarySearchSmallerOrEqual(long arr[], long key) { int n = arr.length; int left = 0, right = n; int mid = 0; while (left < right) { mid = (right + left) >> 1; if (arr[mid] == key) { while (mid + 1 < n && arr[mid + 1] == key) mid++; break; } else if (arr[mid] > key) right = mid; else left = mid + 1; } while (mid > -1 && arr[mid] > key) mid--; return mid; } public static int binarySearchStrictlySmaller(int[] arr, int target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } public static int binarySearchStrictlySmaller(long[] arr, long target) { int start = 0, end = arr.length-1; if(end == 0) return -1; if (target > arr[end]) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static int binarySearch(long arr[], long x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } static void init(int[]arr,int val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static void init(int[][]arr,int val){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ arr[i][j]=val; } } } static void init(long[]arr,long val){ for(int i=0;i<arr.length;i++){ arr[i]=val; } } static<T> void init(ArrayList<ArrayList<T>>arr,int n){ for(int i=0;i<n;i++){ arr.add(new ArrayList()); } } static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target) { int start = 0, end = arr.size()-1; if(end == 0) return -1; if (target > arr.get(end).y) return end; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid).y >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } static int binarySearchStrictlyGreater(int[] arr, int target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] <= target) { start = mid + 1; } else { ans = mid; end = mid - 1; } } return ans; } public static long pow(long n, long pow) { if (pow == 0) { return 1; } long retval = n; for (long i = 2; i <= pow; i++) { retval *= n; } return retval; } static String reverse(String s){ StringBuffer b = new StringBuffer(s); b.reverse(); return b.toString(); } static String charToString (char[] arr){ String t=""; for(char c :arr){ t+=c; } return t; } int[] copy (int [] arr , int start){ int[] res = new int[arr.length-start]; for (int i=start;i<arr.length;i++)res[i-start]=arr[i]; return res; } static int[] swap(int [] A,int l,int r){ int[] B=new int[A.length]; for (int i=0;i<l;i++){ B[i]=A[i]; } int k=0; for (int i=r;i>=l;i--){ B[l+k]=A[i]; k++; } for (int i=r+1;i<A.length;i++){ B[i]=A[i]; } return B; } static int mex (int[] d){ int [] a = Arrays.copyOf(d, d.length); sort(a); if(a[0]!=0)return 0; int ans=1; for(int i=1;i<a.length;i++){ if(a[i]==a[i-1])continue; if(a[i]==a[i-1]+1)ans++; else break; } return ans; } static int[] mexes(int[] arr){ int[] freq = new int [100000+7]; for (int i:arr)freq[i]++; int maxMex =0; for (int i=0;i<=100000+7;i++){ if(freq[i]!=0)maxMex++; else break; } int []ans = new int[arr.length]; ans[arr.length-1] = maxMex; for (int i=arr.length-2;i>=0;i--){ freq[arr[i+1]]--; if(freq[arr[i+1]]<=0){ if(arr[i+1]<maxMex) maxMex=arr[i+1]; ans[i]=maxMex; } else { ans[i]=ans[i+1]; } } return ans; } static int [] freq (int[]arr,int max){ int []b = new int[max]; for (int i:arr)b[i]++; return b; } static int[] prefixSum(int[] arr){ int [] a = new int[arr.length]; a[0]=arr[0]; for (int i=1;i<arr.length;i++)a[i]=a[i-1]+arr[i]; return a; } static class Pair { int x; int y; public int extra; public Pair(int x,int y){ this.x=x; this.y=y; } public Pair(int x,int y,int extra){ this.x=x; this.y=y; this.extra=extra; } @Override public boolean equals(Object o) { if(o instanceof Pair){ if(o.hashCode()!=hashCode()){ return false; } else { return x==((Pair)o).x&&y==((Pair)o).y; } } return false; } @Override public int hashCode() { return x+2*y; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
0ebf285cc07a4ff7e4529fb2f2c54c5b
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class StrangeSort{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static void solve() { int n = sc.nextInt(); HashMap<Integer,Pair> map = new HashMap<>(); int arr[] = new int[n]; for(int i = 0;i<n;i++){ int temp = sc.nextInt(); arr[i] = temp; if(map.containsKey(temp)){ Pair p = map.get(temp); if(i%2==0){ map.put(temp,new Pair(p.odd,p.even+1)); }else{ map.put(temp,new Pair(p.odd+1,p.even)); } }else{ if(i%2==0){ map.put(temp,new Pair(0,1)); }else{ map.put(temp,new Pair(1,0)); } } } int brr[] = Arrays.copyOf(arr,n); Arrays.sort(brr); for(int i = 0;i<n;i++){ Pair p = map.get(brr[i]); int o = p.odd; int e = p.even; if(i%2==0){ if(e==0){ out.println("NO"); return; }else{ e--; } }else{ if(o==0){ out.println("NO"); return; }else{ o--; } } map.put(brr[i],new Pair(o,e)); } out.println("YES"); } static void reverse(int arr[]){ int i = 0;int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static class Pair{ int odd; int even; Pair(int o,int e){ odd = o; even = e; } } 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(); } // 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
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
def0c07bf5ecfc8e2f662add2c43ba33
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static class Pair implements Comparable < Pair > { int d; int i; Pair(int d, int i) { this.d = d; this.i = i; } public int compareTo(Pair o) { return this.d - o.d; } } public static class SegmentTree { long[] st; long[] lazy; int n; SegmentTree(long[] arr, int n) { this.n = n; st = new long[4 * n]; lazy = new long[4 * n]; construct(arr, 0, n - 1, 0); } public long construct(long[] arr, int si, int ei, int node) { if (si == ei) { st[node] = arr[si]; return arr[si]; } int mid = (si + ei) / 2; long left = construct(arr, si, mid, 2 * node + 1); long right = construct(arr, mid + 1, ei, 2 * node + 2); st[node] = left + right; return st[node]; } public long get(int l, int r) { return get(0, n - 1, l, r, 0); } public long get(int si, int ei, int l, int r, int node) { if (r < si || l > ei) return 0; if (lazy[node] != 0) { st[node] += lazy[node] * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += lazy[node]; lazy[2 * node + 2] += lazy[node]; } lazy[node] = 0; } if (l <= si && r >= ei) return st[node]; int mid = (si + ei) / 2; return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2); } public void update(int index, int value) { update(0, n - 1, index, 0, value); } public void update(int si, int ei, int index, int node, int val) { if (si == ei) { st[node] = val; return; } int mid = (si + ei) / 2; if (index <= mid) { update(si, mid, index, 2 * node + 1, val); } else { update(mid + 1, ei, index, 2 * node + 2, val); } st[node] = st[2 * node + 1] + st[2 * node + 2]; } public void rangeUpdate(int l, int r, int val) { rangeUpdate(0, n - 1, l, r, 0, val); } public void rangeUpdate(int si, int ei, int l, int r, int node, int val) { if (r < si || l > ei) return; if (lazy[node] != 0) { st[node] += lazy[node] * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += lazy[node]; lazy[2 * node + 2] += lazy[node]; } lazy[node] = 0; } if (l <= si && r >= ei) { st[node] += val * (ei - si + 1); if (si != ei) { lazy[2 * node + 1] += val; lazy[2 * node + 2] += val; } return; } int mid = (si + ei) / 2; rangeUpdate(si, mid, l, r, 2 * node + 1, val); rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val); st[node] = st[2 * node + 1] + st[2 * node + 2]; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { try { System.setIn(new FileInputStream(new File("input.txt"))); System.setOut(new PrintStream(new File("output.txt"))); } catch (Exception e) { } } Reader sc = new Reader(); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); HashMap<Integer,Pair> hm=new HashMap<>(); HashMap<Integer,Pair> tar=new HashMap<>(); for(int i=0;i<n;i++) { if(!hm.containsKey(arr[i])) { Pair p=new Pair(0,0); hm.put(arr[i],p); } if(i%2==1) { hm.get(arr[i]).d+=1; } else { hm.get(arr[i]).i+=1; } } Arrays.sort(arr); for(int i=0;i<n;i++) { if(!tar.containsKey(arr[i])) { Pair p=new Pair(0,0); tar.put(arr[i],p); } if(i%2==1) { tar.get(arr[i]).d+=1; } else { tar.get(arr[i]).i+=1; } } boolean flag=true; for(int key:hm.keySet()) { Pair a=hm.get(key); Pair b=tar.get(key); if((a.d!=b.d)||(a.i!=b.i)) { flag=false; break; } } if(flag) sb.append("YES"); else sb.append("NO"); sb.append("\n"); } System.out.println(sb); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static boolean isPos(int idx, long[] arr, long[] diff) { if (idx == 0) { for (int i = 0; i <= Math.min(arr[0], arr[1]); i++) { diff[idx] = i; arr[0] -= i; arr[1] -= i; if (isPos(idx + 1, arr, diff)) { return true; } arr[0] += i; arr[1] += i; } } else if (idx == 1) { if (arr[2] - arr[1] >= 0) { long k = arr[1]; diff[idx] = k; arr[1] = 0; arr[2] -= k; if (isPos(idx + 1, arr, diff)) { return true; } arr[1] = k; arr[2] += k; } else return false; } else { if (arr[2] == arr[0] && arr[1] == 0) { diff[2] = arr[2]; return true; } else { return false; } } return false; } 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; } static int upperBound(ArrayList<Long> arr, long key) { int mid, N = arr.size(); // Initialise starting index and // ending index int low = 0; int high = N; // Till low is less than high while (low < high && low != N) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is greater than or equal // to arr[mid], then find in // right subarray if (key >= arr.get(mid)) { low = mid + 1; } // If key is less than arr[mid] // then find in left subarray else { high = mid; } } // If key is greater than last element which is // array[n-1] then upper bound // does not exists in the array return low; } static int lowerBound(ArrayList<Long> array, long key) { // Initialize starting index and // ending index int low = 0, high = array.size(); int mid; // Till high does not crosses low while (low < high) { // Find the index of the middle element mid = low + (high - low) / 2; // If key is less than or equal // to array[mid], then find in // left subarray if (key <= array.get(mid)) { high = mid; } // If key is greater than array[mid], // then find in right subarray else { low = mid + 1; } } // If key is greater than last element which is // array[n-1] then lower bound // does not exists in the array if (low < array.size() && array.get(low) < key) { low++; } // Returning the lower_bound index return low; } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
0f6b8506f3107ea97d33b1de6dc12e2e
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int ttt = 1; ttt <= T; ttt++) { int n = in.nextInt(); int[] a = new int[n]; int[][] count = new int[100001][2]; for(int i = 0; i < n; i++) { a[i] = in.nextInt(); count[a[i]][i%2]++; } Arrays.sort(a); for(int i = 0; i < n; i++) { count[a[i]][i%2]--; } boolean p = true; for(int i = 0; i < n; i++) { if(count[a[i]][0]!=0 || count[a[i]][1]!=0) { p = false; break; } } if(p) out.println("YES"); else out.println("NO"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
376b3cadfe97b180e692905c2ac4de63
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import static java.lang.Math.*; import java.io.*; public class S { public static int surv = 0; public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int test = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while(test > 0){ test--; int n = Integer.parseInt(br.readLine()); int a[] = new int[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++){ a[i] = Integer.parseInt(st.nextToken()); } solve(a, n, sb); } System.out.print(sb.toString()); } final static int MOD = 1000000007; public static void solve(int a[], int n, StringBuilder sb){ int cnt[][] = new int[100001][2]; for(int i = 0; i < n; i++){ cnt[a[i]][(i%2)]++; } sort(a); for(int i = 0; i < n; i++){ cnt[a[i]][(i%2)]--; } for(int i = 0; i < cnt.length; i++){ if(cnt[i][0] != 0 || cnt[i][1] != 0){ sb.append("NO\n"); return; } } sb.append("YES\n"); } public static long calc(int x){ long ans = 0; while(x > 0){ ans += x; x /= 10; } return ans; } public static boolean isPossible(int sum, int pairs, int len, int k){ if(len % 2 == 0){ return pairs >= ((len/2)*k); }else{ int ones = sum - (pairs*2); if(pairs < (len/2)*k){ return false; } pairs -= ((len/2)*k); return ((pairs*2) + ones) >= k; } } public static void balance(int a[], int b[], int i, int amn){ a[i] -= amn; b[i] += amn; } public static long powerr(long base, long exp){ if(exp < 2)return base; if(exp % 2 == 0){ long ans = powerr(base, exp/2) % MOD; ans *= ans; ans %= MOD; return ans; }else{ return (((powerr(base, exp-1)) % MOD) * base) % MOD; } } public static long power(long a, long b){ if(b == 0)return 1l; long ans = power(a, b/2); ans *= ans; ans %= MOD; if(b % 2 != 0){ ans *= a; } return ans % MOD; } public static int logLong(long a){ int ans = 0; long b = 1; while(b < a){ b*=2; ans++; } return ans; } public static void rec(int a[], int l, int r, int d, int d_map[]){ if(l > r)return; if(l == r){ d_map[a[l]] = d; return; } int max = 0; int max_ind = -1; for(int i = l; i <= r; i++){ if(a[i] > max){ max_ind = i; max = a[i]; } } d_map[a[max_ind]] = d; rec(a, l, max_ind - 1, d+1, d_map); rec(a, max_ind + 1, r, d+1, d_map); } public static void sort(int a[]){ List<Integer> l = new ArrayList<Integer>(); for(int val : a){ l.add(val); } Collections.sort(l); int k = 0; for(int val : l){ a[k++] = val; } } public static void sortLong(long a[]){ List<Long> l = new ArrayList<Long>(); for(long val : a){ l.add(val); } Collections.sort(l); int k = 0; for(long val : l){ a[k++] = val; } } public static boolean isPal(String s){ int l = 0; int r = s.length() - 1; while(l <= r){ if(s.charAt(l) != s.charAt(r)){ return false; } l++; r--; } return true; } public static int gcd(int a, int b){ if(b > a){ int temp = a; a = b; b = temp; } if(b == 0)return a; return gcd(b, a%b); } /* public static long gcd(long a, long b){ if(b > a){ long temp = a; a = b; b = temp; } if(b == 0)return a; return gcd(b, a%b); }*/ // public static long lcm(long a, long b){ // return a * b/gcd(a, b); // } /*public static class DJSet{ public int a[]; public DJSet(int n){ this.a = new int[n]; Arrays.fill(a, -1); } public int find(int val){ if(a[val] >= 0){ a[val] = find(a[val]); return a[val]; }else{ return val; } } public boolean union(int val1, int val2){ int p1 = find(val1); int p2 = find(val2); if(p1 == p2){ return false; } int size1 = Math.abs(a[p1]); int size2 = Math.abs(a[p2]); if(size1 >= size2){ a[p2] = p1; a[p1] = (size1 + size2) * -1; }else{ a[p1] = p2; a[p2] = (size1 + size2) * -1; } return true; }*/ /* public static class DSU{ public int a[]; public DSU(int size){ this.a = new int[size]; Arrays.fill(a, -1); } public int find(int u){ if(a[u] < 0)return u; a[u] = find(a[u]); return a[u]; } public boolean union(int u, int v){ int p1 = find(u); int p2 = find(v); if(p1 == p2)return false; int size1 = a[p1] * -1; int size2 = a[p2] * -1; if(size1 >= size2){ a[p2] = p1; a[p1] = (size1 + size2) * -1; }else{ a[p1] = p2; a[p2] = (size1 + size2) * -1; } return true; } }*/ }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
234ab9417abf253b4244d80679f2f2a0
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class A1545 { public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); FastPrintStream out = new FastPrintStream(System.out); int t = sc.nextInt(); for (int tn = 0; tn < t; tn++) { int[] a = readIntArray(sc.nextInt(), sc); out.println(solve(a)); } out.flush(); } private static String solve(int[] a) { Map<Integer, Rec> count = new HashMap<>(); for (int i = 0; i < a.length; i++) { if (!count.containsKey(a[i])) { count.put(a[i], new Rec()); } if (i % 2 == 0) { count.get(a[i]).even++; } else { count.get(a[i]).odd++; } } a = sorted(a); for (int i = 0; i < a.length; i++) { Rec rec = count.get(a[i]); if (i % 2 == 0) { if (rec.even == 0) { return "NO"; } rec.even--; } else { if (rec.odd == 0) { return "NO"; } rec.odd--; } } return "YES"; } static class Rec { int even; int odd; } static int[] readIntArray(int n, FastScanner sc) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } return a; } static int[] sorted(int[] array) { ArrayList<Integer> list = new ArrayList<>(array.length); for (int val : array) { list.add(val); } list.sort(Comparator.naturalOrder()); return list.stream().mapToInt(val -> val).toArray(); } static int[] sortedReverse(int[] array) { ArrayList<Integer> list = new ArrayList<>(array.length); for (int val : array) { list.add(val); } list.sort(Comparator.reverseOrder()); return list.stream().mapToInt(val -> val).toArray(); } static long[] sort(long[] array) { ArrayList<Long> list = new ArrayList<>(array.length); for (long val : array) { list.add(val); } list.sort(Comparator.naturalOrder()); return list.stream().mapToLong(val -> val).toArray(); } static long[] sortedReverse(long[] array) { ArrayList<Long> list = new ArrayList<>(array.length); for (long val : array) { list.add(val); } list.sort(Comparator.reverseOrder()); return list.stream().mapToLong(val -> val).toArray(); } private static class FastPrintStream { private final BufferedWriter writer; public FastPrintStream(PrintStream out) { writer = new BufferedWriter(new OutputStreamWriter(out)); } public void print(char c) { try { writer.write(Character.toString(c)); } catch (IOException e) { e.printStackTrace(); } } public void print(int val) { try { writer.write(Integer.toString(val)); } catch (IOException e) { e.printStackTrace(); } } public void print(long val) { try { writer.write(Long.toString(val)); } catch (IOException e) { e.printStackTrace(); } } public void print(double val) { try { writer.write(Double.toString(val)); } catch (IOException e) { e.printStackTrace(); } } public void print(String val) { try { writer.write(val); } catch (IOException e) { e.printStackTrace(); } } public void println(char c) { print(c + "\n"); } public void println(int val) { print(val + "\n"); } public void println(long val) { print(val + "\n"); } public void println(double val) { print(val + "\n"); } public void println(String str) { print(str + "\n"); } public void println() { print("\n"); } public void flush() { try { writer.flush(); } catch (IOException e) { e.printStackTrace(); } } } private static class FastScanner { private final BufferedReader br; private StringTokenizer st; public FastScanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public byte nextByte() { return Byte.parseByte(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
0cfb900c19e118fedba2aee5cc1d5399
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
//import java.io.IOException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class AquaMoonandStrangeSort { static InputReader inputReader=new InputReader(System.in); static void solve() { int n=inputReader.nextInt(); int arr[]=inputReader.nextIntArray(n); int count[][]=new int[1__00__001][2]; for(int i=0;i<n;i++) { count[arr[i]][i%2]++; } Arrays.sort(arr); for(int i=0;i<n;i++) { count[arr[i]][i%2]--; } for(int i=0;i<(1__00__001);i++) { if(count[i][0]!=0||count[i][1]!=0) { out.println("NO"); return; } } out.println("YES"); } static PrintWriter out=new PrintWriter((System.out)); public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while(t-->0) { solve(); } out.close(); } static void sortDec(int arr[]) { int len=arr.length; List<Integer>list=new ArrayList<>(); for(int ele:arr) { list.add(ele); } Collections.sort(list,Collections.reverseOrder()); for(int i=0;i<len;i++) { arr[i] = list.get(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
5981cbb825492facfddc1c97cecf7cb9
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class new1 { public static void main (String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int i = 0; i < t; i++) { int n = s.nextInt(); int[] arr1 = new int[n]; int[] arr2 = new int[n]; for(int j = 0; j < n; j++) { arr1[j] = s.nextInt(); arr2[j] = arr1[j]; } Arrays.sort(arr1); int[] odd = new int[100001]; int[] even = new int[100001]; for(int j = 0; j < n; j++) { if(j % 2 == 0) { even[arr1[j]]++; } else { odd[arr1[j]]++; } } boolean b = true; for(int j = 0; j < n; j++) { int val = arr2[j]; if(j % 2 == 0) { if(even[val] > 0) even[val]--; else { b = false; break; } } else { if(odd[val] > 0) odd[val]--; else { b = false; break; } } } if(b == true) System.out.println("YES"); else System.out.println("NO"); } } } 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(); } public 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
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
dc063d937849162fad3e48e597cbf226
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.io.PrintStream; //import java.util.*; public class Solution { public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; static class FastScanner { BufferedReader br; StringTokenizer st ; FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } FastScanner(String file) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); st = new StringTokenizer(""); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found"); e.printStackTrace(); } } 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()); } String readLine() throws IOException{ return br.readLine(); } } static class Pair{ String word; int cnt; Pair(String word,int cnt){ this.word = word; this.cnt = cnt; } } public static void main(String[] args) throws Exception { FastScanner s = new FastScanner(); if(LOCAL){ s = new FastScanner("src/input.txt"); PrintStream o = new PrintStream("src/sampleout.txt"); System.setOut(o); } int tcr = s.nextInt(); for(int tc=0;tc<tcr;tc++){ int n = s.nextInt(); int arr[] = new int[n]; List<Integer> even = new ArrayList<>(); List<Integer> odd = new ArrayList<>(); for(int i=0;i<n;i++){ arr[i] = s.nextInt(); if(i % 2 == 0){ even.add(arr[i]); }else{ odd.add(arr[i]); } } Collections.sort(even); Collections.sort(odd); int e_i = 0; int o_i = 0; boolean poss = true; arr[0] = even.get(e_i); e_i++; for(int i=1;i<n;i++){ if(i%2 == 0){ arr[i] = even.get(e_i); e_i++; if(arr[i] < arr[i-1]){ poss = false; break; } }else{ arr[i] = odd.get(o_i); o_i++; if(arr[i] < arr[i-1]){ poss = false; break; } } } println(poss?"YES":"NO"); } } public static void println(Object obj){ System.out.println(obj.toString()); } public static void print(Object obj){ System.out.println(obj.toString()); } public static long getA(long num){ if(num == 0l){ return 0l; } long ans = num - 1; long pow = 10; while((num/pow) > 0){ ans = ans + (num/pow); pow = pow * 10l; } return ans; } public static long gcd(long a,long b){ if(b == 0l){ return a; } return gcd(b,a%b); } public static int find(int parent[],int v){ if(parent[v] == v){ return v; } return parent[v] = find(parent, parent[v]); } public static List<Integer> sieve(){ List<Integer> prime = new ArrayList<>(); int arr[] = new int[100001]; Arrays.fill(arr,1); arr[1] = 0; arr[2] = 1; for(int i=2;i<=100000;i++){ if(arr[i] == 1){ prime.add(i); for(long j = (i*1l*i);j<100001;j+=i){ arr[(int)j] = 0; } } } return prime; } static boolean isPower(long n,long a){ long log = (long)(Math.log(n)/Math.log(a)); long power = (long)Math.pow(a,log); if(power == n){return true;} return false; } private static int mergeAndCount(int[] arr, int l,int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l, swaps = 0; while (i < left.length && j < right.length) { if (left[i] <= right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static int mergeSortAndCount(int[] arr, int l,int r) { // Keeps track of the inversion count at a // particular node of the recursion tree int count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } static class Debug { //change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; private static <T> String ts(T t) { if(t==null) { return "null"; } try { return ts((Iterable) t); }catch(ClassCastException e) { if(t instanceof int[]) { String s = Arrays.toString((int[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof long[]) { String s = Arrays.toString((long[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof char[]) { String s = Arrays.toString((char[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof double[]) { String s = Arrays.toString((double[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; }else if(t instanceof boolean[]) { String s = Arrays.toString((boolean[]) t); return "{"+s.substring(1, s.length()-1)+"}\n"; } try { return ts((Object[]) t); }catch(ClassCastException e1) { return t.toString(); } } } private static <T> String ts(T[] arr) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: arr) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } private static <T> String ts(Iterable<T> iter) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: iter) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } public static void dbg(Object... o) throws Exception { if(LOCAL) { PrintStream ps = new PrintStream("src/Debug.txt"); System.setErr(ps); System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": ["); for(int i = 0; i<o.length; i++) { if(i!=0) { System.err.print(", "); } System.err.print(ts(o[i])); } System.err.println("]"); } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
a5f8c0150ed7b99e171e0dc0065df264
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class CODECHEF { static class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static long MODE=1000000000+7; static long gcd(long a,long b){ if(a==0) return b; return gcd(b%a,a); } static boolean isSafe(int i,int n){ return (i>=0 && i<n); } static long[] dp; static boolean solve(int[] arr,int n){ //n=8 // 0 1 2 3 4 5 6 7 //n=7 // 0 1 2 3 4 5 6 int[] odd=new int[n/2]; int[] even; if((n&1)==1) even=new int[(n/2)+1]; else even=new int[n/2]; for(int i=0;i<n;i++){ if((i&1)==1) odd[i/2]=arr[i]; else even[i/2]=arr[i]; } Arrays.sort(arr); Arrays.sort(even); Arrays.sort(odd); int odd_ptr=0; int even_ptr=0; for(int i=0;i<n;i++){ if((i&1)==1){ if(arr[i]!=odd[odd_ptr]) return false; odd_ptr++; }else{ if(arr[i]!=even[even_ptr]) return false; even_ptr++; } } return true; } public static void main(String[] args) throws java.lang.Exception { FastReader fs=new FastReader(System.in); StringBuilder sb=new StringBuilder(); PrintWriter out=new PrintWriter(System.out); int t=fs.nextInt(); while (t-->0){ int n=fs.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) arr[i]=fs.nextInt(); System.out.println(solve(arr,n)?"YES":"NO"); } // out.close(); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
d81db380634b973a3e9f9a283551e0d7
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static void main() throws Exception{ int n=sc.nextInt(); int[]in=sc.intArr(n); int[]even=new int[(n+1)>>1],odd=new int[n>>1]; for(int i=0,idx=0;i<n;i+=2,idx++) { even[idx]=in[i]; } for(int i=1,idx=0;i<n;i+=2,idx++) { odd[idx]=in[i]; } shuffle(odd); shuffle(even); Arrays.sort(odd); Arrays.sort(even); int[]cur=new int[n]; for(int i=0,idx=0;i<n;i+=2,idx++) { cur[i]=even[idx]; } for(int i=1,idx=0;i<n;i+=2,idx++) { cur[i]=odd[idx]; } boolean yes=true; for(int i=1;i<n;i++) { yes&=cur[i]>=cur[i-1]; } pw.println(yes?"YES":"NO"); } public static void main(String[] args) throws Exception{ sc=new MScanner(System.in); pw = new PrintWriter(System.out); int tc=1; tc=sc.nextInt(); for(int i=1;i<=tc;i++) { // pw.printf("Case #%d: ", i); main(); } pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void dbg(int[]in) { System.out.println(Arrays.toString(in)); } static void dbg(long[]in) { System.out.println(Arrays.toString(in)); } static void sort(int[]in) { shuffle(in); Arrays.sort(in); } static void sort(long[]in) { shuffle(in); Arrays.sort(in); } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
4d362b516e058c999a7e30e2ed414868
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public final class Solution { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(1, 0), new Pair(0, -1), new Pair(0, 1)}; public static void main(String[] args) { int tt = i(); int mod = (int) 1e9 + 7; out: while (tt-- > 0) { int n = i(); int[] a = input(n); int[] copy = Arrays.copyOf(a, a.length); shuffleAndSort(copy); int max = copy[n - 1]; int[] apos = new int[max + 1]; int[] bpos = new int[max + 1]; for (int i = 0; i < n; i++) { if (i % 2 == 0) { apos[copy[i]]++; } else { bpos[copy[i]]++; } } for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (apos[a[i]] > 0) { apos[a[i]]--; } else { printNo(); continue out; } } else { if (bpos[a[i]] > 0) { bpos[a[i]]--; } else { printNo(); continue out; } } } printYes(); } out.flush(); } 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 void printYes() { out.println("YES"); } static void printNo() { out.println("NO"); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String s() { return in.nextLine(); } 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; } 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(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 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; } } class Pair { int i, j; Pair(int i, int j) { this.i = i; this.j = 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; } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
ea327b70e3084b0cda006a32c862a2f9
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class AquamoonAndStrangeSort { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int t = readInt(); while (t-- > 0) { int n = readInt(); Integer[] a = new Integer[n]; int[][] freq = new int[100001][2]; for (int i = 0; i < n; i ++) { a[i] = readInt(); freq[a[i]][i % 2] ++; } Arrays.parallelSort(a); boolean flag = true; int cur = -1; for (int i = 0; i < n; i ++) { if (a[i] == cur) continue; cur = a[i]; if (i % 2 == 0) { int works = freq[cur][0], notWorks = freq[cur][1]; if (notWorks < works - 1 || notWorks > works) { flag = false; break; } } else { int works = freq[cur][1], notWorks = freq[cur][0]; if (notWorks < works - 1 || notWorks > works) { flag = false; break; } } } System.out.println(flag ? "YES" : "NO"); } } 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
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
2cc478afbb9ec39941cd501c8e58f123
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.Arrays; import java.util.Comparator; import java.util.Locale; import java.util.Random; import java.util.StringTokenizer; public class Solution implements Runnable { private PrintStream out; private BufferedReader in; private StringTokenizer st; public void solve() throws IOException { long time0 = System.currentTimeMillis(); int t = nextInt(); for (int test = 1; test <= t; test++) { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } boolean ans = solve(n, a); if (ans) { out.println("YES"); } else { out.println("NO"); } } System.err.println("time: " + (System.currentTimeMillis() - time0)); } private boolean solve(int n, int[] a) { int[][] b = new int[n][2]; for (int i = 0; i < n; i++) { b[i][0] = a[i]; b[i][1] = i; } sort(b); for (int i = 0; i < n; ) { int j = i; int n0 = 0; int o0 = 0; while (j < n && b[j][0] == b[i][0]) { if (j % 2 == 0) { n0++; } if (b[j][1] % 2 == 0) { o0++; } j++; } if (n0 != o0) { return false; } i = j; } return true; } private void sort(int[][] a) { Random rnd = new Random(); for (int i = 0; i < Math.min(1000, a.length); i++) { int j = rnd.nextInt(a.length); int[] tmp = a[i]; a[i] = a[j]; a[j] = tmp; } Arrays.sort(a, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { if (o1[0] < o2[0]) { return -1; } if (o1[0] > o2[0]) { return 1; } return 0; } }); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } @Override public void run() { try { solve(); out.close(); } catch (Throwable e) { throw new RuntimeException(e); } } public Solution(String name) throws IOException { Locale.setDefault(Locale.US); if (name == null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintStream(new BufferedOutputStream(System.out)); } else { in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in"))); out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out"))); } st = new StringTokenizer(""); } public static void main(String[] args) throws IOException { new Thread(new Solution(null)).start(); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
d86816ea86187594c2dc25fe4d708a26
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.io.*; import java.util.*; public class D { FastScanner in; PrintWriter out; boolean systemIO = true; public int add(int x, int y) { if (x + y >= mod) { return x + y - mod; } return x + y; } public int subtract(int x, int y) { if (x >= y) { return x - y; } return x - y + mod; } public int multiply(long x, long y) { return (int) (x * 1L * y % mod); } public int divide(long x, long y) { return (int) (x * 1L * modInv(y) % mod); } public 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; } @Override 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; } } public class Fenvik { int[] sum; public Fenvik(int n) { sum = new int[n]; } public void add(int x, int d) { for (int i = x; i < sum.length; i = (i | (i + 1))) { sum[i] += d; } } public int sum(int r) { int ans = 0; for (int i = r; i >= 0; i = (i & (i + 1)) - 1) { ans += sum[i]; } return ans; } public int sum(int l, int r) { if (l > r) { return 0; } return sum(r) - sum(l - 1); } } public long gcd(long x, long y) { if (y == 0) { return x; } if (x == 0) { return y; } return gcd(y, x % y); } public long[][] pow(long[][] x, long p) { if (p == 0) { long[][] ans = new long[x.length][x.length]; for (int i = 0; i < ans.length; i++) { ans[i][i] = 1; } return ans; } long[][] t = pow(x, p / 2); t = multiply(t, t); if (p % 2 == 1) { t = multiply(t, x); } return t; } public long[][] multiply(long[][] a, long[][] b) { long[][] ans = new long[a.length][b[0].length]; for (int i = 0; i < ans.length; i++) { for (int j = 0; j < ans[0].length; j++) { for (int k = 0; k < b.length; k++) { ans[i][j] += a[i][k] * b[k][j]; ans[i][j] %= mod; } } } return ans; } public long pow(long x, long p) { if (p == 0) { return 1; } long t = pow(x, p / 2); t *= t; t %= mod; if (p % 2 == 1) { t *= x; t %= mod; } return t; } public long modInv(long x) { return pow(x, mod - 2); } int mod = 1000000007; Random random = new Random(566); public void solve() { f : for (int qwerty = in.nextInt(); qwerty > 0; qwerty--) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = in.nextInt(); } int[] odd = new int[n / 2]; int[] even = new int[n - n / 2]; for (int i = 0; i < a.length; i += 2) { even[i / 2] = a[i]; } for (int i = 1; i < a.length; i += 2) { odd[i / 2] = a[i]; } for (int i = 1; i < even.length; i++) { int x = random.nextInt(i); even[i] ^= even[x]; even[x] ^= even[i]; even[i] ^= even[x]; } for (int i = 1; i < odd.length; i++) { int x = random.nextInt(i); odd[i] ^= odd[x]; odd[x] ^= odd[i]; odd[i] ^= odd[x]; } Arrays.sort(even); Arrays.sort(odd); for (int i = 0; i < a.length; i++) { if (i % 2 == 0) { a[i] = even[i / 2]; } else { a[i] = odd[i / 2]; } } for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { out.println("NO"); continue f; } } out.println("YES"); } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } 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()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { long time = System.currentTimeMillis(); new D().run(); System.err.println(System.currentTimeMillis() - time); } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 8
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
1af984c3136a6b6f80eb3d1d5146470f
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
import java.util.*; import java.util.Scanner; import java.lang.Math; public class JvForces { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int cs = 0; cs < t; cs++) { int n = sc.nextInt(); TreeMap<Integer, ArrayList<Integer>> mp = new TreeMap<Integer, ArrayList<Integer>>(); for (int i = 0; i < n; i++) { int a = sc.nextInt(); if (mp.containsKey(a)) { ArrayList<Integer> temp = mp.get(a); temp.add(i); mp.put(a, temp); } else { ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(i); mp.put(a, arr); } } boolean flag=true; int cnt = 0; for (Map.Entry<Integer, ArrayList<Integer>> it : mp.entrySet()) { int tempe = 0, tempo = 0; for (Integer i : it.getValue()) { if (i % 2 == 0) { tempe++; } else { tempo++; } } if ((it.getValue().size() % 2) == 0) { if (tempe != tempo) { System.out.println("NO"); flag = false; break; } } else { if ((cnt % 2) == 0) { if (tempe - 1 != tempo) { System.out.println("NO"); flag = false; break; } } else { if (tempo - 1 != tempe) { System.out.println("NO"); flag = false; break; } } } cnt+=it.getValue().size(); } if(flag==true) { System.out.println("YES"); } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 17
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
eda88054f1cea91fc10734efefba8971
train_108.jsonl
1626012300
AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
256 megabytes
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.util.*; public class Main { public static boolean issorted(int []arr){ for(int i = 1;i<arr.length;i++){ if(arr[i]<arr[i-1]){ return false; } } return true; } public static long sum(int []arr){ long sum = 0; for(int i = 0;i<arr.length;i++){ sum+=arr[i]; } return sum; } public static class pair implements Comparable<pair>{ int x; int y; pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(pair o){ return this.x - o.x; // sort increasingly on the basis of x // return o.x - this.x // sort decreasingly on the basis of x } } public static void swap(int []arr,int i,int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static int []parent = new int[1000]; static int []size = new int[1000]; public static void make(int v){ parent[v] = v; size[v] = 1; } public static int find(int v){ if(parent[v]==v){ return v; } else{ return parent[v] = find(parent[v]); } } public static void union(int a,int b){ a = find(a); b = find(b); if(a!=b){ if(size[a]>size[b]){ parent[b] = parent[a]; size[b]+=size[a]; } else{ parent[a] = parent[b]; size[a]+=size[b]; } } } static boolean []visited = new boolean[1000]; public static void dfs(int vertex,ArrayList<ArrayList<Integer>>graph){ if(visited[vertex] == true){ return; } System.out.println(vertex); for(int child : graph.get(vertex)){ // work to be done before entering the child dfs(child,graph); // work to be done after exitting the child } } public static void displayint(int []arr){ for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]+" "); } System.out.println(); } public static void displaystr(String str){ StringBuilder sb = new StringBuilder(str); for(int i = 0;i<sb.length();i++){ System.out.print(sb.charAt(i)); } System.out.println(); } public static boolean checkbalanceparenthesis(StringBuilder ans){ Stack<Character>st = new Stack<>(); int i = 0; while(i<ans.length()){ if(ans.charAt(i) == '('){ st.push('('); } else{ if(st.size() == 0 || st.peek()!='('){ return false; } else{ st.pop(); } } } return st.size() == 0; } public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ int n = scn.nextInt(); int []arr = new int[n]; for(int i = 0;i<n;i++){ arr[i] = scn.nextInt(); } int [][]map = new int[2][100005]; for(int i = 0;i<n;i++){ map[i%2][arr[i]]++; } Arrays.sort(arr); for(int i = 0;i<n;i++){ map[i%2][arr[i]]--; } boolean flag = true; for(int i = 0;i<2;i++){ for(int j = 0;j<map[0].length;j++){ if(map[i][j]>0){ flag = false; break; } } } if(flag){ System.out.println("YES"); } else{ System.out.println("NO"); } } } }
Java
["3\n4\n4 3 2 5\n4\n3 3 2 2\n5\n1 2 3 5 4"]
1 second
["YES\nYES\nNO"]
NoteThe possible list of operations in the first test case: Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right. Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right. Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.
Java 17
standard input
[ "sortings" ]
1d27d6d736d891b03b7476f8a7209291
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.
1,500
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).
standard output
PASSED
24a89a16fa6b8cd279fca8c0ec27ca5f
train_108.jsonl
1626012300
Cirno has prepared $$$n$$$ arrays of length $$$n$$$ each. Each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. These arrays are special: for all $$$1 \leq i \leq n$$$, if we take the $$$i$$$-th element of each array and form another array of length $$$n$$$ with these elements, the resultant array is also a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. In the other words, if you put these $$$n$$$ arrays under each other to form a matrix with $$$n$$$ rows and $$$n$$$ columns, this matrix is a Latin square.Afterwards, Cirno added additional $$$n$$$ arrays, each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. For all $$$1 \leq i \leq n$$$, there exists at least one position $$$1 \leq k \leq n$$$, such that for the $$$i$$$-th array and the $$$(n + i)$$$-th array, the $$$k$$$-th element of both arrays is the same. Notice that the arrays indexed from $$$n + 1$$$ to $$$2n$$$ don't have to form a Latin square. Also, Cirno made sure that for all $$$2n$$$ arrays, no two arrays are completely equal, i. e. for all pair of indices $$$1 \leq i &lt; j \leq 2n$$$, there exists at least one position $$$1 \leq k \leq n$$$, such that the $$$k$$$-th elements of the $$$i$$$-th and $$$j$$$-th array are different.Finally, Cirno arbitrarily changed the order of $$$2n$$$ arrays.AquaMoon calls a subset of all $$$2n$$$ arrays of size $$$n$$$ good if these arrays from a Latin square.AquaMoon wants to know how many good subsets exist. Because this number may be particularly large, find it modulo $$$998\,244\,353$$$. Also, she wants to find any good subset. Can you help her?
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.List; import java.util.*; public class realfast implements Runnable { private static final int INF = (int) 1e9; long in= 998244353; long fac[]= new long[1000001]; long inv[]=new long[1000001]; public void solve() throws IOException { int t1 = readInt(); ArrayList<Integer> val[][]=new ArrayList[510][510]; int arr[][]=new int[1010][510]; int b[][]=new int[510][510]; // boolean row[]=new boolean[1000+1]; for(int f =0;f<t1;f++) { int n = readInt(); int stack[]= new int[n*n+10]; int stack2[]=new int[n*n+10]; int start=0; int end =0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) val[i][j]= new ArrayList<Integer>(); boolean map[]=new boolean[2*n+10]; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { b[i][j]=0; arr[i][j]=0; } } // for(int i=1;i<=n;i++) for(int i=1;i<=2*n;i++) { for(int j=1;j<=n;j++) { arr[i][j]= readInt(); b[j][arr[i][j]]++; val[j][arr[i][j]].add(i); } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(b[i][j]==1) { stack[end]=i; stack2[end]=j; end++; } } } long ans =1; ArrayList<Integer>pal = new ArrayList<Integer>(); int cnt =0; while(cnt<n) { int t=-1; if(start<end) { if(b[stack[start]][stack2[start]]!=1) { start++; continue; } int u = stack[start]; int v = stack2[start]; start++; for(int j = 0;j<val[u][v].size();j++) { if(!map[val[u][v].get(j)]) { t=val[u][v].get(j); break; } } } // out.println(t+" before"); if(t==-1) { ans = (ans*2)%in; for(int j=1;j<=2*n;j++) { if(!map[j]) { t=j; break; } } } // out.println(t+" after"); if(t==-1){ out.println("nigga"); break; } if(t!=-1) map[t]= true; cnt++; if(t!=-1) pal.add(t); for(int i=1;i<=n;i++) { b[i][arr[t][i]]=0; } for(int i =1;i<=n;i++) { int p = arr[t][i]; for(int j=0;j<val[i][p].size();j++) { int l = val[i][p].get(j); if(!map[l]) { map[l]= true; for(int k=1;k<=n;k++) { b[k][arr[l][k]]--; if(b[k][arr[l][k]]==1) { stack[end]= k; stack2[end]= arr[l][k]; end++; } } } } } } out.println(ans); for(int j =0;j<pal.size();j++) out.print(pal.get(j)+" "); out.println(); } } public int value (int seg[], int left , int right ,int index, int l, int r) { if(left>right) { return -100000000; } if(right<l||left>r) return -100000000; if(left>=l&&right<=r) return seg[index]; int mid = left+(right-left)/2; int val = value(seg,left,mid,2*index+1,l,r); int val2 = value(seg,mid+1,right,2*index+2,l,r); return Math.max(val,val2); } public int gcd(int a , int b ) { if(a<b) { int t =a; a=b; b=t; } if(a%b==0) return b ; return gcd(b,a%b); } public long pow(long n , long p,long m) { if(p==0) return 1; long val = pow(n,p/2,m);; val= (val*val)%m; if(p%2==0) return val; else return (val*n)%m; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new realfast(), "", 128 * (1L << 20)).start(); } private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter out; @Override public void run() { try { if (ONLINE_JUDGE || !new File("input.txt").exists()) { reader = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { reader = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } solve(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (IOException e) { // nothing } out.close(); } } private String readString() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } @SuppressWarnings("unused") private int readInt() throws IOException { return Integer.parseInt(readString()); } @SuppressWarnings("unused") private long readLong() throws IOException { return Long.parseLong(readString()); } @SuppressWarnings("unused") private double readDouble() throws IOException { return Double.parseDouble(readString()); } } class edge implements Comparable<edge>{ long u ; long v; edge(long u, long v) { this.u=u; this.v=v; } public int compareTo(edge e) { if(this.v>=e.v) return 1; else return -1; } }
Java
["3\n7\n1 2 3 4 5 6 7\n2 3 4 5 6 7 1\n3 4 5 6 7 1 2\n4 5 6 7 1 2 3\n5 6 7 1 2 3 4\n6 7 1 2 3 4 5\n7 1 2 3 4 5 6\n1 2 3 4 5 7 6\n1 3 4 5 6 7 2\n1 4 5 6 7 3 2\n1 5 6 7 4 2 3\n1 6 7 5 2 3 4\n1 7 6 2 3 4 5\n1 7 2 3 4 5 6\n5\n4 5 1 2 3\n3 5 2 4 1\n1 2 3 4 5\n5 2 4 1 3\n3 4 5 1 2\n2 3 4 5 1\n1 3 5 2 4\n4 1 3 5 2\n2 4 1 3 5\n5 1 2 3 4\n6\n2 3 4 5 6 1\n3 1 2 6 4 5\n6 1 2 3 4 5\n5 6 1 3 2 4\n4 3 6 5 2 1\n5 6 1 2 3 4\n4 5 6 1 2 3\n3 4 5 6 1 2\n1 2 3 4 5 6\n2 5 4 1 6 3\n3 2 5 4 1 6\n1 4 3 6 5 2"]
2 seconds
["1\n1 2 3 4 5 6 7\n2\n1 3 5 6 10\n4\n1 3 6 7 8 9"]
NoteIn the first test case, the number of good subsets is $$$1$$$. The only such subset is the set of arrays with indices $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$.In the second test case, the number of good subsets is $$$2$$$. They are $$$1$$$, $$$3$$$, $$$5$$$, $$$6$$$, $$$10$$$ or $$$2$$$, $$$4$$$, $$$7$$$, $$$8$$$, $$$9$$$.
Java 8
standard input
[ "2-sat", "brute force", "combinatorics", "constructive algorithms", "graph matchings", "graphs" ]
e284f33d82824afb23042e9dab0956ad
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$5 \leq n \leq 500$$$). Then $$$2n$$$ lines followed. The $$$i$$$-th of these lines contains $$$n$$$ integers, representing the $$$i$$$-th array. It is guaranteed, that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.
2,800
For each test case print two lines. In the first line, print the number of good subsets by modulo $$$998\,244\,353$$$. In the second line, print $$$n$$$ indices from $$$1$$$ to $$$2n$$$ — indices of the $$$n$$$ arrays that form a good subset (you can print them in any order). If there are several possible answers — print any of them.
standard output
PASSED
fe7e9b93512c1aa0b018acf30e5b3b38
train_108.jsonl
1626012300
Cirno has prepared $$$n$$$ arrays of length $$$n$$$ each. Each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. These arrays are special: for all $$$1 \leq i \leq n$$$, if we take the $$$i$$$-th element of each array and form another array of length $$$n$$$ with these elements, the resultant array is also a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. In the other words, if you put these $$$n$$$ arrays under each other to form a matrix with $$$n$$$ rows and $$$n$$$ columns, this matrix is a Latin square.Afterwards, Cirno added additional $$$n$$$ arrays, each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. For all $$$1 \leq i \leq n$$$, there exists at least one position $$$1 \leq k \leq n$$$, such that for the $$$i$$$-th array and the $$$(n + i)$$$-th array, the $$$k$$$-th element of both arrays is the same. Notice that the arrays indexed from $$$n + 1$$$ to $$$2n$$$ don't have to form a Latin square. Also, Cirno made sure that for all $$$2n$$$ arrays, no two arrays are completely equal, i. e. for all pair of indices $$$1 \leq i &lt; j \leq 2n$$$, there exists at least one position $$$1 \leq k \leq n$$$, such that the $$$k$$$-th elements of the $$$i$$$-th and $$$j$$$-th array are different.Finally, Cirno arbitrarily changed the order of $$$2n$$$ arrays.AquaMoon calls a subset of all $$$2n$$$ arrays of size $$$n$$$ good if these arrays from a Latin square.AquaMoon wants to know how many good subsets exist. Because this number may be particularly large, find it modulo $$$998\,244\,353$$$. Also, she wants to find any good subset. Can you help her?
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.Locale; import java.util.StringTokenizer; public class Solution implements Runnable { public static final int MOD = 998244353; private PrintStream out; private BufferedReader in; private StringTokenizer st; public void solve() throws IOException { long time0 = System.currentTimeMillis(); int t = nextInt(); for (int test = 1; test <= t; test++) { int n = nextInt(); int[][] a = new int[2 * n][n]; for (int i = 0; i < 2 * n; i++) { for (int j = 0; j < n; j++) { a[i][j] = nextInt() - 1; } } String answer = solve(n, a); out.println(answer); } System.err.println("time: " + (System.currentTimeMillis() - time0)); } private String solve(int n, int[][] a) { int[][] posnumcount = new int[n][n]; int[] posonescount = new int[n]; for (int i = 0; i < a.length; i++) { add(a[i], 1, posnumcount, posonescount); } boolean[] killed = new boolean[2 * n]; int answer = 1; int[] ansarr = new int[n]; for (int i = 0; i < n; i++) { int onepos = -1; for (int j = 0; j < n; j++) { if (posonescount[j] > 0) { onepos = j; break; } } int tokill = -1; if (onepos == -1) { answer = (answer * 2) % MOD; for (int j = 0; j < 2 * n; j++) { if (!killed[j]) { tokill = j; break; } } } else { for (int j = 0; j < 2 * n; j++) { if (!killed[j] && posnumcount[onepos][a[j][onepos]] == 1) { tokill = j; break; } } } ansarr[i] = tokill; for (int j = 0; j < 2 * n; j++) { if (!killed[j]) { for (int k = 0; k < n; k++) { if (a[j][k] == a[tokill][k]) { killed[j] = true; add(a[j], -1, posnumcount, posonescount); break; } } } } } StringBuilder buf = new StringBuilder(); buf.append(answer).append("\n"); for (int i = 0; i < n; i++) { buf.append(ansarr[i] + 1).append(" "); } return buf.toString(); } private void add(int[] ai, int add, int[][] posnumcount, int[] posonescount) { for (int j = 0; j < ai.length; j++) { if (posnumcount[j][ai[j]] == 1) { posonescount[j]--; } posnumcount[j][ai[j]] += add; if (posnumcount[j][ai[j]] == 1) { posonescount[j]++; } } } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } @Override public void run() { try { solve(); out.close(); } catch (Throwable e) { throw new RuntimeException(e); } } public Solution(String name) throws IOException { Locale.setDefault(Locale.US); if (name == null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintStream(new BufferedOutputStream(System.out)); } else { in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in"))); out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out"))); } st = new StringTokenizer(""); } public static void main(String[] args) throws IOException { new Thread(new Solution(null)).start(); } }
Java
["3\n7\n1 2 3 4 5 6 7\n2 3 4 5 6 7 1\n3 4 5 6 7 1 2\n4 5 6 7 1 2 3\n5 6 7 1 2 3 4\n6 7 1 2 3 4 5\n7 1 2 3 4 5 6\n1 2 3 4 5 7 6\n1 3 4 5 6 7 2\n1 4 5 6 7 3 2\n1 5 6 7 4 2 3\n1 6 7 5 2 3 4\n1 7 6 2 3 4 5\n1 7 2 3 4 5 6\n5\n4 5 1 2 3\n3 5 2 4 1\n1 2 3 4 5\n5 2 4 1 3\n3 4 5 1 2\n2 3 4 5 1\n1 3 5 2 4\n4 1 3 5 2\n2 4 1 3 5\n5 1 2 3 4\n6\n2 3 4 5 6 1\n3 1 2 6 4 5\n6 1 2 3 4 5\n5 6 1 3 2 4\n4 3 6 5 2 1\n5 6 1 2 3 4\n4 5 6 1 2 3\n3 4 5 6 1 2\n1 2 3 4 5 6\n2 5 4 1 6 3\n3 2 5 4 1 6\n1 4 3 6 5 2"]
2 seconds
["1\n1 2 3 4 5 6 7\n2\n1 3 5 6 10\n4\n1 3 6 7 8 9"]
NoteIn the first test case, the number of good subsets is $$$1$$$. The only such subset is the set of arrays with indices $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$.In the second test case, the number of good subsets is $$$2$$$. They are $$$1$$$, $$$3$$$, $$$5$$$, $$$6$$$, $$$10$$$ or $$$2$$$, $$$4$$$, $$$7$$$, $$$8$$$, $$$9$$$.
Java 8
standard input
[ "2-sat", "brute force", "combinatorics", "constructive algorithms", "graph matchings", "graphs" ]
e284f33d82824afb23042e9dab0956ad
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$5 \leq n \leq 500$$$). Then $$$2n$$$ lines followed. The $$$i$$$-th of these lines contains $$$n$$$ integers, representing the $$$i$$$-th array. It is guaranteed, that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.
2,800
For each test case print two lines. In the first line, print the number of good subsets by modulo $$$998\,244\,353$$$. In the second line, print $$$n$$$ indices from $$$1$$$ to $$$2n$$$ — indices of the $$$n$$$ arrays that form a good subset (you can print them in any order). If there are several possible answers — print any of them.
standard output
PASSED
7b1f73f06c34a14add8665082829e649
train_108.jsonl
1626012300
Cirno has prepared $$$n$$$ arrays of length $$$n$$$ each. Each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. These arrays are special: for all $$$1 \leq i \leq n$$$, if we take the $$$i$$$-th element of each array and form another array of length $$$n$$$ with these elements, the resultant array is also a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. In the other words, if you put these $$$n$$$ arrays under each other to form a matrix with $$$n$$$ rows and $$$n$$$ columns, this matrix is a Latin square.Afterwards, Cirno added additional $$$n$$$ arrays, each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. For all $$$1 \leq i \leq n$$$, there exists at least one position $$$1 \leq k \leq n$$$, such that for the $$$i$$$-th array and the $$$(n + i)$$$-th array, the $$$k$$$-th element of both arrays is the same. Notice that the arrays indexed from $$$n + 1$$$ to $$$2n$$$ don't have to form a Latin square. Also, Cirno made sure that for all $$$2n$$$ arrays, no two arrays are completely equal, i. e. for all pair of indices $$$1 \leq i &lt; j \leq 2n$$$, there exists at least one position $$$1 \leq k \leq n$$$, such that the $$$k$$$-th elements of the $$$i$$$-th and $$$j$$$-th array are different.Finally, Cirno arbitrarily changed the order of $$$2n$$$ arrays.AquaMoon calls a subset of all $$$2n$$$ arrays of size $$$n$$$ good if these arrays from a Latin square.AquaMoon wants to know how many good subsets exist. Because this number may be particularly large, find it modulo $$$998\,244\,353$$$. Also, she wants to find any good subset. Can you help her?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.util.Collection; import java.io.IOException; import java.lang.reflect.Field; import java.util.stream.Collectors; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.io.UncheckedIOException; import java.util.List; import java.util.stream.Stream; import java.io.Closeable; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); EAquaMoonAndPermutations solver = new EAquaMoonAndPermutations(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } } static class EAquaMoonAndPermutations { int mod = 998244353; public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.ri(); List<int[]> ps = new ArrayList<>(n * 2); for (int i = 0; i < n * 2; i++) { int[] p = new int[n + 1]; for (int j = 0; j < n; j++) { p[j] = in.ri(); } p[n] = i; ps.add(p); } List<Integer> res = new ArrayList<>(n); int[][] cnts = new int[n + 1][n + 1]; long ans = 1; while (!ps.isEmpty()) { SequenceUtils.fill(cnts, 0); for (int[] p : ps) { for (int i = 0; i < n; i++) { cnts[i][p[i]]++; } } int col = -1; int v = -1; for (int i = 0; i < n; i++) { for (int j = 1; j <= n; j++) { if (cnts[i][j] == 1) { col = i; v = j; } } } int[] pick = null; if (col != -1) { for (int i = 0; i < ps.size(); i++) { int[] p = ps.get(i); if (p[col] == v) { //find pick = p; } } } else { pick = ps.get(0); ans = ans * 2 % mod; } assert pick != null; res.add(pick[n]); int[] finalPick = pick; ps = ps.stream().filter(x -> { for (int i = 0; i < n; i++) { if (x[i] == finalPick[i]) { return false; } } return true; }).collect(Collectors.toList()); } res.sort(Comparator.naturalOrder()); out.println(ans); for (int x : res) { out.append(x + 1).append(' '); } out.println(); } } static class SequenceUtils { public static void fill(int[][] x, int val) { for (int[] v : x) { Arrays.fill(v, val); } } } static class FastInput { private final InputStream is; private StringBuilder defaultStringBuf = new StringBuilder(1 << 13); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public String next() { return readString(); } public int ri() { return readInt(); } public int readInt() { boolean rev = false; skipBlank(); if (next == '+' || next == '-') { rev = next == '-'; next = read(); } int val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } return rev ? val : -val; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private OutputStream writer; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); private static Field stringBuilderValueField; static { try { stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value"); stringBuilderValueField.setAccessible(true); } catch (Exception e) { stringBuilderValueField = null; } } public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(OutputStream writer) { this.writer = writer; } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(int c) { cache.append(c); afterWrite(); return this; } public FastOutput append(long c) { cache.append(c); afterWrite(); return this; } public FastOutput println(long c) { return append(c).println(); } public FastOutput println() { return append('\n'); } public FastOutput flush() { try { if (stringBuilderValueField != null) { try { byte[] value = (byte[]) stringBuilderValueField.get(cache); writer.write(value, 0, cache.length()); } catch (Exception e) { stringBuilderValueField = null; } } if (stringBuilderValueField == null) { //slow writer.write(cache.toString().getBytes(StandardCharsets.UTF_8)); // writer.append(cache); } writer.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { writer.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } }
Java
["3\n7\n1 2 3 4 5 6 7\n2 3 4 5 6 7 1\n3 4 5 6 7 1 2\n4 5 6 7 1 2 3\n5 6 7 1 2 3 4\n6 7 1 2 3 4 5\n7 1 2 3 4 5 6\n1 2 3 4 5 7 6\n1 3 4 5 6 7 2\n1 4 5 6 7 3 2\n1 5 6 7 4 2 3\n1 6 7 5 2 3 4\n1 7 6 2 3 4 5\n1 7 2 3 4 5 6\n5\n4 5 1 2 3\n3 5 2 4 1\n1 2 3 4 5\n5 2 4 1 3\n3 4 5 1 2\n2 3 4 5 1\n1 3 5 2 4\n4 1 3 5 2\n2 4 1 3 5\n5 1 2 3 4\n6\n2 3 4 5 6 1\n3 1 2 6 4 5\n6 1 2 3 4 5\n5 6 1 3 2 4\n4 3 6 5 2 1\n5 6 1 2 3 4\n4 5 6 1 2 3\n3 4 5 6 1 2\n1 2 3 4 5 6\n2 5 4 1 6 3\n3 2 5 4 1 6\n1 4 3 6 5 2"]
2 seconds
["1\n1 2 3 4 5 6 7\n2\n1 3 5 6 10\n4\n1 3 6 7 8 9"]
NoteIn the first test case, the number of good subsets is $$$1$$$. The only such subset is the set of arrays with indices $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$.In the second test case, the number of good subsets is $$$2$$$. They are $$$1$$$, $$$3$$$, $$$5$$$, $$$6$$$, $$$10$$$ or $$$2$$$, $$$4$$$, $$$7$$$, $$$8$$$, $$$9$$$.
Java 8
standard input
[ "2-sat", "brute force", "combinatorics", "constructive algorithms", "graph matchings", "graphs" ]
e284f33d82824afb23042e9dab0956ad
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$5 \leq n \leq 500$$$). Then $$$2n$$$ lines followed. The $$$i$$$-th of these lines contains $$$n$$$ integers, representing the $$$i$$$-th array. It is guaranteed, that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.
2,800
For each test case print two lines. In the first line, print the number of good subsets by modulo $$$998\,244\,353$$$. In the second line, print $$$n$$$ indices from $$$1$$$ to $$$2n$$$ — indices of the $$$n$$$ arrays that form a good subset (you can print them in any order). If there are several possible answers — print any of them.
standard output
PASSED
7ae69df51c65c4be87aa9dcb4c5a4da4
train_108.jsonl
1626012300
Cirno has prepared $$$n$$$ arrays of length $$$n$$$ each. Each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. These arrays are special: for all $$$1 \leq i \leq n$$$, if we take the $$$i$$$-th element of each array and form another array of length $$$n$$$ with these elements, the resultant array is also a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. In the other words, if you put these $$$n$$$ arrays under each other to form a matrix with $$$n$$$ rows and $$$n$$$ columns, this matrix is a Latin square.Afterwards, Cirno added additional $$$n$$$ arrays, each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. For all $$$1 \leq i \leq n$$$, there exists at least one position $$$1 \leq k \leq n$$$, such that for the $$$i$$$-th array and the $$$(n + i)$$$-th array, the $$$k$$$-th element of both arrays is the same. Notice that the arrays indexed from $$$n + 1$$$ to $$$2n$$$ don't have to form a Latin square. Also, Cirno made sure that for all $$$2n$$$ arrays, no two arrays are completely equal, i. e. for all pair of indices $$$1 \leq i &lt; j \leq 2n$$$, there exists at least one position $$$1 \leq k \leq n$$$, such that the $$$k$$$-th elements of the $$$i$$$-th and $$$j$$$-th array are different.Finally, Cirno arbitrarily changed the order of $$$2n$$$ arrays.AquaMoon calls a subset of all $$$2n$$$ arrays of size $$$n$$$ good if these arrays from a Latin square.AquaMoon wants to know how many good subsets exist. Because this number may be particularly large, find it modulo $$$998\,244\,353$$$. Also, she wants to find any good subset. Can you help her?
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class C { FastScanner in; PrintWriter out; boolean systemIO = true; public static void quickSort(int[] a, int from, int to) { if (to - from <= 1) { return; } int i = from; int j = to - 1; int x = a[from + (new Random()).nextInt(to - from)]; while (i <= j) { while (a[i] < x) { i++; } while (a[j] > x) { j--; } if (i <= j) { int t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; } } quickSort(a, from, j + 1); quickSort(a, j + 1, to); } public long gcd(long x, long y) { if (y == 0) { return x; } if (x == 0) { return y; } return gcd(y, x % y); } public boolean prime(long x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) { return false; } } return true; } public long pow(long x, long p) { if (p == 0) { return 1; } long t = pow(x, p / 2); t *= t; t %= mod; if (p % 2 == 1) { t *= x; t %= mod; } return t; } public 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; } @Override 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; } } public class Fenvik2D { long[][] t; int n, m; public Fenvik2D(int n, int m) { t = new long[n][m]; this.n = n; this.m = m; } public int sum(int x1, int y1, int x2, int y2) { return sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) + sum(x1 - 1, y1 - 1); } public int sum(int x, int y) { int result = 0; for (int i = x; i >= 0; i = (i & (i + 1)) - 1) { for (int j = y; j >= 0; j = (j & (j + 1)) - 1) { result += t[i][j]; } } return result; } public void add(int x, int y, int delta) { for (int i = x; i < n; i = (i | (i + 1))) { for (int j = y; j < m; j = (j | (j + 1))) { t[i][j] += delta; } } } } public int[] prefF(int[] a) { int[] p = new int[a.length]; for (int i = 1; i < p.length; i++) { int j = p[i - 1]; while (j > 0) { if (a[j] == a[i]) { break; } if (j == 0) { break; } j = p[j - 1]; } if (a[i] == a[j]) { p[i] = j + 1; } } return p; } public class Tree { long[] sum; long[] min; int[] alive; int pow; long inf = Long.MAX_VALUE / 2; public Tree(long[] a, long[] b) { pow = 1; while (pow < a.length) { pow *= 2; } sum = new long[2 * pow]; min = new long[2 * pow]; for (int i = 0; i < min.length; i++) { min[i] = inf; } alive = new int[2 * pow]; for (int i = 0; i < b.length; i++) { sum[pow + i] = a[i]; min[pow + i] = a[i] + b[i]; alive[pow + i] = 1; } for (int i = pow - 1; i > 0; i--) { merge(i); } } public void merge(int v) { sum[v] = sum[2 * v] + sum[2 * v + 1]; min[v] = inf; if (min[2 * v] < min[v]) { min[v] = min[2 * v]; } if (min[2 * v + 1] + sum[2 * v] < min[v]) { min[v] = min[2 * v + 1] + sum[2 * v]; } alive[v] = alive[2 * v] + alive[2 * v + 1]; } public void add(long a, long b, int c) { add(1, 0, pow, a, b, c - 1); } private void add(int v, int l, int r, long a, long b, int c) { if (v >= pow) { sum[v] = a; min[v] = a + b; alive[v] = 1; return; } int m = (l + r) / 2; if (c < m) { add(2 * v, l, m, a, b, c); } else { add(2 * v + 1, m, r, a, b, c); } merge(v); } public int update(long x) { return update(1, 0, pow, x); } private int update(int v, int l, int r, long x) { if (v >= pow) { if (x > min[v]) { sum[v] = 0; min[v] = inf; alive[v] = 0; return 1; } if (alive[v] == 1 && x >= sum[v]) { return 1; } return 0; } int m = (l + r) / 2; if (sum[2 * v] > x) { int ans = update(2 * v, l, m, x); merge(v); return ans; } int ans = alive[2 * v] + update(2 * v + 1, m, r, x - sum[2 * v]); if (x > min[2 * v]) { update(2 * v, l, m, x); } merge(v); return ans; } } public class MyStack { int[] st; int sz; public MyStack(int max) { st = new int[max]; sz = 0; } public int size() { return sz; } public boolean isEmpty() { return sz == 0; } public int peek() { return st[sz - 1]; } public int pop() { return st[--sz]; } public void add(int x) { st[sz++] = x; } public void clear() { sz = 0; } } public long[][] mult(long[][] a, long[][] b) { long[][] c = new long[a.length][b[0].length]; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { for (int k = 0; k < b.length; k++) { c[i][j] += a[i][k] * b[k][j]; c[i][j] %= mod; } } } return c; } int mod = 998244353; public long[][] pow(long p, long[][] m) { if (p == 0) { long[][] matrix = new long[m.length][m.length]; for (int i = 0; i < matrix.length; i++) { matrix[i][i] = 1; } return matrix; } if (p % 2 == 1) { return mult(m, pow(p - 1, m)); } long[][] cur = pow(p / 2, m); return mult(cur, cur); } public void add(HashMap<Integer, Integer> map, int x) { if (map.containsKey(x)) { map.put(x, map.get(x) + 1); } else { map.put(x, 1); } } public class Point implements Comparable<Point> { long x; long y; int n; public Point(long x, long y, int n) { this.x = x; this.y = y; this.n = n; } public void norm() { long gcd = gcd(Math.abs(x), Math.abs(y)); x /= gcd; y /= gcd; } public int quater() { if (x > 0 && y >= 0) { return 0; } if (x <= 0 && y > 0) { return 1; } if (x < 0) { return 2; } return 3; } public long mod2() { return x * x + y * y; } @Override public int compareTo(Point o) { if (quater() != o.quater()) { return quater() - o.quater(); } int s = sign(cp(o, this)); if (s != 0) { return s; } return sign(mod2() - o.mod2()); } } public long cp(Point a, Point b) { return a.x * b.y - a.y * b.x; } public int sign(long x) { if (x > 0) { return 1; } if (x < 0) { return -1; } return 0; } public class Vector { double x; double y; public Vector(double x, double y) { this.x = x; this.y = y; } public double norm() { return Math.sqrt(norm2()); } public double norm2() { return x * x + y * y; } public String toString() { return x + " " + y; } public Vector negative() { return new Vector(-x, -y); } public Vector add(Vector v) { return new Vector(x + v.x, y + v.y); } public Vector subtract(Vector v) { return new Vector(x - v.x, y - v.y); } } public double dp(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } public double cp(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } public class Polygon { Vector[] v; double s; public Polygon(Vector[] v) { this.v = v; s = Math.abs(s()); } public double s() { double ans = 0; for (int i = 1; i < v.length - 1; i++) { Vector a = v[i].subtract(v[0]); Vector b = v[i + 1].subtract(v[0]); ans += cp(a, b); } return ans / 2; } public boolean intersect(Vector o, double r) { for (int i = 0; i < v.length; i++) { if (intersectSeg(v[i], v[(i + 1) % v.length], o, r)) { return true; } } boolean inside = false; for (int i = 0; i < v.length; i++) { inside ^= intersectRay(v[i], v[(i + 1) % v.length], o); } if (inside) { System.err.println(o + " " + s); return true; } return false; } } public double sq(double x) { return x * x; } public boolean intersectSeg(Vector a, Vector b, Vector o, double r) { Vector ao = o.subtract(a); Vector bo = o.subtract(b); if (ao.norm2() <= r * r * (1 + eps) || bo.norm2() <= r * r * (1 + eps)) { return true; } Vector ab = b.subtract(a); if (sq(Math.abs(cp(ao, bo))) > ab.norm2() * r * r * (1 + eps)) { return false; } if (dp(ao, ab) < -eps || dp(bo, ab) > eps) { return false; } return true; } public boolean intersectRay(Vector a, Vector b, Vector o) { if (a.y == b.y) { return false; } if (o.y == Math.max(a.y, b.y) && o.x < Math.min(a.x, b.x)) { return true; } if (o.y == Math.min(a.y, b.y)) { return false; } if ((o.y - a.y) * (o.y - b.y) < 0) { if (a.y < b.y) { if (cp(b.subtract(a), o.subtract(a)) > 0) { return true; } } else { if (cp(b.subtract(a), o.subtract(a)) < 0) { return true; } } } return false; } double eps = 0; Random random = new Random(566); public class SegmentTreeSet { int pow; long[] sum; long[] max; long[] min; long[] delta; boolean[] flag; public SegmentTreeSet(long[] a) { pow = 1; while (pow < a.length) { pow *= 2; } flag = new boolean[2 * pow]; sum = new long[2 * pow]; max = new long[2 * pow]; min = new long[2 * pow]; delta = new long[2 * pow]; for (int i = 0; i < a.length; i++) { sum[pow + i] = a[i]; max[pow + i] = a[i]; min[pow + i] = a[i]; } for (int i = pow - 1; i > 0; i--) { sum[i] = sum[2 * i] + sum[2 * i + 1]; max[i] = max[2 * i]; min[i] = min[2 * i + 1]; } } public int get(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if (l >= tr || r <= tl) { return 0; } if (l <= tl && r >= tr) { if (money >= sum[v]) { money -= sum[v]; return tr - tl; } if (money < min[v]) { return 0; } } int tm = (tl + tr) / 2; return get(2 * v, tl, tm, l, r) + get(2 * v + 1, tm, tr, l, r); } public void set(int v, int tl, int tr, int l, int r, long x) { push(v, tl, tr); if (l >= tr || r <= tl) { return; } if (l <= tl && r >= tr) { if (x >= max[v]) { delta[v] = x; flag[v] = true; push(v, tl, tr); } else if (v < pow) { int tm = (tl + tr) / 2; push(2 * v + 1, tm, tr); if (x >= max[2 * v + 1]) { delta[2 * v + 1] = x; flag[2 * v + 1] = true; push(2 * v + 1, tm, tr); set(2 * v, tl, tm, l, r, x); } else { push(2 * v, tl, tm); set(2 * v + 1, tm, tr, l, r, x); } sum[v] = (sum[2 * v] + sum[2 * v + 1]); max[v] = max[2 * v]; min[v] = min[2 * v + 1]; } return; } int tm = (tl + tr) / 2; set(2 * v, tl, tm, l, r, x); set(2 * v + 1, tm, tr, l, r, x); sum[v] = (sum[2 * v] + sum[2 * v + 1]); max[v] = max[2 * v]; min[v] = min[2 * v + 1]; } public void push(int v, int tl, int tr) { if (flag[v]) { if (v < pow) { flag[2 * v] = true; flag[2 * v + 1] = true; delta[2 * v] = delta[v]; delta[2 * v + 1] = delta[v]; } flag[v] = false; sum[v] = delta[v] * (tr - tl); max[v] = delta[v]; min[v] = delta[v]; } } public int f(int a, int b) { return a + b; } } long money = 0; public long[] diofant(long a, long b) { long[] ans = new long[2]; if (b == 0) { ans[0] = 1; ans[1] = 0; return ans; } long div = a / b; long mod = a % b; long[] get = diofant(b, mod); ans[0] = get[1]; ans[1] = get[0] - (get[1] * div); return ans; } public class Arr implements Comparable<Arr> { int[] a; public Arr(int[] a) { this.a = a; } @Override public int compareTo(Arr o) { for (int i = 0; i < comp.length; i++) { if (a[comp[i]] != o.a[comp[i]]) { return a[comp[i]] - o.a[comp[i]]; } } return 0; } } int[] comp; public void dfs(int v) { boolean flag = false; if (!used[v]) { used[v] = true; flag = true; ans.add(v); } for (int x = 0; x < sz; x++) { if (!used[x] && g[v][x]) { used[x] = flag; dfs(x); } } } public int components() { int c = 0; for (int i = 0; i < sz; i++) { if (!used[i]) { dfs(i); c++; } } return c; } int max = 500; boolean[][] g = new boolean[2 * max][2 * max]; boolean[] used = new boolean[2 * max]; int[][] value = new int[max][max]; int sz = -1; ArrayList<Integer> ans = new ArrayList<>(); public void solve() { int[] pow = new int[2 * max + 1]; pow[0] = 1; for (int i = 0; i < pow.length - 1; i++) { pow[i + 1] = pow[i] * 2 % 998244353; } for (int qwerty = in.nextInt(); qwerty > 0; qwerty--) { // for (int qwerty = 1; qwerty > 0; qwerty--) { ans.clear(); int n = in.nextInt(); // int n = 500; int[][] a = new int[2 * n][n]; sz = 2 * n; for (int i = 0; i < sz; i++) { used[i] = false; for (int j = 0; j < n; j++) { a[i][j] = in.nextInt() - 1; // a[i][j] = (i + j) % n; } } boolean flag = false; for (int i = 0; i < sz; ++i) { for (int j = i + 1; j < sz; ++j) { flag = false; for (int k = 0; k < n; ++k) { if (a[i][k] == a[j][k]) { flag = true; break; } } g[i][j] = flag; g[j][i] = flag; } } for (int col = 0; col < n; ++col) { for (int v = 0; v < n; ++v) { value[col][v] = 0; } for (int i = 0; i < sz; ++i) { value[col][a[i][col]]++; } } w : while (true) { for (int c = 0; c < n; ++c) { for (int v = 0; v < n; ++v) { if (value[c][v] == 1) { for (int r = 0; r < sz; ++r) { if (!used[r] && a[r][c] == v) { used[r] = true; for (int x = 0; x < n; ++x) { value[x][a[r][x]]--; } ans.add(r); for (int i = 0; i < sz; ++i) { if (!used[i] && g[r][i]) { used[i] = true; for (int x = 0; x < n; ++x) { value[x][a[i][x]]--; } } } continue w; } } } } } break; } out.println(pow[components()]); for (int i : ans) { out.print(i + 1 + " "); } out.println(); } } public void run() { try { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } 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()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { long time = System.currentTimeMillis(); new C().run(); System.err.println(System.currentTimeMillis() - time); } }
Java
["3\n7\n1 2 3 4 5 6 7\n2 3 4 5 6 7 1\n3 4 5 6 7 1 2\n4 5 6 7 1 2 3\n5 6 7 1 2 3 4\n6 7 1 2 3 4 5\n7 1 2 3 4 5 6\n1 2 3 4 5 7 6\n1 3 4 5 6 7 2\n1 4 5 6 7 3 2\n1 5 6 7 4 2 3\n1 6 7 5 2 3 4\n1 7 6 2 3 4 5\n1 7 2 3 4 5 6\n5\n4 5 1 2 3\n3 5 2 4 1\n1 2 3 4 5\n5 2 4 1 3\n3 4 5 1 2\n2 3 4 5 1\n1 3 5 2 4\n4 1 3 5 2\n2 4 1 3 5\n5 1 2 3 4\n6\n2 3 4 5 6 1\n3 1 2 6 4 5\n6 1 2 3 4 5\n5 6 1 3 2 4\n4 3 6 5 2 1\n5 6 1 2 3 4\n4 5 6 1 2 3\n3 4 5 6 1 2\n1 2 3 4 5 6\n2 5 4 1 6 3\n3 2 5 4 1 6\n1 4 3 6 5 2"]
2 seconds
["1\n1 2 3 4 5 6 7\n2\n1 3 5 6 10\n4\n1 3 6 7 8 9"]
NoteIn the first test case, the number of good subsets is $$$1$$$. The only such subset is the set of arrays with indices $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$.In the second test case, the number of good subsets is $$$2$$$. They are $$$1$$$, $$$3$$$, $$$5$$$, $$$6$$$, $$$10$$$ or $$$2$$$, $$$4$$$, $$$7$$$, $$$8$$$, $$$9$$$.
Java 8
standard input
[ "2-sat", "brute force", "combinatorics", "constructive algorithms", "graph matchings", "graphs" ]
e284f33d82824afb23042e9dab0956ad
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$5 \leq n \leq 500$$$). Then $$$2n$$$ lines followed. The $$$i$$$-th of these lines contains $$$n$$$ integers, representing the $$$i$$$-th array. It is guaranteed, that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.
2,800
For each test case print two lines. In the first line, print the number of good subsets by modulo $$$998\,244\,353$$$. In the second line, print $$$n$$$ indices from $$$1$$$ to $$$2n$$$ — indices of the $$$n$$$ arrays that form a good subset (you can print them in any order). If there are several possible answers — print any of them.
standard output
PASSED
301d8d5ab1fb972e642b60bcfa65c3f9
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ boolean k = s.charAt(i)==s.charAt(i-1); if(k){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } solve.append(ans).append(" ").append(Math.max(ans2, 1)).append("\n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
9223f81aacd412de798c8f1b950d6ebe
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ boolean k = s.charAt(i)==s.charAt(i-1); if(k){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } solve.append(ans).append(" ").append(Math.max(ans2, 1)).append(" \n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
c753bda7720796aa741889e6c84a832b
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ boolean k = s.charAt(i)==s.charAt(i-1); if(k){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans).append(" ").append(ans2).append(" \n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
c10a95906f329efe9621504ba11e802e
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ boolean k = s.charAt(i)==s.charAt(i-1); if(k){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans).append(" ").append(ans2).append(" \n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
a8222dbf96dcca7a4072ec71dc39e024
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ boolean k = s.charAt(i)==s.charAt(i-1); if(k){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans).append(" ").append(ans2).append("\n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
b2d225956bc6f865ec174ce3d5ec0af4
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); boolean o = true; for(int i=1; i<n; i++){ boolean k = s.charAt(i)==s.charAt(i-1); if(k){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); o=false; } i++; continue; } i++; ans++; } if(o)ans2++; solve.append(ans).append(" ").append(ans2).append(" ").append("\n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
f0ee4b7ba8f0ff72a67f7bdbd363b09f
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i++){ boolean k = s.charAt(i)==s.charAt(i-1); if(k){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } i++; continue; } i++; ans++; } if(ans2==0)ans2++; solve.append(ans).append(" ").append(ans2).append(" ").append("\n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
872410c0ade551eef666659a3bdc1611
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ boolean k = s.charAt(i)==s.charAt(i-1); if(k){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans).append(" ").append(ans2).append(" ").append("\n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
abbdd71176025f5e29cbe221397c08d9
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans).append(" ").append(ans2).append(" ").append("\n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
d020e2110b0d0f907c37f63d7f65b6c2
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){ FastReader sc = new FastReader(); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans).append(" ").append(ans2).append(" ").append("\n"); } System.out.print(solve); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
7cfed7b4721a1020ba96c798b98c16b1
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans).append(" ").append(ans2).append(" ").append("\n"); } pw.print(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
12fd61f8fb51e43d7e3cc7b71978cb36
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 C{ public static void main(String[] args) { FastReader sc = new FastReader(); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); out:while(t-- >0) { int n = sc.nextInt(); char[] ch = sc.nextLine().toCharArray(); int l =-1; int seg = 0; int flips = 0; for(int i =0;i<n;i+=2) { if(ch[i]!=ch[i+1]) { flips++; } else { if(ch[i]-'0'!=l) { seg++; l = ch[i]-'0'; } } } sb.append(flips).append(" ").append(Math.max(seg, 1)).append("\n"); } System.out.println(sb); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
4a38bf4e1aee58a470ac59721f667782
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; pw.print(ans+" "+ans2+"\n"); } 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
14bcb0a3b9b939ca163e21952537104e
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){ FastReader sc = new FastReader(); StringBuilder solve = new StringBuilder(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); out:while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans+" "+ans2+"\n"); } pw.print(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
d508525530d7d379ab39e93c77bcf8e2
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){ FastReader sc = new FastReader(); StringBuilder solve = new StringBuilder(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } if(ans2==0)ans2++; solve.append(ans+" "+ans2+"\n"); } pw.print(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
240a29e6f204420cbf6c9d7cbbbd2c7c
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){ FastReader sc = new FastReader(); StringBuilder solve = new StringBuilder(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } solve.append(ans+" "+Math.max(ans2, 1)+"\n"); } pw.print(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
cfff3d8dbaec0f7230a12a1a566a379b
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){ FastReader sc = new FastReader(); StringBuilder solve = new StringBuilder(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } solve.append(ans+" "+Math.max(ans2, 1)+"\n"); } System.out.print(solve); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
cac8a45cff616b832184822a777cb764
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){ FastReader sc = new FastReader(); StringBuilder solve = new StringBuilder(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.nextLine(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } solve.append(ans+" "+Math.max(ans2, 1)+"\n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
d49d9af201571fa0c9e4e629eb72a579
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){ FastReader sc = new FastReader(); StringBuilder solve = new StringBuilder(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } solve.append(ans+" "+Math.max(ans2, 1)+"\n"); } pw.println(solve); 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
f4a92b2fc6cdd8458b949458afbf1729
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){ FastReader sc = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } pw.println(ans+" "+Math.max(ans2, 1)); } 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
1296dd3ccef7af8890a12bdf0d8fe3b2
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){ FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } System.out.println(ans+" "+Math.max(ans2, 1)); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
31dd51a5f60d7c091fe1e4a038538c78
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(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } System.out.println(ans+" "+Math.max(ans2, 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
1f4177b3b20a93872a3d70c44a780e68
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{ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(), ans = 0, ans2=0; char prev = 'w'; String s = sc.next(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } System.out.println(ans+" "+Math.max(ans2, 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
0f259f275afc08bc7066a88773e1761b
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{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long t = Long.parseLong(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); int ans = 0; int ans2 =0; char prev = 'w'; String s = br.readLine(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } System.out.println(ans+" "+Math.max(ans2, 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
360c95e4951e04c03de6370cd35526ce
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{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); int ans = 0; int ans2 =0; char prev = 'w'; String s = br.readLine(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } pw.println(ans+" "+Math.max(ans2, 1)); } 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 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
3b5ace4db8756e1d964a08251b3f1f5e
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{ 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 ans = 0; int ans2 =0; char prev = 'w'; String s = br.readLine(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } continue; } ans++; } pw.println(ans+" "+Math.max(ans2, 1)); } 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 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
331b7093e53316d42da28b0f6f8fbe04
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{ 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 ans = 0; int ans2 =0; char prev = 'w'; String s = br.readLine(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if(prev!=s.charAt(i)){ ans2++; prev=s.charAt(i); } } else{ ans++; } } pw.println(ans+" "+Math.max(ans2, 1)); } 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 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
4ffb3724c7495f45c5e6786a6da74cac
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{ 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()); ArrayDeque<Character> con = new ArrayDeque<>(); int ans = 0; String s = br.readLine(); for(int i=1; i<n; i+=2){ if(s.charAt(i)==s.charAt(i-1)){ if (con.size()==0||con.getFirst()!=s.charAt(i)){ con.addFirst(s.charAt(i)); } } else{ ans++; } } pw.println(ans+" "+Math.max(con.size(), 1)); } 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 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
e8b0d6c25e72e895286ffe83786d06ac
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{ 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]; ArrayDeque<Integer> con = new ArrayDeque<>(); int ans = 0; String s = br.readLine(); for(int i=1; i<n; i+=2){ arr[i-1] = Integer.parseInt(String.valueOf(s.charAt(i-1))); arr[i] = Integer.parseInt(String.valueOf(s.charAt(i))); if(arr[i]==arr[i-1]){ if (con.size()==0||con.getFirst()!=arr[i]){ con.addFirst(arr[i]); } } else{ ans++; } } pw.println(ans+" "+Math.max(con.size(), 1)); } 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 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
fbbb8daaf922c1667d559bd6d9a611ac
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{ 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<>(); ArrayDeque<Integer> con = new ArrayDeque<>(); 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); } } if(i%2==1&&arr[i]==arr[i-1]&&(con.size()==0||con.getFirst()!=arr[i])){ con.addFirst(arr[i]); } } 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)+" "+Math.max(con.size(), 1)); } 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 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
f9ea23c021c25216a387d77b7087d1cb
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{ 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.print(ans+times); ArrayDeque<Integer> con = new ArrayDeque<>(); for(int i=1; i<n; i+=2){ if(arr[i]==arr[i-1]&&(con.size()==0||con.getFirst()!=arr[i])){ con.addFirst(arr[i]); } } pw.println(" " + Math.max(con.size(), 1)); } 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 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
3fea85d280501af03a1908e55a869b34
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 B1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while(test -- > 0) { int n = sc.nextInt(); String s = sc.next(); long[] cut = calc(s,n); System.out.println(cut[0] +" " +cut[1]); } } static long []calc(String s, int n) { long ans = 0; long cut = 0; char p = '2'; for(int i=0; i<n; i+=2) { if (s.charAt(i) != s.charAt(i+1)) { ans++; } else { if (s.charAt(i) != p) { cut++; p = s.charAt(i); } } } return new long[]{ans, Math.max(cut, 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
5683078e4cffef77af26889ddff4880b
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 mod = 1000000007; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while( t-- > 0) { int n = sc.nextInt(); char arr[] = sc.next().toCharArray(); int ans = 0; for( int i = 0 ;i < n ;i+=2) { if( arr[i] != arr[i+1]) { ans++; } } int res = 0; ArrayList<Integer> a = new ArrayList<>(); for( int i = 0 ;i < n ;i+=2) { if( arr[i] == arr[i+1]) { if( arr[i] == '1') { a.add(1); } else { a.add(2); } } else { a.add(-1); } } int s = a.size(); int ar[] = new int[s]; for( int i = 0; i < s ;i++) { ar[i] = a.get(i); } for( int i = 0 ;i < s; i++) { int toad = 1; while( i < s && ar[i] == -1 ) { i++; } while(i+1 < s && (ar[i+1] == -1 || ar[i+1] == ar[i])) { ar[i+1] = ar[i]; i++; } res+=toad; } out.println(ans + " " + res); } out.flush(); } /* * time for a change */ public static boolean ifpowof2(long n ) { return ((n&(n-1)) == 0); } static boolean isprime(long x ) { if( x== 2) { return true; } if( x%2 == 0) { return false; } for( long i = 3 ;i*i <= x ;i+=2) { if( x%i == 0) { return false; } } return true; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int)n + 1]; for (int i = 0; i <= n; i++) { prime[i] = true; } for (long p = 2; p * p <= n; p++) { if (prime[(int)p] == true) { for (long i = p * p; i <= n; i += p) prime[(int)i] = false; } } return prime; } public static int[] nextLargerElement(int[] arr, int n) { Stack<Integer> stack = new Stack<>(); int rtrn[] = new int[n]; rtrn[n-1] = -1; stack.push( n-1); for( int i = n-2 ;i >= 0 ; i--){ int temp = arr[i]; int lol = -1; while( !stack.isEmpty() && arr[stack.peek()] <= temp){ if(arr[stack.peek()] == temp ) { lol = stack.peek(); } stack.pop(); } if( stack.isEmpty()){ if( lol != -1) { rtrn[i] = lol; } else { rtrn[i] = -1; } } else{ rtrn[i] = stack.peek(); } stack.push( i); } return rtrn; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static void mySort(long[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); long loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static long rightmostsetbit(long n) { return n&-n; } static long leftmostsetbit(long n) { long k = (long)(Math.log(n) / Math.log(2)); return k; } static HashMap<Long,Long> primefactor( long n){ HashMap<Long ,Long> hm = new HashMap<>(); long temp = 0; while( n%2 == 0) { temp++; n/=2; } if( temp!= 0) { hm.put( 2L, temp); } long c = (long)Math.sqrt(n); for( long i = 3 ; i <= c ; i+=2) { temp = 0; while( n% i == 0) { temp++; n/=i; } if( temp!= 0) { hm.put( i, temp); } } if( n!= 1) { hm.put( n , 1L); } return hm; } static ArrayList<Long> allfactors(long abs) { HashMap<Long,Integer> hm = new HashMap<>(); ArrayList<Long> rtrn = new ArrayList<>(); for( long i = 2 ;i*i <= abs; i++) { if( abs% i == 0) { hm.put( i , 0); hm.put(abs/i, 0); } } for( long x : hm.keySet()) { rtrn.add(x); } if( abs != 0) { rtrn.add(abs); } return rtrn; } public static int[][] prefixsum( int n , int m , int arr[][] ){ int prefixsum[][] = new int[n+1][m+1]; for( int i = 1 ;i <= n ;i++) { for( int j = 1 ; j<= m ; j++) { int toadd = 0; if( arr[i-1][j-1] == 1) { toadd = 1; } prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1]; } } return prefixsum; } 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
457b612a2537a31328d58d0235cc7959
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 mod = 1000000007; static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t = sc.nextInt(); while( t-- > 0) { int n = sc.nextInt(); char arr[] = sc.next().toCharArray(); int ans = 0; for( int i = 0 ;i < n ;i+=2) { if( arr[i] != arr[i+1]) { ans++; } } int res = 0; ArrayList<Integer> a = new ArrayList<>(); for( int i = 0 ;i < n ;i+=2) { if( arr[i] == arr[i+1]) { if( arr[i] == '1') { a.add(1); } else { a.add(2); } } else { a.add(-1); } } int s = a.size(); int ar[] = new int[s]; for( int i = 0; i < s ;i++) { ar[i] = a.get(i); } for( int i = 0 ;i < s; i++) { int toad = 1; int check = -1; while( i < s && ar[i] == -1 ) { i++; check++; } while(i+1 < s && (ar[i+1] == -1 || ar[i+1] == ar[i])) { ar[i+1] = ar[i]; i++; } res+=toad; } out.println(ans + " " + res); } out.flush(); } /* * time for a change */ public static boolean ifpowof2(long n ) { return ((n&(n-1)) == 0); } static boolean isprime(long x ) { if( x== 2) { return true; } if( x%2 == 0) { return false; } for( long i = 3 ;i*i <= x ;i+=2) { if( x%i == 0) { return false; } } return true; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int)n + 1]; for (int i = 0; i <= n; i++) { prime[i] = true; } for (long p = 2; p * p <= n; p++) { if (prime[(int)p] == true) { for (long i = p * p; i <= n; i += p) prime[(int)i] = false; } } return prime; } public static int[] nextLargerElement(int[] arr, int n) { Stack<Integer> stack = new Stack<>(); int rtrn[] = new int[n]; rtrn[n-1] = -1; stack.push( n-1); for( int i = n-2 ;i >= 0 ; i--){ int temp = arr[i]; int lol = -1; while( !stack.isEmpty() && arr[stack.peek()] <= temp){ if(arr[stack.peek()] == temp ) { lol = stack.peek(); } stack.pop(); } if( stack.isEmpty()){ if( lol != -1) { rtrn[i] = lol; } else { rtrn[i] = -1; } } else{ rtrn[i] = stack.peek(); } stack.push( i); } return rtrn; } static void mysort(int[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); int loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static void mySort(long[] arr) { for(int i=0;i<arr.length;i++) { int rand = (int) (Math.random() * arr.length); long loc = arr[rand]; arr[rand] = arr[i]; arr[i] = loc; } Arrays.sort(arr); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static long rightmostsetbit(long n) { return n&-n; } static long leftmostsetbit(long n) { long k = (long)(Math.log(n) / Math.log(2)); return k; } static HashMap<Long,Long> primefactor( long n){ HashMap<Long ,Long> hm = new HashMap<>(); long temp = 0; while( n%2 == 0) { temp++; n/=2; } if( temp!= 0) { hm.put( 2L, temp); } long c = (long)Math.sqrt(n); for( long i = 3 ; i <= c ; i+=2) { temp = 0; while( n% i == 0) { temp++; n/=i; } if( temp!= 0) { hm.put( i, temp); } } if( n!= 1) { hm.put( n , 1L); } return hm; } static ArrayList<Long> allfactors(long abs) { HashMap<Long,Integer> hm = new HashMap<>(); ArrayList<Long> rtrn = new ArrayList<>(); for( long i = 2 ;i*i <= abs; i++) { if( abs% i == 0) { hm.put( i , 0); hm.put(abs/i, 0); } } for( long x : hm.keySet()) { rtrn.add(x); } if( abs != 0) { rtrn.add(abs); } return rtrn; } public static int[][] prefixsum( int n , int m , int arr[][] ){ int prefixsum[][] = new int[n+1][m+1]; for( int i = 1 ;i <= n ;i++) { for( int j = 1 ; j<= m ; j++) { int toadd = 0; if( arr[i-1][j-1] == 1) { toadd = 1; } prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1]; } } return prefixsum; } 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
f68f55996b869ac1a4e6c4cd46577cc6
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
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n; static char[] a; static int min, segments; public static void main(String[] args) throws IOException { t = in.iscan(); while (t-- > 0) { n = in.iscan(); a = in.sscan().toCharArray(); min = 0; segments = 0; Queue<Integer> q = new LinkedList<Integer>(); int prev = -1; // prev = 0 -> previous continuous was 00 // prev = 1 -> previous continuous was 11 for (int i = 0; i < n; i += 2) { if (a[i] == a[i+1]) { int newprev = a[i] == '0' ? 0 : 1; if (prev != newprev) { segments++; } prev = newprev; } else { q.add(i); } if (prev != -1) { while (!q.isEmpty()) { int nxt = q.poll(); a[nxt] = prev == 0 ? '0' : '1'; a[nxt+1] = prev == 0 ? '0' : '1'; min++; } } } if (prev != -1) { out.println(min + " " + segments); } else { out.println((n/2) + " " + 1); } } out.close(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
Java
["5\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
ba5f62870e5df2fe753fd8b3ca62613b
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class CF789VC { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } static void sort(int [] a) { ArrayList<Integer> arr = new ArrayList<Integer>(); for (int i:a) { arr.add(i); } Collections.sort(arr); int len = arr.size(); for(int i=0;i<len;++i) a[i] = arr.get(i); } public static void main(String[] args) { // TODO Auto-generated method stub PrintWriter out = new PrintWriter(System.out); FastReader fs = new FastReader(); int t = fs.nextInt(); for(int cs=0;cs<t;++cs) { int n = fs.nextInt(); String input = fs.nextLine(); int index=0; Vector<Integer> tracker = new Vector<Integer>(); //Vector<Character> inp = new Vector<Character>(); Vector<Integer> elemSize = new Vector<Integer>(); Vector<Integer> fin = new Vector<Integer>(); for(int i=0;i<n;++i) { int st=i; while(st<n && (input.charAt(st) == input.charAt(i))) {// inp.add(input.charAt(st)); st++; } elemSize.add(st-i); if(((st-i)%2)==1) { tracker.add(index); } index++; i=st-1; } int len = tracker.size(); int answer=0; for(int i=0;i<len-1;i+=2) { answer+=(tracker.get(i+1)-tracker.get(i)); } int secondPart=0; int leng = elemSize.size(); int current = 0; for(int i=0;i<leng;++i ) { if((elemSize.get(i)%2)==0) { fin.add(current); current=1-current; continue; } int inserted=-1; int sz=fin.size(); int avail=-1 ; if(sz > 0) avail = fin.get(sz-1); int j=i+1; if(elemSize.get(i)>2) { inserted=1; fin.add(current); } while((j<leng) && ((elemSize.get(j)%2)==0)) { current=1-current; if(elemSize.get(j)>2) { fin.add(current); inserted=1; } j++; } if(j==leng) { out.println(cs); } current=1-current; if(elemSize.get(j) > 2) { inserted=1; fin.add(current); } if(inserted == -1) { fin.add(avail); } current=1-current; i=j; } secondPart= 0; leng = fin.size(); int lastenc=-1; int flag1=0; for(int i=0;i<leng;++i) { //System.out.println(fin.get(i)); if(fin.get(i)!=-1) { lastenc = fin.get(i); continue; } if(lastenc!=-1) { fin.set(i,lastenc); } else { int j=i; while((j<leng)&&(fin.get(j)==fin.get(i))) j++; int setelem; if(j==leng) { setelem=0; } else { setelem = fin.get(j); //fin.set(i,fin.get(j)); } for(int k=i;k<j;++k) fin.set(k, setelem); i=--j; } } for(int i=0;i<leng-1;++i) { if(fin.get(i)!=fin.get(i+1)) secondPart++; } out.printf("%d %d\n" , answer,secondPart+1); } out.close(); } } //Working program with FastReader
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
5ba900c6698f262785e8ccf07d851f0d
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 TaskB { public static void main(String[] arg) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); final int T = in.nextInt(); for (int t = 0; t < T; t++) { final int n = in.nextInt(); final char[] s = in.next().toCharArray(); int[] res = solution(n, s); out.printf("%d %d%n", res[0], res[1]); } out.flush(); out.close(); in.close(); } private static int[] solution(final int n, char[] s) { int operations = 0; for (int i = 0; i < n; i += 2) { if (s[i] == s[i + 1]) { continue; } operations++; if (i > 0) { s[i] = s[i - 1]; s[i + 1] = s[i]; } else { Character c = getNext(i + 2, s); s[i] = c; s[i + 1] = c; } } return new int[]{operations, countGroups(n, s).size()}; } private static Character getNext(int from, char[] s) { for (int i = from; i < s.length; i += 2) { if (s[i] == s[i + 1]) { return s[i]; } } return '0'; } private static List<Integer> countGroups(int n, char[] s) { final List<Integer> groups = new ArrayList<>(); int i = 0; while (i < n) { int j = i; while (j < n && s[i] == s[j]) { j++; } groups.add(j - i); i = j; } return groups; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } 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()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } long[] readLongArr(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = Long.parseLong(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } 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
00005b1d9fbe1f54ace134ac1d57ca61
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.*; // you can compare with output.txt and expected out public class Round789B { MyPrintWriter out; MyScanner in; final static String IMPOSSIBLE = "IMPOSSIBLE"; final static String POSSIBLE = "POSSIBLE"; final static String YES = "YES"; final static String NO = "NO"; private void preferFileIO(boolean isFileIO) { if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) { try{ in = new MyScanner(new FileInputStream("input.txt")); out = new MyPrintWriter(new FileOutputStream("output.txt")); } catch(FileNotFoundException e){ e.printStackTrace(); } } else{ in = new MyScanner(System.in); out = new MyPrintWriter(new BufferedOutputStream(System.out)); } } public static void main(String[] args){ // Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); Round789B sol = new Round789B(); sol.run(); } private void run() { boolean isDebug = false; boolean isFileIO = true; preferFileIO(isFileIO); int t = in.nextInt(); for (int i = 1; i <= t; ++i) { int n = in.nextInt(); // even String s = in.next(); if(isDebug){ out.printf("Test %d\n", i); out.println(s); } int[] ans = solve3(s); out.println(ans); if(isDebug) out.flush(); } in.close(); out.close(); } int[] solve3(String s) { boolean isPrev00 = false; boolean isPrev11 = false; int n = s.length(); char[] ss = s.toCharArray(); int numOp = 0; int numPieces = 0; for(int i=0; i<n; i+= 2) { if(ss[i] == ss[i+1]) { if(ss[i] == '0' && !isPrev00) numPieces++; else if(ss[i] == '1' && !isPrev11) numPieces++; if(ss[i] == '0') { isPrev00 = true; isPrev11 = false; } else { isPrev00 = false; isPrev11 = true; } } else { numOp++; // postpone the change to 00 or 11 // but eventually, this will not increase numPieces whatsoever } } if(numPieces == 0) numPieces++; return new int[] {numOp, numPieces}; } // can't do well for 101000 vs 101111 int[] solve2(String s) { int n = s.length(); char[] ss = s.toCharArray(); int numOp = 0; int numPieces = 0; ArrayList<Integer> chain = new ArrayList<>(); boolean isFirst = true; boolean appendedPrev = false; for(int i=0; i<n; i++) { int len = 1; while(i+len<n && ss[i] == ss[i+len]) len++; i=i+len-1; // ss[i..i+len-1] is one block boolean isEven = (len&1) == 0; if( !isEven && chain.size() == 0) chain.add(len); else if( isEven && chain.size() == 0) { if(!appendedPrev) numPieces++; else appendedPrev = false; isFirst = false; } else if( isEven) chain.add(len); else { chain.add(len); int left = 0; int right = chain.size()-1; boolean appendedPrevNext = false; while(left < right) { if(right-left+1 > 3) { if(chain.get(right) == 1) { numOp++; chain.set(right, 0); chain.set(right-1, chain.get(right-1)+1); if(right == chain.size()-1) appendedPrevNext = true; right--; } else if(chain.get(right-1) == 2) { numOp+=2; chain.set(right, chain.get(right)+1); chain.set(right-1, 0); chain.set(right-2, chain.get(right-2)+1); right -= 2; } else { numOp++; numPieces++; chain.set(right, chain.get(right)-1); chain.set(right-1, chain.get(right-1)+1); right--; } } else if(right-left+1 == 3) { if(appendedPrev) { if(chain.get(left+1) == 2) { numOp += 2; chain.set(left, chain.get(left)+1); chain.set(left+1, 0); chain.set(right, chain.get(right)+1); } else if(chain.get(right) == 1) { numOp += 2; numPieces++; chain.set(left, chain.get(left)-1); chain.set(left+1, chain.get(left+1)+2); chain.set(right, 0); if(right == chain.size()-1) appendedPrevNext = true; } else { numOp += 2; numPieces += 2; chain.set(left, chain.get(left)-1); chain.set(left+1, chain.get(left+1)+2); chain.set(right, chain.get(right)-1); } } else { if(chain.get(right) == 1) { numOp += 2; numPieces++; numPieces += chain.get(left) == 1? 0:1; chain.set(left, chain.get(left)-1); chain.set(left+1, chain.get(left+1)+2); chain.set(right, 0); if(right == chain.size()-1) appendedPrevNext = true; } else if(chain.get(left+1) == 2) { numOp += 2; numPieces++; chain.set(left, chain.get(left)+1); chain.set(left+1, 0); chain.set(right, chain.get(right)+1); } else { numOp += 2; numPieces += 2; numPieces += chain.get(left) == 1? 0:1; chain.set(left, chain.get(left)-1); chain.set(left+1, chain.get(left+1)+2); chain.set(right, chain.get(right)-1); } } break; } else { if(appendedPrev) { if(chain.get(right) == 1) { numOp++; chain.set(left, chain.get(left)+1); chain.set(right, 0); if(right == chain.size()-1) appendedPrevNext = true; } else { numOp++; numPieces++; chain.set(left, chain.get(left)+1); chain.set(right, chain.get(right)-1); } } else { if(chain.get(left) == 1 && !isFirst) { numOp++; chain.set(left, 0); chain.set(right, chain.get(right)+1); } else if(chain.get(right) == 1) { numOp++; numPieces++; chain.set(left, chain.get(left)+1); chain.set(right, 0); if(right == chain.size()-1) appendedPrevNext = true; } else if(chain.get(left) == 1) { // && isFirst numOp++; numPieces++; chain.set(left, 0); chain.set(right, chain.get(right)+1); } else { numOp++; numPieces += 2; chain.set(left, chain.get(left)+1); chain.set(right, chain.get(right)-1); } } break; } } chain.clear(); appendedPrev = appendedPrevNext; isFirst = false; // 3 6 6 6 6 3 // 2 7 6 6 6 3 // 2 6 7 6 6 3 // 2 6 6 7 6 3 // 2 6 6 6 7 3 // 2 6 6 6 6 4 // 3 2 6 6 6 3 // 4 0 7 6 6 3 } } return new int[] {numOp, numPieces}; } private int[] solve(String s) { // even even ... even -> good // odd odd -> switch the middle one // even odd -> we have to propagate? or discard all even or odd // 1110011000 // even odd even even even odd // even even even even odd odd // 2x 2x+1 2x 2x 2x 2x+1 // all even before first odd -- don't need to touch // odd odd -> convert to even even or possibly destroys the first odd to decrease # segment // edge case: either # odd is 1 // odd even even ..... odd // edge case: # odd is 1, # even is 2 // 1 0000 1111 00 1111 // 0 0000 .. // 0 000 11111 11 1111 // 1 0000 1111 0000 1111 // 0 000 1111 0000 1111 int n = s.length(); int numOp = 0; boolean isPrevEven = true; char[] ss = s.toCharArray(); ArrayList<Integer> chain = new ArrayList<>(); for(int i=0; i<n; i++) { int left = i; while(i+1 < n && ss[i] == ss[i+1]) { i++; } int right = i; int len = right-left+1; boolean isCurrEven = (len & 1) == 0; if(isPrevEven && isCurrEven) { continue; } else if(isPrevEven && !isCurrEven) { if(len == 1) { flip(ss, left); numOp++; i--; } else { isPrevEven = false; } } else if(!isPrevEven && isCurrEven) { if(len == 2) { flip(ss, left); flip(ss, right); numOp += 2; i = right-1; isPrevEven = true; } else { flip(ss, left); numOp ++; isPrevEven = false; } } else { if(len == 1) { flip(ss, left); numOp++; i-=2; isPrevEven = true; } else { flip(ss, left); numOp++; isPrevEven = true; } // if(prevLen == 1 && len == 1) { // flip(ss, left-1); // numOp ++; // } // else if(prevLen == 1 && len > 1) { // flip(ss, left-1); // numOp++; //// numPieces++; // prevLen = len+1; // } // else if(len == 1) { // flip(ss, left); // numOp++; // prevLen++; // } // else { // flip(ss, left); // numOp++; // //numPieces++; // prevLen = len-1; // } // isPrevEven = true; } } // System.out.println(ss); int numPieces = 0; for(int i=0; i<n; i++) { numPieces++; while(i+1 < n && ss[i] == ss[i+1]) { i++; } } return new int[] {numOp, numPieces}; } private void flip(char[] s, int i) { if(s[i] == '0') s[i] = '1'; else s[i] = '0'; } public static class MyScanner { BufferedReader br; StringTokenizer st; // 32768? public MyScanner(InputStream is, int bufferSize) { br = new BufferedReader(new InputStreamReader(is), bufferSize); } public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); // br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[][] nextGraphEdges(){ return nextGraphEdges(0); } int[][] nextGraphEdges(int offset) { int m = nextInt(); int[][] e = new int[m][2]; for(int i=0; i<m; i++){ e[i][0] = nextInt()+offset; e[i][1] = nextInt()+offset; } return e; } int[] nextIntArray(int len) { return nextIntArray(len, 0); } int[] nextIntArray(int len, int offset){ int[] a = new int[len]; for(int j=0; j<len; j++) a[j] = nextInt()+offset; return a; } long[] nextLongArray(int len) { return nextLongArray(len, 0); } long[] nextLongArray(int len, int offset){ long[] a = new long[len]; for(int j=0; j<len; j++) a[j] = nextLong()+offset; return a; } } public static class MyPrintWriter extends PrintWriter{ public MyPrintWriter(OutputStream os) { super(os); } public void print(long[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void println(long[] arr){ print(arr); println(); } public void print(int[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void println(int[] arr){ print(arr); println(); } public <T> void print(ArrayList<T> arr){ if(arr != null && arr.size() > 0){ print(arr.get(0)); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)); } } } public <T> void println(ArrayList<T> arr){ print(arr); println(); } public void println(int[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public <T> void println(ArrayList<T> arr, int split){ if(arr != null && !arr.isEmpty()){ for(int i=0; i<arr.size(); i+=split){ print(arr.get(i)); for(int j=i+1; j<i+split; j++){ print(" "); print(arr.get(j)); } println(); } } } } static private int[][] constructChildren(int n, int[] parent, int parentRoot){ int[][] childrens = new int[n][]; int[] numChildren = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) numChildren[parent[i]]++; } for(int i=0; i<n; i++) { childrens[i] = new int[numChildren[i]]; } int[] idx = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) childrens[parent[i]][idx[parent[i]]++] = i; } return childrens; } static private int[][] constructNeighborhood(int n, int[][] e) { int[] degree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; degree[u]++; degree[v]++; } int[][] neighbors = new int[n][]; for(int i=0; i<n; i++) neighbors[i] = new int[degree[i]]; int[] idx = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; neighbors[u][idx[u]++] = v; neighbors[v][idx[v]++] = u; } return neighbors; } static private void makeDotUndirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict graph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "--" + e[i][1] + ";"); } out2.println("}"); out2.close(); } static private void makeDotDirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict digraph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "->" + e[i][1] + ";"); } out2.println("}"); out2.close(); } }
Java
["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
2f39161f79a8726f139aad4449adcb22
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.text.DecimalFormat; import java.util.Arrays; import java.util.*; import java.util.Scanner; import java.util.StringTokenizer; public class copy { static int log=18; static int[][] ancestor; static int[] depth; static void sieveOfEratosthenes(int n, ArrayList<Integer> arr) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p]) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i]) { arr.add(i); } } } public static long fac(long N, long mod) { if (N == 0) return 1; if(N==1) return 1; return ((N % mod) * (fac(N - 1, mod) % mod)) % mod; } static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if (y % 2 == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p static long modInverse(long n, long p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static long nCrModPFermat(long n, long r, long p) { if (n < r) return 0; // Base case if (r == 0) return 1; return ((fac(n, p) % p * (modInverse(fac(r, p), p) % p)) % p * (modInverse(fac(n - r, p), p) % p)) % p; } public static int find(int[] parent, int x) { if (parent[x] == x) return x; return find(parent, parent[x]); } public static void merge(int[] parent, int[] rank, int x, int y,int[] child) { int x1 = find(parent, x); int y1 = find(parent, y); if (rank[x1] > rank[y1]) { parent[y1] = x1; child[x1]+=child[y1]; } else if (rank[y1] > rank[x1]) { parent[x1] = y1; child[y1]+=child[x1]; } else { parent[y1] = x1; child[x1]+=child[y1]; rank[x1]++; } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int power(int x, int y, int p) { int res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } // Returns n^(-1) mod p static int modInverse(int n, int p) { return power(n, p - 2, p); } // Returns nCr % p using Fermat's // little theorem. static int nCrModPFermat(int n, int r, int p) { if (n<r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r int[] fac = new int[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } public static long[][] ncr(int n,int r) { long[][] dp=new long[n+1][r+1]; for(int i=0;i<=n;i++) dp[i][0]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=r;j++) { if(j>i) continue; dp[i][j]=dp[i-1][j-1]+dp[i-1][j]; } } return dp; } public static boolean prime(long N) { int c=0; for(int i=2;i*i<=N;i++) { if(N%i==0) ++c; } return c==0; } public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child) { int c=0; for(int i:arr.get(x)) { if(i!=parent) { // System.out.println(i+" hello "+x); depth[i]=depth[x]+1; ancestor[i][0]=x; // if(i==2) // System.out.println(parent+" hello"); for(int j=1;j<log;j++) ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1]; c+=sparse_ancestor_table(arr,i,x,child); } } child[x]=1+c; return child[x]; } public static int lca(int x,int y) { if(depth[x]<depth[y]) { int temp=x; x=y; y=temp; } x=get_kth_ancestor(depth[x]-depth[y],x); if(x==y) return x; // System.out.println(x+" "+y); for(int i=log-1;i>=0;i--) { if(ancestor[x][i]!=ancestor[y][i]) { x=ancestor[x][i]; y=ancestor[y][i]; } } return ancestor[x][0]; } public static int get_kth_ancestor(int K,int x) { if(K==0) return x; int node=x; for(int i=0;i<log;i++) { if(K%2!=0) { node=ancestor[node][i]; } K/=2; } return node; } public static ArrayList<Integer> primeFactors(int n) { // Print the number of 2s that divide n ArrayList<Integer> factors=new ArrayList<>(); if(n%2==0) factors.add(2); while (n%2==0) { n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n if(n%i==0) factors.add(i); while (n%i == 0) { n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { factors.add(n); } return factors; } static long ans=1,mod=1000000007; public static void recur(long X,long N,int index,ArrayList<Integer> temp) { // System.out.println(X); if(index==temp.size()) { System.out.println(X); ans=((ans%mod)*(X%mod))%mod; return; } for(int i=0;i<=60;i++) { if(X*Math.pow(temp.get(index),i)<=N) recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp); else break; } } public static int upper(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)<X) l=mid+1; else { if(mid-1>=0 && temp.get(mid-1)>=X) r=mid-1; else return mid; } } return -1; } public static int lower(ArrayList<Integer> temp,int X) { int l=0,r=temp.size()-1; while(l<=r) { int mid=(l+r)/2; if(temp.get(mid)==X) return mid; // System.out.println(mid+" "+temp.get(mid)); if(temp.get(mid)>X) r=mid-1; else { if(mid+1<temp.size() && temp.get(mid+1)<=X) l=mid+1; else return mid; } } return -1; } public static int[] check(String shelf,int[][] queries) { int[] arr=new int[queries.length]; ArrayList<Integer> indices=new ArrayList<>(); for(int i=0;i<shelf.length();i++) { char ch=shelf.charAt(i); if(ch=='|') indices.add(i); } for(int i=0;i<queries.length;i++) { int x=queries[i][0]-1; int y=queries[i][1]-1; int left=upper(indices,x); int right=lower(indices,y); if(left<=right && left!=-1 && right!=-1) { arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1); } else arr[i]=0; } return arr; } static boolean check; public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited) { visited[x]=true; PriorityQueue<Integer> pq=new PriorityQueue<>(); for(int i:arr.get(x)) { if(color[i]<color[x]) pq.add(color[i]); if(color[i]==color[x]) check=false; if(!visited[i]) check(arr,i,color,visited); } int start=1; while(pq.size()>0) { int temp=pq.poll(); if(temp==start) ++start; else break; } if(start!=color[x]) check=false; } static boolean cycle; public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr) { if(stack[x]) { cycle=true; return; } visited[x]=true; for(int i:arr.get(x)) { if(!visited[x]) { cycle(stack,visited,i,arr); } } stack[x]=false; } public static int check(char[][] ch,int x,int y) { int cnt=0; int c=0; int N=ch.length; int x1=x,y1=y; while(c<ch.length) { if(ch[x][y]=='1') ++cnt; // if(x1==0 && y1==3) // System.out.println(x+" "+y+" "+cnt); x=(x+1)%N; y=(y+1)%N; ++c; } return cnt; } public static void s(char[][] arr,int x) { char start=arr[arr.length-1][x]; for(int i=arr.length-1;i>0;i--) { arr[i][x]=arr[i-1][x]; } arr[0][x]=start; } public static void shuffle(char[][] arr,int x,int down) { int N= arr.length; down%=N; char[] store=new char[N-down]; for(int i=0;i<N-down;i++) store[i]=arr[i][x]; for(int i=0;i<arr.length;i++) { if(i<down) { // Printing rightmost // kth elements arr[i][x]=arr[N + i - down][x]; } else { // Prints array after // 'k' elements arr[i][x]=store[i-down]; } } } public static String form(int C1,char ch1,char ch2) { char ch=ch1; String s=""; for(int i=1;i<=C1;i++) { s+=ch; if(ch==ch1) ch=ch2; else ch=ch1; } return s; } public static void printArray(long[] arr) { for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(); } public static boolean check(long mid,long[] arr,long K) { long[] arr1=Arrays.copyOfRange(arr,0,arr.length); long ans=0; for(int i=0;i<arr1.length-1;i++) { if(arr1[i]+arr1[i+1]>=mid) { long check=(arr1[i]+arr1[i+1])/mid; // if(mid==5) // System.out.println(check); long left=check*mid; left-=arr1[i]; if(left>=0) arr1[i+1]-=left; ans+=check; } // if(mid==5) // printArray(arr1); } // if(mid==5) // System.out.println(ans); ans+=arr1[arr1.length-1]/mid; return ans>=K; } public static long search(long sum,long[] arr,long K) { long l=1,r=sum/K; while(l<=r) { long mid=(l+r)/2; if(check(mid,arr,K)) { if(mid+1<=sum/K && check(mid+1,arr,K)) l=mid+1; else return mid; } else r=mid-1; } return -1; } public static void primeFactors(int n,HashSet<Integer> hp) { // Print the number of 2s that divide n ArrayList<Integer> factors=new ArrayList<>(); if(n%2==0) hp.add(2); while (n%2==0) { n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n if(n%i==0) hp.add(i); while (n%i == 0) { n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) { hp.add(n); } } public static boolean check(String s) { HashSet<Character> hp=new HashSet<>(); char ch=s.charAt(0); for(int i=1;i<s.length();i++) { // System.out.println(hp+" "+s.charAt(i)); if(hp.contains(s.charAt(i))) { // System.out.println(i); // System.out.println(hp); // System.out.println(s.charAt(i)); return false; } if(s.charAt(i)!=ch) { hp.add(ch); ch=s.charAt(i); } } return true; } public static int check_end(String[] arr,boolean[] st,char ch) { for(int i=0;i<arr.length;i++) { if(ch==arr[i].charAt(0) && !st[i] && ch==arr[i].charAt(arr[i].length()-1)) return i; } for(int i=0;i<arr.length;i++) { if(ch==arr[i].charAt(0) && !st[i]) return i; } return -1; } public static int check_start(String[] arr,boolean[] st,char ch) { for(int i=0;i<arr.length;i++) { // if(ch=='B') // { // if(!st[i]) // System.out.println(arr[i]+" hello"); // } if(ch==arr[i].charAt(arr[i].length()-1) && !st[i] && ch==arr[i].charAt(0)) return i; } for(int i=0;i<arr.length;i++) { // if(ch=='B') // { // if(!st[i]) // System.out.println(arr[i]+" hello"); // } if(ch==arr[i].charAt(arr[i].length()-1) && !st[i]) return i; } return -1; } public static boolean palin(int N) { String s=""; while(N>0) { s+=N%10; N/=10; } int l=0,r=s.length()-1; while(l<=r) { if(s.charAt(l)!=s.charAt(r)) return false; ++l; --r; } return true; } public static boolean check(long org_s,long org_d,long org_n,long check_ele) { if(check_ele<org_s) return false; if((check_ele-org_s)%org_d!=0) return false; long num=(check_ele-org_s)/org_d; // if(check_ele==5) // System.out.println(num+" "+org_n); return num+1<=org_n; } public static long check(long c,long c_diff,long mod,long b_start,long c_start, long c_end,long b_end,long b_diff) { // System.out.println(c); long max=Math.max(c,b_diff); long min=Math.min(c,b_diff); long lcm=(max/gcd(max,min))*min; // System.out.println(lcm); // System.out.println(c); // System.out.println(c_diff); // if(b_diff>c) // { long start_point=c_diff/c-c_diff/lcm; // System.out.println(start_point); // } // else // { // start_point=c_diff/b_diff-c_diff/c; // } // System.out.println(c+" "+start_point); return (start_point%mod*start_point%mod)%mod; } public static boolean check_bounds(int x,int y,int[][] arr,int N,int M) { return x>=0 && x<N && y>=0 && y<M && (arr[x][y]==1 || arr[x][y]==9); } static boolean found=false; public static void check(int x,int y,int[][] arr,boolean status[][]) { if(arr[x][y]==9) { found=true; return; } status[x][y]=true; if(check_bounds(x-1,y,arr,arr.length,arr[0].length)&& !status[x-1][y]) check(x-1,y,arr,status); if(check_bounds(x+1,y,arr,arr.length,arr[0].length)&& !status[x+1][y]) check(x+1,y,arr,status); if(check_bounds(x,y-1,arr,arr.length,arr[0].length)&& !status[x][y-1]) check(x,y-1,arr,status); if(check_bounds(x,y+1,arr,arr.length,arr[0].length)&& !status[x][y+1]) check(x,y+1,arr,status); } public static int check(String s1,String s2,int M) { int ans=0; for(int i=0;i<M;i++) { ans+=Math.abs(s1.charAt(i)-s2.charAt(i)); } return ans; } public static int check(int[][] arr,int dir1,int dir2,int x1,int y1) { int sum=0,N=arr.length,M=arr[0].length; int x=x1+dir1,y=y1+dir2; while(x<N && x>=0 && y<M && y>=0) { sum+=arr[x][y]; x=x+dir1; y+=dir2; } return sum; } public static int check(long[] pref,long X,int N) { if(X>pref[N-1]) return -1; // System.out.println(pref[0]); if(X<=pref[0]) return 1; int l=0,r=N-1; while(l<=r) { int mid=(l+r)/2; if(pref[mid]>=X) { if(mid-1>=0 && pref[mid-1]<X) return mid+1; else r=mid-1; } else l=mid+1; } return -1; } public static div dfs(ArrayList<ArrayList<Integer>> arr,int x,int parent,char[] c) { int w=0,b=0; for(int i:arr.get(x)) { if(i!=parent) { div temp=dfs(arr,i,x,c); w+=temp.x; b+=temp.y; } } if(c[x]=='W') w+=1; else b+=1; if(w==b) ++ans; return new div(w,b); } private static long mergeAndCount(int[] arr, int l, int m, int r) { // Left subarray int[] left = Arrays.copyOfRange(arr, l, m + 1); // Right subarray int[] right = Arrays.copyOfRange(arr, m + 1, r + 1); int i = 0, j = 0, k = l;long swaps = 0; while (i < left.length && j < right.length) { if (left[i] < right[j]) arr[k++] = left[i++]; else { arr[k++] = right[j++]; swaps += (m + 1) - (l + i); } } while (i < left.length) arr[k++] = left[i++]; while (j < right.length) arr[k++] = right[j++]; return swaps; } // Merge sort function private static long mergeSortAndCount(int[] arr, int l, int r) { // Keeps track of the inversion count at a // particular node of the recursion tree long count = 0; if (l < r) { int m = (l + r) / 2; // Total inversion count = left subarray count // + right subarray count + merge count // Left subarray count count += mergeSortAndCount(arr, l, m); // Right subarray count count += mergeSortAndCount(arr, m + 1, r); // Merge count count += mergeAndCount(arr, l, m, r); } return count; } public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int T=Reader.nextInt(); A:for(int m=1;m<=T;m++) { int N=Reader.nextInt(); char[] arr=Reader.next().toCharArray(); // if(N==2) // { // if(arr[0]!=arr[1]) // { // output.write(1+" "+1+"\n"); // // } // else // output.write(0+" "+1+"\n"); // continue; // } int mov=0,cnt=0; String s=""; for(int i=0;i<N;i+=2) { if(arr[i]!=arr[i+1]) { ++mov; } else s+=arr[i]; } for(int i=1;i<s.length();i++) { if(s.charAt(i)!=s.charAt(i-1)) ++cnt; } cnt+=1; output.write(mov+" "+cnt+"\n"); } output.flush(); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { left=null; right=null; this.data=data; } } class div { int x; int y; div(int x,int y) { this.x=x; this.y=y; } }
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
fcabc3082088d1e2ba66d7489d297f02
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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Test{ static FastReader scan; static void solveA(){ int n=scan.nextInt(); int[]arr=scanInt(n); Set<Integer>set=new HashSet<>(); boolean ans=false; int z=0; for(int x:arr){ if(x==0)z++; if(set.contains(x))ans=true; else set.add(x); } if(z==0){ if(ans){ System.out.println(n); } else{ System.out.println(n+1); } } else{ System.out.println(n-z); } } static void solveB(){ int n=scan.nextInt(); String str=scan.next(); char[]ch=str.toCharArray(); char prev='2'; int f=0,s=0; for(int i=0;i<n;i+=2){ if(ch[i]!=ch[i+1]){ f++; } else{ if(ch[i]!=prev){ prev=ch[i]; s++; } } } System.out.println(f+" "+Math.max(1,s)); } public static void main (String[] args) throws java.lang.Exception{ scan=new FastReader(); int t=scan.nextInt(); while(t-->0){ solveB(); } } static int[] scanInt(int n){ int[]arr=new int[n]; for(int i=0;i<arr.length;i++){ arr[i]=scan.nextInt(); } return arr; } 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
65e4b84796d63e3d062af3b72e1467a6
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.InputStreamReader; public class C { public static void main(String args[]) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { br.readLine(); char arr[] = br.readLine().trim().toCharArray(); sb.append(solve(arr)).append("\n"); } br.close(); System.out.println(sb); } private static String solve(char arr[]) { int ops = 0, segs = 0; char prev = ' '; for (int i = 0; i < arr.length; i += 2) if (arr[i] != arr[i + 1]) ops++; else { if (prev != arr[i]) segs++; prev = arr[i]; } return ops + " " + Math.max(segs, 1); } private static int getMinSubSegmentCount(char arr[], String s, int cur) { if (cur == arr.length) return getSegmentCount(s); if (arr[cur] != arr[cur + 1]) return Math.min(getMinSubSegmentCount(arr, s + "00", cur + 2), getMinSubSegmentCount(arr, s + "11", cur + 2)); return getMinSubSegmentCount(arr, s + arr[cur] + arr[cur + 1], cur + 2); } private static int getSegmentCount(String s) { int ret = 1; char start = s.charAt(0); for (int i = 0; i < s.length(); i++) if (s.charAt(i) != start) { start = s.charAt(i); ret++; } return ret; } }
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
d29bead15066fe6e30fc90efacd38dbb
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 Good01String { public static void main(String[] args) throws IOException { // BufferedReader in = new BufferedReader(new FileReader("Good01String.in")); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Good01String.out"))); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = Integer.parseInt(in.readLine()); for (int t = 0; t < T; t++) { int N = Integer.parseInt(in.readLine()); String s = in.readLine(); int[][] dp = new int[N][2]; for (int i = 0; i < N; i++) { dp[i][0] = Integer.MAX_VALUE - 1; dp[i][1] = Integer.MAX_VALUE - 1; } if (s.charAt(0) == s.charAt(1)) { dp[0][s.charAt(0) - '0'] = 1; } else { dp[0][0] = 1; dp[0][1] = 1; } int ans1 = s.charAt(0) == s.charAt(1) ? 0 : 1; for (int i = 2; i < N; i += 2) { boolean same = s.charAt(i) == s.charAt(i + 1); ans1 += same ? 0 : 1; if (same) { int idx = s.charAt(i) - '0'; dp[i][idx] = Math.min( dp[i - 2][idx], dp[i - 2][idx ^ 1] + 1); } else { dp[i][0] = Math.min( dp[i - 2][0], dp[i - 2][1] + 1); dp[i][1] = Math.min( dp[i - 2][0] + 1, dp[i - 2][1]); } } out.println(ans1 + " "+ Math.min(dp[N - 2][0], dp[N - 2][1])); } out.close(); 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 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
9564fab38d555f885aa4a9d31d21f971
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 { private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private FastWriter wr; private Reader rd; public final int MOD = 1000000007; /************************************************** FAST INPUT IMPLEMENTATION *********************************************/ class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader( new InputStreamReader(System.in)); } public Reader(String path) throws FileNotFoundException { br = new BufferedReader( new InputStreamReader(new FileInputStream(path))); } 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 int ni() throws IOException { return rd.nextInt(); } public long nl() throws IOException { return rd.nextLong(); } public void yOrn(boolean flag){ if(flag){ wr.println("YES"); }else { wr.println("NO"); } } char nc() throws IOException { return rd.next().charAt(0); } public String ns() throws IOException { return rd.nextLine(); } public Double nd() throws IOException { return rd.nextDouble(); } public int[] nai(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } public long[] nal(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } /************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/ 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(); } } /********************************************************* USEFUL CODE **************************************************/ boolean[] SAPrimeGenerator(int n) { // TC-N*LOG(LOG N) //Create Prime Marking Array and fill it with true value boolean[] primeMarker = new boolean[n + 1]; Arrays.fill(primeMarker, true); primeMarker[0] = false; primeMarker[1] = false; for (int i = 2; i <= n; i++) { if (primeMarker[i]) { // we start from 2*i because i*1 must be prime for (int j = 2 * i; j <= n; j += i) { primeMarker[j] = false; } } } return primeMarker; } private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } class Pair<F, S> { private F first; private S second; Pair(F first, S second) { this.first = first; this.second = second; } public F getFirst() { return first; } public S getSecond() { return second; } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<F, S> pair = (Pair<F, S>) o; return first == pair.first && second == pair.second; } @Override public int hashCode() { return Objects.hash(first, second); } } class PairThree<F, S, X> { private F first; private S second; private X third; PairThree(F first, S second,X third) { this.first = first; this.second = second; this.third=third; } public F getFirst() { return first; } public void setFirst(F first) { this.first = first; } public S getSecond() { return second; } public void setSecond(S second) { this.second = second; } public X getThird() { return third; } public void setThird(X third) { this.third = third; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairThree<?, ?, ?> pair = (PairThree<?, ?, ?>) o; return first.equals(pair.first) && second.equals(pair.second) && third.equals(pair.third); } @Override public int hashCode() { return Objects.hash(first, second, third); } } public static void main(String[] args) throws IOException { new Main().run(); } public void run() throws IOException { if (oj) { rd = new Reader(); wr = new FastWriter(System.out); } else { File input = new File("input.txt"); File output = new File("output.txt"); if (input.exists() && output.exists()) { rd = new Reader(input.getPath()); wr = new FastWriter(output.getPath()); } else { rd = new Reader(); wr = new FastWriter(System.out); oj = true; } } long s = System.currentTimeMillis(); solve(); wr.flush(); tr(System.currentTimeMillis() - s + "ms"); } /*************************************************************************************************************************** *********************************************************** MAIN CODE ****************************************************** ****************************************************************************************************************************/ boolean[] sieve; public void solve() throws IOException { int t = 1; t = ni(); while (t-- > 0) { go(); } } /********************************************************* MAIN LOGIC HERE ****************************************************/ long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public void go() throws IOException { int n=ni(); String st=ns(); char[] arr=st.toCharArray(); char dominantChar='0'; for(int i=0;i<arr.length-1;i+=2){ if(arr[i]==arr[i+1]){ dominantChar=arr[i]; break; } } int counter=0; for(int i=0;i<arr.length-1;i+=2){ if(arr[i]==arr[i+1]){ dominantChar=arr[i]; }else { arr[i]=dominantChar; arr[i+1]=dominantChar; counter++; } } int lastChar=arr[0]; int cnt=1; for(int i=1;i<n-1;i++){ if(lastChar!=arr[i]){ cnt++; lastChar=arr[i]; } } wr.println(counter+" "+cnt); } }
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
4feb1a0fb67b58ffc3a1e504096248fd
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 TokitsukazeAndGoodStringH { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); String s = sc.next(); int ans = 0; int count = 1; int ch = ' '; for (int i = 0; i < n - 1; i+=2) { if (s.charAt(i) != s.charAt(i + 1)) { ans++; } else { if (ch == ' ') { ch = s.charAt(i); } else if (ch != s.charAt(i)) { count++; ch = s.charAt(i); } } } System.out.println(ans + " " + 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 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
2b512b2e56f3ae4ebffa0c71eb795766
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
// Contest 1678, Problem B2 // Tokitsukaze and Good 01-String (hard version) import java.io.*; import java.util.*; public class B2 { static BufferedInputStream bis; public static int readInt() throws IOException { int num = 0; int b = bis.read(); while(b < '0' || b > '9') b = bis.read(); while(b >= '0') { num = num*10 + b - '0'; b = bis.read(); } return num; } public static void main(String[] args) throws IOException { bis = new BufferedInputStream(System.in); StringBuilder sb = new StringBuilder(); int T = readInt(); for(int cN=0; cN<T; cN++) { int N = readInt(); int last = -1; int count = 0; int moves = 0; int b = bis.read(); while(b < '0' || b > '1') b = bis.read(); int [] s = new int [N]; int UNKNOWN = '?'; for(int i=0; i<N; i++) { if(last != b) { if((count&1)==1) { // count was odd, either change current character to previous bit or previous bit to current char count++; moves++; s[i-1] = UNKNOWN; s[i] = UNKNOWN; } else { // count was even, switch to counting new bit count = 1; last = b; s[i] = b; } } else { // no change s[i] = b; count++; } b = bis.read(); } // N is guaranteed to be even so count must be even at the end // now count min number of segments int seg = 1; int x = 0; while(x < N && s[x] == UNKNOWN) x++; if(x < N) { int y = x+1; while(y < N) { if(s[y] != UNKNOWN && s[y] != s[x]) { seg++; x = y; } y++; } } sb.append(moves).append(' ').append(seg).append('\n'); } System.out.print(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 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
59da5c13f20bfa31ac77b0093b37ee06
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
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class CodeForces { static private final String INPUT = "input.txt"; static private final String OUTPUT = "output.txt"; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.00000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; final static long INF = Long.MAX_VALUE; final static long NEG_INF = Long.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== input(); preprocess(); int t = 1; t = readInt(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(); String s = readLine(); int ans = 0, same = 1; for (int i = 1; i < n; i++) { if (s.charAt(i) != s.charAt(i - 1)) { if ((same & 1) == 1) { ans++; same = 1; } else same = 0; } same++; } dp = new Integer[n][3]; out.println(ans + " " + helper(s, 0, 2)); } static Integer[][] dp; private static int helper(String s, int i, int prev) { if (i == s.length()) return 1; if (dp[i][prev] != null) return dp[i][prev]; if (s.charAt(i) == s.charAt(i + 1)) { if (s.charAt(i) - '0' == prev || i == 0) return dp[i][prev] = helper(s, i + 2, s.charAt(i) - '0'); else return dp[i][prev] = 1 + helper(s, i + 2, s.charAt(i) - '0'); } else { if (i > 0) return dp[i][prev] = helper(s, i + 2, prev); else return dp[i][prev] = Math.min(helper(s, i + 2, s.charAt(i) - '0'), helper(s, i + 2, s.charAt(i + 1) - '0')); } } private static void preprocess() throws IOException { } // cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces // javac CodeForces.java // java CodeForces // javac CodeForces.java && java CodeForces // ==================== CUSTOM CLASSES ================================ static class Pair { int first, second; Pair(int f, int s) { first = f; second = s; } public int compareTo(Pair o) { if (this.first == o.first) return this.second - o.second; return this.first - o.first; } @Override public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (this.getClass() != obj.getClass()) return false; Pair other = (Pair) (obj); if (this.first != other.first) return false; if (this.second != other.second) return false; return true; } @Override public int hashCode() { return this.first ^ this.second; } @Override public String toString() { return this.first + " " + this.second; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== private static void input() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { System.err.println("Error Occurred."); } br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static int mod_pow(long a, long b, int mod) { if (b == 0) return 1; int temp = mod_pow(a, b >> 1, mod); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static int divide(int a, int b) { return multiply(a, mod_pow(b, mod - 2, mod)); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } private static List<Integer> factors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; 1L * i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } private static List<Long> factors(long n) { List<Long> list = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { list.add(i); if (i != n / i) list.add(n / i); } return list; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; 1L * i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; 1L * i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) if (primes[j] == j) primes[j] = i; } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } // check if a is subsequence of b private static boolean isSubsequence(String a, String b) { int idx = 0; for (int i = 0; i < b.length() && idx < a.length(); i++) if (a.charAt(idx) == b.charAt(i)) idx++; return idx == a.length(); } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ================== public static class SegmentTree { int n; long[] arr, tree, lazy; SegmentTree(long arr[]) { this.arr = arr; this.n = arr.length; this.tree = new long[(n << 2)]; this.lazy = new long[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, long val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, long val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } long query(int l, int r) { return query(1, 0, n - 1, l, r); } long query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0L; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ==================== FENWICK TREE ================================ static class FT { long[] tree; int n; FT(int[] arr, int n) { this.n = n; this.tree = new long[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return ans; } } }
Java
["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
2d90400e6f6612e01e3360b2247863d3
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 Main { static AReader scan = new AReader(); static int N = 200010; static int[][] dp = new int[N][2]; static void solve() { int n = scan.nextInt(); char[] cs = scan.next().toCharArray(); dp[0][0] = dp[0][1] = 0; //00 01 10 11 for(int i = 2;i<=n;i+=2){ if(cs[i-1] == '0'){ dp[i][0] = Math.min(dp[i-2][0],dp[i-2][1]); if(cs[i-2] == '1') dp[i][0] += 1; dp[i][1] = Math.min(dp[i-2][0],dp[i-2][1]) + 1; if(cs[i-2] == '0') dp[i][1] +=1; }else{ // 当前为是1 dp[i][0] = Math.min(dp[i-2][0],dp[i-2][1]) + 1; if(cs[i-2] == '1') dp[i][0] +=1; dp[i][1] = Math.min(dp[i-2][0],dp[i-2][1]); if(cs[i-2] == '0') dp[i][1] +=1; } } int ans = Math.min(dp[n][0],dp[n][1]); int cnt = 0; int last = -1; for(int i = 2;i<=n;i++){ if(dp[i][0] == dp[i][1]) continue; if(dp[i][0] < dp[i][1]){ if(last != 0 && last != -1) cnt++; last = 0; }else{ if(last != 1 && last != -1) cnt++; last = 1; } } System.out.println(ans + " " + ++cnt); } public static void main(String[] args) { int T = scan.nextInt(); while (T-- > 0) { solve(); } } } class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); 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 Pair { int x,y; public 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
b6317677bb8c2cb3627d56d4a2a774ca
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.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Random; import java.util.StringTokenizer; /* Every number has to appear an even number of times Go back to front, swap if don't find */ public class Sol { public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int T=fs.nextInt(); // int T=1; outer: for (int tt=0; tt<T; tt++) { list.clear(); int n=fs.nextInt(); char[] s=fs.next().toCharArray(); int ans = 0, len = 0; char ch = '#'; for (int i = 0; i + 1 < n; i += 2){ if(s[i] == s[i + 1]){ if(ch != s[i]){ len += 1; ch = s[i]; } } else { ans += 1; } } out.println(ans + " " + Math.max(len, 1)); } out.close(); } static ArrayList<Integer> list=new ArrayList<>(); static void swap(int a, int b, char[] s, char[] t) { list.add(a+1); list.add(b+1); char temp=s[a]; s[a]=t[b]; t[b]=temp; } static final Random random=new Random(); static final int mod=998244353; static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long add(long a, long b) { return (a+b)%mod; } static long sub(long a, long b) { return ((a-b)%mod+mod)%mod; } static long mul(long a, long b) { return (a*b)%mod; } static long exp(long base, long exp) { if (exp==0) return 1; long half=exp(base, exp/2); if (exp%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials=new long[2_000_001]; static long[] invFactorials=new long[2_000_001]; static void precompFacts() { factorials[0]=invFactorials[0]=1; for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i); invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2); for (int i=invFactorials.length-2; i>=0; i--) invFactorials[i]=mul(invFactorials[i+1], i+1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k])); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public FastScanner() { try { String filename=this.getClass().getEnclosingClass().getSimpleName(); br=new BufferedReader(new FileReader(new File(filename+".in"))); } catch (Exception e) { System.err.println("Using standard input instead."); br=new BufferedReader(new InputStreamReader(System.in)); } } 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
ec7583ef8513e1e34d21ff8f65e9b669
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.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.*; public class b { public static void main(String []args) { MyScanner s=new MyScanner(); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); String str=s.next(); char []c=str.toCharArray(); char l='2'; int res=0; int ans=0; for(int i=0;i<n;i+=2) { if(c[i]!=c[i+1]) res++; else { if(l!=c[i]) ans++; l=c[i]; } } System.out.println(res+" "+Math.max(ans, 1)); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } public static boolean is(String s) { int left=0; int right=s.length()-1; while(left<right) { if(s.charAt(left)!=s.charAt(right)) return false; left++; right--; } 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
5e0cf1fd74a61464bbbd908fadfc3ccb
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 C{ public static void main(String[] args) { FastReader sc = new FastReader(); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); out:while(t-- >0) { int n = sc.nextInt(); char[] ch = sc.nextLine().toCharArray(); int l =-1; int seg = 0; int flips = 0; for(int i =0;i<n;i+=2) { if(ch[i]!=ch[i+1]) { flips++; } else { if(ch[i]-'0'!=l) { seg++; l = ch[i]-'0'; } } } sb.append(flips).append(" ").append(Math.max(seg, 1)).append("\n"); } System.out.println(sb); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } String nextLine() { String str = ""; try { 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
3a83f76f9a50a6ab43326cb1cafdbca5
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.time.Year; public class Solution { public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[] arr = sc.next().toCharArray(); int count = 0; int segments = 0; char l = '2'; for (int i = 0; i < arr.length; i += 2) { if (arr[i] != arr[i + 1]) count++; else { if (arr[i] != l) segments++; l = arr[i]; } } pw.println(count + " " + Math.max(segments, 1)); } pw.close(); } static HashMap Hash(int[] arr) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static HashMap Hash(char[] arr) { HashMap<Character, Integer> map = new HashMap<>(); for (char i : arr) { map.put(i, map.getOrDefault(i, 0) + 1); } return map; } static boolean isPrime(int n) { if (n <= 1) return false; for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; return true; } public static long combination(long n, long r) { return factorial(n) / (factorial(n - r) * factorial(r)); } static long factorial(Long n) { if (n == 0) return 1; return n * factorial(n - 1); } static boolean isPalindrome(char[] str, int i, int j) { // While there are characters to compare while (i < j) { // If there is a mismatch if (str[i] != str[j]) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static double log2(int N) { double result = (Math.log(N) / Math.log(2)); return result; } public static double log4(int N) { double result = (Math.log(N) / Math.log(4)); return result; } public static int setBit(int mask, int idx) { return mask | (1 << idx); } public static int clearBit(int mask, int idx) { return mask ^ (1 << idx); } public static boolean checkBit(int mask, int idx) { return (mask & (1 << idx)) != 0; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } public static String toString(int[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(ArrayList arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.size(); i++) { sb.append(arr.get(i) + " "); } return sb.toString().trim(); } public static String toString(int[][] arr) { StringBuilder sb = new StringBuilder(); for (int[] i : arr) { sb.append(toString(i) + "\n"); } return sb.toString(); } public static String toString(boolean[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(Integer[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(String[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } public static String toString(char[] arr) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i] + " "); } return sb.toString().trim(); } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3 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
301cc08fcb78c3cada1629f3e09e6d22
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 B2 { static IOHandler in = new IOHandler(); public static void main(String[] args) { // TODO Auto-generated method stub int testCases = in.nextInt(); for (int i = 1; i <= testCases; ++i) { solve(i); } } private static void solve(int t) { int n = in.nextInt(); String s = in.next(); int total = 0; for (int i = 0; i < s.length() ; i += 2) { if (s.charAt(i) != s.charAt(i + 1)) ++total; } int result = Math.min(minSegments(s , '0'), minSegments(s , '1')); System.out.println(total + " " + result); } private static int minSegments(String s, char prev) { char [] arr = s.toCharArray(); for (int i = 0; i < s.length(); i += 2) { if (s.charAt(i) != s.charAt( i + 1)) { arr[i] = prev; arr[i + 1] = prev; }else { prev = s.charAt(i); } } int result = 1; for (int i = 1; i < arr.length; ++i) { if (arr[i] != arr[i - 1]) ++result; } return result; } private static class IOHandler { BufferedReader br; StringTokenizer st; public IOHandler() { 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
8531e271b695687260153fd4eb12c6c4
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.*; /** * @author KaiXin * @version 11 * @date 2022-07-12 13:03 */ public class TaskA { static final int SIZE = (int) 2e5 + 5; static String s; static int[][] dp = new int[SIZE][2]; public static void solve(InputReader in,PrintWriter out){ int n = in.nextInt(); s = in.next(); char[] c = s.toCharArray(); int dif = 0; for(int i = 1; i < n; i += 2) { if(c[i] != c[i - 1]) dif++; } for(int i = 0; i <= n; ++i) Arrays.fill(dp[i],SIZE); if(c[0] == c[1]){ if(c[0] == '1') dp[1][1] = 1; else dp[1][0] = 1; } else { dp[1][1] = 1; dp[1][0] = 1; } for(int i = 3; i < n; i += 2){ if(c[i] == c[i - 1]){ int now = c[i] - '0'; if(dp[i - 2][now] != SIZE) dp[i][now] = dp[i - 2][now]; if(dp[i - 2][now ^ 1] != SIZE) dp[i][now] = Math.min(dp[i][now],dp[i - 2][now ^ 1] + 1); } else { if(dp[i - 2][1] != SIZE){ dp[i][1] = dp[i - 2][1]; dp[i][0] = dp[i - 2][1] + 1; } if(dp[i - 2][0] != SIZE){ dp[i][1] = Math.min(dp[i - 2][0] + 1,dp[i][1]); dp[i][0] = Math.min(dp[i - 2][0],dp[i][0]); } } } out.println(dif + " " + Math.min(dp[n - 1][0],dp[n - 1][1])); } public static void main(String[] args) throws FileNotFoundException { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int T = 1; T = in.nextInt(); for(int i = 1; i <= T; ++i){ solve(in,out); } out.close(); } private static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } /* 6 2 4 5 3 6 6 1 3 6 */
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
205aa5f4906887b2a7e416ca23f8473e
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 Q1678B { public static void main(String args[]) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); String a = s.next(); ArrayList<Integer> count = new ArrayList<>(); int c = 1; for(int i=1; i<=n-1; i++) { if(a.charAt(i)==a.charAt(i-1)) c++; else { count.add(c); c = 1; } } count.add(c); int ans1 = 0, ans2 = 1; ArrayList<Integer> pos = new ArrayList<Integer>(); boolean check = false; for(int i = 1; i<=count.size(); i++) { if(count.get(i-1)%2!=0) { check = !check; if(check) ans1++; if(count.get(i-1)>2) pos.add(i); } else { if(check) { ans1++; if(count.get(i-1)>2) pos.add(i); } else pos.add(i); } } for(int i = 1; i<=pos.size()-1; i++) { if((pos.get(i)-pos.get(i-1))%2!=0) ans2++; } //System.out.println(pos.toString()); System.out.println(ans1 + " " + ans2); } } }
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
f91d28a3a133e672b01af65e9f6fa706
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.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.security.cert.X509CRL; import java.util.*; import java.lang.*; import java.util.stream.Collector; import java.util.stream.Collectors; @SuppressWarnings("unused") public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static String OUTPUT = ""; //global private final static long BASE = 998244353L; private final static int INF_I = 1001001001; private final static long INF_L = 1001001001001001001L; private final static int MAXN = 100100; private static int decode(int a, int b) { return a + 2*b; } static void solve() { int ntest = readInt(); for (int test=0;test<ntest;test++) { int NS = readInt(); char[] S = readString().toCharArray(); int ans=0,ans2=0; int mark=-1; for (int i=0;i<NS;i+=2) { if (S[i]!=S[i+1]) ans+=1; else { if (S[i]==mark) continue; ans2+=1; mark = S[i]; } } out.println(ans + " " + (ans2+((mark==-1)?1:0))); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); if (INPUT=="") { is = System.in; } else { File file = new File(INPUT); is = new FileInputStream(file); } if (OUTPUT == "") out = new PrintWriter(System.out); else out = new PrintWriter(OUTPUT); solve(); out.flush(); long G = System.currentTimeMillis(); } private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> { private T x; private T y; public Point(T x, T y) { this.x = x; this.y = y; } public T getX() {return x;} public T getY() {return y;} @Override public int compareTo(Point<T> o) { int cmp = x.compareTo(o.getX()); if (cmp==0) return y.compareTo(o.getY()); return cmp; } } private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> { public ClassComparator() {} @Override public int compare(T a, T b) { return a.compareTo(b); } } private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> { public ListComparator() {} @Override public int compare(List<T> o1, List<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double readDouble() { return Double.parseDouble(readString()); } private static char readChar() { return (char)skip(); } private static String readString() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] readChar(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readTable(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = readChar(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = readInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i=0;i<n;i++) a[i] = readLong(); return a; } private static int readInt() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long readLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
Java
["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
9ee664f72b3508540f182f0d267f16c4
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.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); String s = br.readLine(); char start = s.charAt(0); int len = 1; int ans = 0; int part = 0; char c = '2'; for(int i=0;i<n;i+=2){ if(s.charAt(i)!=s.charAt(i+1)){ ans++; }else if(s.charAt(i)!=c){ part++; c = s.charAt(i); } } System.out.println(ans + " "+Math.max(1,part)); } } }
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
d37c6f57644913bf473020e1e40321cc
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.Arrays; import java.io.*; import java.util.*; import java.util.Random; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; /* Solution Created: 23:52:31 11/05/2022 Custom Competitive programming helper. */ public class Main { public static class Pair{ int f, s; public Pair(int f, int s) { this.f = f; this.s = s; } } public static Pair min(Pair a, Pair b) { if(a.f == b.f) { if(a.s < b.s) return a; return b; } if(a.f < b.f) return a; return b; } static void solve() { int n = in.nextInt(); char[] a = in.nca(); Pair[][] dp = new Pair[n+1][2]; //dp[i][j][k] up to i ending with char j for(int k = 0; k<2; k++) for(int i = 0; i<2; i++) for(int j = 0; j<2; j++) dp[k][i] = new Pair(Integer.MAX_VALUE/2, Integer.MAX_VALUE/2); for(int j = 0; j<2; j++) dp[0][j] = new Pair(0, 1); for(int i = 1; i<=n; i++) { boolean curZero = a[i-1]=='0'; dp[i][0] = new Pair(dp[i-1][0].f + (curZero?0:1), dp[i-1][0].s); dp[i][1] = new Pair(dp[i-1][1].f + (curZero?1:0), dp[i-1][1].s); if(i%2==1){ { Pair p1 = new Pair(dp[i-1][1].f + (curZero?0:1), dp[i-1][1].s + 1); dp[i][0] = min(dp[i][0], p1); } { Pair p1 = new Pair(dp[i-1][0].f + (curZero?1:0), dp[i-1][0].s + 1); dp[i][1] = min(dp[i][1], p1); } } } Pair ans = min(dp[n][0], dp[n][1]); out.println(ans.f+" "+ ans.s); } public static void main(String[] args) { in = new Reader(); out = new Writer(); int t = in.nextInt(); while (t-- > 0) solve(); out.exit(); } static Reader in;static Writer out; static class Reader { static BufferedReader br; static StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } public Reader(String f){ try { br = new BufferedReader(new FileReader(f)); } catch (IOException e) { e.printStackTrace(); } } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nd(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[] nca() { return next().toCharArray(); } public String[] ns(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int nextInt() { ensureNext(); return Integer.parseInt(st.nextToken()); } public double nextDouble() { ensureNext(); return Double.parseDouble(st.nextToken()); } public Long nextLong() { ensureNext(); return Long.parseLong(st.nextToken()); } public String next() { ensureNext(); return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); return null; } } private void ensureNext() { if (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } } } static class Util { private static Random random = new Random(); private static long MOD; static long[] fact, inv, invFact; public static void initCombinatorics(int n, long mod, boolean inversesToo, boolean inverseFactorialsToo) { MOD = mod; fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i < n + 1; i++) fact[i] = (fact[i - 1] * i) % mod; if (inversesToo) { inv = new long[n + 1]; inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (mod - (mod / i) * inv[(int) (mod % i)] % mod) % mod; } if (inverseFactorialsToo) { invFact = new long[n + 1]; invFact[n] = Util.modInverse(fact[n], mod); for (int i = n - 1; i >= 0; i--) { if (invFact[i + 1] == -1) { invFact[i] = Util.modInverse(fact[i], mod); continue; } invFact[i] = (invFact[i + 1] * (i + 1)) % mod; } } } public static long modInverse(long a, long mod) { long[] gcdE = gcdExtended(a, mod); if (gcdE[0] != 1) return -1; // Inverse doesn't exist long x = gcdE[1]; return (x % mod + mod) % mod; } public static long[] gcdExtended(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = gcdExtended(q, p % q); long tmp = vals[2]; vals[2] = vals[1] - (p / q) * vals[2]; vals[1] = tmp; return vals; } public static long nCr(int n, int r) { if (r > n) return 0; return (((fact[n] * invFact[r]) % MOD) * invFact[n - r]) % MOD; } public static long nPr(int n, int r) { if (r > n) return 0; return (fact[n] * invFact[n - r]) % MOD; } public 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 boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n + 1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i * i <= n; i++) if (isPrime[i]) for (int j = i; i * j <= n; j++) isPrime[i * j] = false; return isPrime; } static long pow(long x, long pow, long mod) { long res = 1; x = x % mod; if (x == 0) return 0; while (pow > 0) { if ((pow & 1) != 0) res = (res * x) % mod; pow >>= 1; x = (x * x) % mod; } return res; } public static int gcd(int a, int b) { int tmp = 0; while (b != 0) { tmp = b; b = a % b; a = tmp; } return a; } public static long gcd(long a, long b) { long tmp = 0; while (b != 0) { tmp = b; b = a % b; a = tmp; } return a; } public static int random(int min, int max) { return random.nextInt(max - min + 1) + min; } public static void dbg(Object... o) { System.out.println(Arrays.deepToString(o)); } public static void reverse(int[] s, int l, int r) { for (int i = l; i <= (l + r) / 2; i++) { int tmp = s[i]; s[i] = s[r + l - i]; s[r + l - i] = tmp; } } public static void reverse(int[] s) { reverse(s, 0, s.length - 1); } public static void reverse(long[] s, int l, int r) { for (int i = l; i <= (l + r) / 2; i++) { long tmp = s[i]; s[i] = s[r + l - i]; s[r + l - i] = tmp; } } public static void reverse(long[] s) { reverse(s, 0, s.length - 1); } public static void reverse(float[] s, int l, int r) { for (int i = l; i <= (l + r) / 2; i++) { float tmp = s[i]; s[i] = s[r + l - i]; s[r + l - i] = tmp; } } public static void reverse(float[] s) { reverse(s, 0, s.length - 1); } public static void reverse(double[] s, int l, int r) { for (int i = l; i <= (l + r) / 2; i++) { double tmp = s[i]; s[i] = s[r + l - i]; s[r + l - i] = tmp; } } public static void reverse(double[] s) { reverse(s, 0, s.length - 1); } public static void reverse(char[] s, int l, int r) { for (int i = l; i <= (l + r) / 2; i++) { char tmp = s[i]; s[i] = s[r + l - i]; s[r + l - i] = tmp; } } public static void reverse(char[] s) { reverse(s, 0, s.length - 1); } public static <T> void reverse(T[] s, int l, int r) { for (int i = l; i <= (l + r) / 2; i++) { T tmp = s[i]; s[i] = s[r + l - i]; s[r + l - i] = tmp; } } public static <T> void reverse(T[] s) { reverse(s, 0, s.length - 1); } public static void shuffle(int[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); int t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(long[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); long t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(float[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); float t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(double[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); double t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(char[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); char t = s[i]; s[i] = s[j]; s[j] = t; } } public static <T> void shuffle(T[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); T t = s[i]; s[i] = s[j]; s[j] = t; } } public static void sortArray(int[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(long[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(float[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(double[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(char[] a) { shuffle(a); Arrays.sort(a); } public static <T extends Comparable<T>> void sortArray(T[] a) { Arrays.sort(a); } public static int[][] rotate90(int[][] a) { int n = a.length, m = a[0].length; int[][] ans = new int[m][n]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ans[m - j - 1][i] = a[i][j]; return ans; } public static char[][] rotate90(char[][] a) { int n = a.length, m = a[0].length; char[][] ans = new char[m][n]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ans[m - j - 1][i] = a[i][j]; return ans; } } static class Writer { private PrintWriter pw; public Writer(){ pw = new PrintWriter(System.out); } public Writer(String f){ try { pw = new PrintWriter(new FileWriter(f)); } catch (IOException e) { e.printStackTrace(); } } public void yesNo(boolean condition) { println(condition?"YES":"NO"); } public void printArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); pw.println(); } public void printArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); pw.println(); } public void print(Object o) { pw.print(o.toString()); } public void println(Object o) { pw.println(o.toString()); } public void println() { pw.println(); } public void flush() { pw.flush(); } public void exit() { 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 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
4b0f1e621cdb593eb6dbbe0e3a300d99
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.security.Guard; import java.util.*; import java.util.List; import java.util.stream.IntStream; public class Main implements Runnable { public static final int LIMIT = 100010; int n, m; long k; static boolean use_n_tests = true; int[] a; void solve(FastScanner in, PrintWriter out, int testNumber) { int n = in.nextInt(); char[] s = in.nextc(); int ans = 0; char prev = '0'; for (int i = 0; i < n; i += 2) { if (s[i] == s[i + 1]) { prev = s[i]; break; } } for (int i = 0; i < n; i += 2) { if (s[i] == s[i + 1]) { prev = s[i]; } else { ans++; s[i] = s[i + 1] = prev; } } int ranges = 1; for (int i = 0; i < n; i++) { if (i - 1 >= 0 && s[i] != s[i - 1]) { ranges++; } } out.println(ans + " " + ranges); } long ans(int n) { long cnt = n / 2; long res = cnt * cnt * 2; if (n % 2 == 1) { res *= cnt * 4; } return res; } boolean isPolindrom(int a) { if (a < 10) { return true; } List<Integer> dig = getGidits(a); for (int i = 0; i < dig.size() / 2; i++) { if (!Objects.equals(dig.get(i), dig.get(dig.size() - 1 - i))) { return false; } } return true; } // ****************************** template code *********** static class SegmentTree { int[] delta; int n; int[][] cnt; public SegmentTree(int n) { this.n = n; delta = new int[4 * n + 10]; cnt = new int[4][4 * n + 10]; initCnts(0, 0, n - 1); } private void initCnts(int root, int rl, int rr) { if (rl != rr) { int rm = (rl + rr) / 2; initCnts(root * 2 + 1, rl, rm); initCnts(root * 2 + 2, rm + 1, rr); } updateCnts(root, rl, rr); } public void update(int left, int right, int by) { internalUpdate(0, 0, n - 1, left, right, by); } private void internalUpdate(int root, int rl, int rr, int left, int right, int by) { if (left > right) return; if (rl == left && rr == right) { delta[root] += by; updateCnts(root, rl, rr); } else { int rm = (rl + rr) / 2; internalUpdate(root * 2 + 1, rl, rm, left, Math.min(right, rm), by); internalUpdate(root * 2 + 2, rm + 1, rr, Math.max(rm + 1, left), right, by); updateCnts(root, rl, rr); } } private void updateCnts(int root, int rl, int rr) { if (delta[root] < 0) { throw new RuntimeException(); } for (int i = 0; i < 4; ++i) { cnt[i][root] = 0; } if (rl == rr) { ++cnt[Math.min(3, delta[root])][root]; } else { for (int i = 0; i < 4; ++i) { cnt[Math.min(3, i + delta[root])][root] += cnt[i][root * 2 + 1] + cnt[i][root * 2 + 2]; } } } public int getAnswer(int left, int right) { return internalGetAtMost(0, 0, n - 1, left, right, 2); } private int internalGetAtMost(int root, int rl, int rr, int left, int right, int most) { if (left > right || most < 0) return 0; if (rl == left && rr == right) { int res = 0; for (int i = 0; i <= most; ++i) { res += cnt[i][root]; } return res; } else { int rm = (rl + rr) / 2; return internalGetAtMost(root * 2 + 1, rl, rm, left, Math.min(right, rm), most - delta[root]) + internalGetAtMost(root * 2 + 2, rm + 1, rr, Math.max(rm + 1, left), right, most - delta[root]); } } } public static class DisjointSets { int[] p; int[] size; int sccCount; DisjointSets(int size) { p = new int[size]; this.size = new int[size]; for (int i = 0; i < size; i++) { this.size[i] = 1; p[i] = i; } sccCount = size; } public int root(int x) { return x == p[x] ? x : (p[x] = root(p[x])); } public void unite(int a, int b) { a = root(a); b = root(b); if (a == b) { return; } if (size[b] > size[a]) { int tmp = a; a = b; b = tmp; } size[a] += size[b]; p[b] = a; sccCount--; } int size(int id) { return size[root(id)]; } } static boolean use_file_insteadof_stdin = false; //System.getProperty("ONLINE_JUDGE") == null; static String input = "input.txt"; static String output = "output.txt"; List<Integer> getGidits(long n) { List<Integer> res = new ArrayList<>(); while (n != 0) { res.add((int) (n % 10L)); n /= 10; } return res; } List<Integer> generatePrimes(int n) { List<Integer> res = new ArrayList<>(); boolean[] sieve = new boolean[n + 1]; for (int i = 2; i <= n; i++) { if (!sieve[i]) { res.add(i); } if ((long) i * i <= n) { for (int j = i * i; j <= n; j += i) { sieve[j] = true; } } } return res; } int ask(int l, int r) { if (l >= r) { return -1; } System.out.printf("? %d %d\n", l + 1, r + 1); System.out.flush(); return in.nextInt() - 1; } static int stack_size = 1 << 27; class Mod { long mod; Mod(long mod) { this.mod = mod; } long add(long a, long b) { a = mod(a); b = mod(b); return (a + b) % mod; } long sub(long a, long b) { a = mod(a); b = mod(b); return (a - b + mod) % mod; } long mul(long a, long b) { a = mod(a); b = mod(b); return (a * b) % mod; } long div(long a, long b) { a = mod(a); b = mod(b); return (a * inv(b)) % mod; } public long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } private long mod(long a) { return a % mod; } } static class Coeff { long mod; long[][] C; long[] fact; boolean cycleWay = false; Coeff(int n, long mod) { this.mod = mod; fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = i; fact[i] %= mod; fact[i] *= fact[i - 1]; fact[i] %= mod; } } Coeff(int n, int m, long mod) { // n > m cycleWay = true; this.mod = mod; C = new long[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= Math.min(i, m); j++) { if (j == 0 || j == i) { C[i][j] = 1; } else { C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; C[i][j] %= mod; } } } } public long C(int n, int m) { if (cycleWay) { return C[n][m]; } return fC(n, m); } private long fC(int n, int m) { return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod; } private long fact(int n) { return fact[n]; } private long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } } class Pair { int first; long second; Pair(int f, long s) { first = f; second = s; } public int getFirst() { return first; } public long getSecond() { return second; } } class MultisetTree<T> { int size = 0; TreeMap<T, Integer> mp = new TreeMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } List<T> getElems() { List<T> res = new ArrayList<>(); for (T k : mp.keySet()) { int c = mp.get(k); for (int i = 0; i < c; i++) { res.add(k); } } return res; } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } boolean contains(T x) { return mp.containsKey(x); } T greatest() { return mp.lastKey(); } T smallest() { return mp.firstKey(); } int size() { return size; } int diffSize() { return mp.size(); } } class Multiset<T> { int size = 0; Map<T, Integer> mp = new HashMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } boolean contains(T x) { return mp.containsKey(x); } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } int size() { return size; } int diffSize() { return mp.size(); } } static class Range { int l, r; int id; public int getL() { return l; } public int getR() { return r; } public Range(int l, int r, int id) { this.l = l; this.r = r; this.id = id; } } static class Array { static Range[] readRanges(int n, FastScanner in) { Range[] result = new Range[n]; for (int i = 0; i < n; i++) { result[i] = new Range(in.nextInt(), in.nextInt(), i); } return result; } static List<List<Integer>> intInit2D(int n) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } return res; } static boolean isSorted(Integer[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static public long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static public long sum(long[] a) { long sum = 0; for (long x : a) { sum += x; } return sum; } static public long sum(Integer[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static public int min(Integer[] a) { int mn = Integer.MAX_VALUE; for (int x : a) { mn = Math.min(mn, x); } return mn; } static public int min(int[] a) { int mn = Integer.MAX_VALUE; for (int x : a) { mn = Math.min(mn, x); } return mn; } static public int max(Integer[] a) { int mx = Integer.MIN_VALUE; for (int x : a) { mx = Math.max(mx, x); } return mx; } static public int max(int[] a) { int mx = Integer.MIN_VALUE; for (int x : a) { mx = Math.max(mx, x); } return mx; } static public int[] readint(int n, FastScanner in) { int[] out = new int[n]; for (int i = 0; i < out.length; i++) { out[i] = in.nextInt(); } return out; } } class Graph { List<List<Integer>> graph; Graph(int n) { create(n); } private void create(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } this.graph = graph; } void readBi(int m, FastScanner in) { for (int i = 0; i < m; i++) { int v = in.nextInt() - 1; int u = in.nextInt() - 1; graph.get(v).add(u); graph.get(u).add(v); } } void add(int v, int u) { graph.get(v).add(u); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream io) { br = new BufferedReader(new InputStreamReader(io)); } public String line() { String result = ""; try { result = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return result; } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public char[] nextc() { return next().toCharArray(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public long[] nextArrayL(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = in.nextLong(); } return res; } public Long[] nextArrayL2(int n) { Long[] res = new Long[n]; for (int i = 0; i < n; i++) { res[i] = in.nextLong(); } return res; } public Integer[] nextArray2(int n) { Integer[] res = new Integer[n]; for (int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public long nextLong() { return Long.parseLong(next()); } } void run_t_tests() { int t = in.nextInt(); int i = 0; while (t-- > 0) { solve(in, out, i++); } } void run_one() { solve(in, out, -1); } @Override public void run() { if (use_file_insteadof_stdin) { try { in = new FastScanner(new FileInputStream(input)); // out = new PrintWriter(new FileOutputStream(output)); } catch (FileNotFoundException e) { e.printStackTrace(); } } else { in = new FastScanner(System.in); } out = new PrintWriter(System.out); if (use_n_tests) { run_t_tests(); } else { run_one(); } out.close(); } static FastScanner in; static PrintWriter out; public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(null, new Main(), "", stack_size); thread.start(); thread.join(); } }
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
8232c7a5783181d9f0212dbf2112844c
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 org.graalvm.compiler.phases.graph.FixedNodeProbabilityCache; //import org.graalvm.compiler.phases.graph.FixedNodeProbabilityCache; import java.io.*; import java.math.*; import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLIntegrityConstraintViolationException; public class Main { private static class MyScanner { private static final int BUF_SIZE = 2048; BufferedReader br; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } private boolean isSpace(char c) { return c == '\n' || c == '\r' || c == ' '; } String next() { try { StringBuilder sb = new StringBuilder(); int r; while ((r = br.read()) != -1 && isSpace((char)r)); if (r == -1) { return null; } sb.append((char) r); while ((r = br.read()) != -1 && !isSpace((char)r)) { sb.append((char)r); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Reader{ BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long mod_mul( long... a) { long ans = a[0]%mod; for(int i = 1 ; i<a.length ; i++) { ans = (ans * (a[i]%mod))%mod; } return ans; } static long mod_sum( long... a) { long ans = 0; for(long e:a) { ans = (ans + e)%mod; } return ans; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void print(long[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static void print(int[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2); } static long lcm(long a , long b) { return (a*b)/gcd(a, b); } static int lcm(int a , int b) { return (int)((a*b)/gcd(a, b)); } static long power(long x, long y){ if(y<0) return 0; long m = mod; if (y == 0) return 1; long p = power(x, y / 2) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; // factorial private long[] z1; // inverse factorial private long[] z2; // incerse number private long mod; public Combinations(long N , long mod) { this.mod = mod; z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return z1[(int)n]; } long ncr(long N, long R) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int max(int... a ) { int max = a[0]; for(int e:a) max = Math.max(max, e); return max; } static long max(long... a ) { long max = a[0]; for(long e:a) max = Math.max(max, e); return max; } static int min(int... a ) { int min = a[0]; for(int e:a) min = Math.min(e, min); return min; } static long min(long... a ) { long min = a[0]; for(long e:a) min = Math.min(e, min); return min; } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return min(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = gcd(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(0); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return min(left, right); } public void update(int index , int val) { arr[index] = val; update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } /* ***************************************************************************************************************************************************/ // static MyScanner sc = new MyScanner(); // only in case of less memory static Reader sc = new Reader(); static int TC; static StringBuilder sb = new StringBuilder(); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws IOException { int tc = 1; tc = sc.nextInt(); TC = 0; for(int i = 1 ; i<=tc ; i++) { TC++; // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.print(sb); } static void TEST_CASE() { int n = sc.nextInt(); String str = sc.next(); int[] arr = new int[n]; for(int i = 0 ;i<n ; i++) arr[i] = str.charAt(i)-'0'; int num = 0; int ans = 0; for(int i = 0 ;i<n ; ) { int v = arr[i]; while(i<n && v == arr[i]) { num++; i++; } if(num%2 == 1) ans++; num %= 2; } sb.append(ans+" "); int seg = 0; int prv = -1; for(int i= 0 ; i<n ;i+=2) { if(arr[i] == arr[i+1]) { if(arr[i]!=prv) { seg++; prv = arr[i]; } } } sb.append(max(seg , 1)+"\n"); } } /*******************************************************************************************************************************************************/ /** 3 10 12 3 2 11 12 3 2 10 13 3 2 10 12 14 3 2 3 3 2 3 4 0 4 */
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
e521d2269c7a98d4b846d0773a533f51
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 B1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test = sc.nextInt(); while(test -- > 0) { int n = sc.nextInt(); String s = sc.next(); long[] cut = calc(s,n); System.out.println(cut[0] +" " +cut[1]); } } static long []calc(String s, int n) { long ans = 0; long cut = 0; char p = '2'; for(int i=0; i<n; i+=2) { if (s.charAt(i) != s.charAt(i+1)) { ans++; } else { if (s.charAt(i) != p) { cut++; p = s.charAt(i); } } } return new long[]{ans, Math.max(cut, 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
bfa7208f658ab30211b74ec79dfd5281
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 Scanner obj = new Scanner(System.in); public static PrintWriter out = new PrintWriter(System.out); public static int i() { return obj.nextInt(); } public static void main(String[] args) { int len = i(); while (len-- != 0) { int n=obj.nextInt(); String s=obj.next(); Queue<Integer> q=new LinkedList<>(); int a=1; char[] ch=s.toCharArray(); for(int i=1;i<n;i++) { if(ch[i]==ch[i-1])a++; else { q.add(a); a=1; } } if(a>0)q.add(a); int prev=q.poll(); int ans=0; while(!q.isEmpty()) { if(prev%2!=0) { ans++; prev=q.poll()+1; } else prev+=q.poll(); } int last=-1,a2=0; for(int i=0;i<n;i+=2) { if(ch[i]==ch[i+1]) { if(last!=ch[i])a2++; last=ch[i]; } } a2=Math.max(a2, 1); out.println(ans+" "+a2); } 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
87a67abe5fdbf5569f46e7a718067b79
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
/** * @description: * @author: dzx * @date: 2022/5/3 */ import java.io.*; import java.util.*; public class Main2 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); String s = in.next(); String pre=""; int ans1 = 0, ans2 = 0; for (int i=0;i<s.length();i+=2) { String tmp = s.substring(i,i+2); if(!tmp.equals(pre)) { if(tmp.charAt(0)==tmp.charAt(1)){ ans2++; pre = tmp; }else{ ans1++; } } } if(pre.equals("")) ans2++; System.out.println(ans1+" "+ans2); } } static int[] codeZ(String s, String p) { // p[left:rightMax]=s[0:rightMax-left]---> left<=i<rightMax:p[i:rightMax]=s[i-left:rightMax-left] int n = p.length(), rightMax=0,left=0,m=s.length(); //z[i]=len:p[i:]与s的最长公共前缀,即p[i:i+len]=s[0:len] int[] z = new int[n]; int max = 0; long sum=0; // p,s相同时跳过z[0] if(p.equals(s)) { z[0] = n; rightMax = 1; left = 1; sum += n; } for (int i = rightMax; i < n; i++) { if (i < rightMax) { int rLen = rightMax - i,lLen=i-left; if(z[lLen]<rLen) z[i] = z[lLen]; else { while (rightMax-i<m&&rightMax < n && p.charAt(rightMax) == s.charAt(rightMax-i)) { rightMax++; left = i; } z[i] = rightMax - i; } }else{ int idx = 0; rightMax = i; while (idx<m&&rightMax < n && p.charAt(rightMax) == s.charAt(idx)) { rightMax++; idx++; } left = i; z[i] = rightMax - i; } max = Math.max(z[i], max); sum += z[i]; } return z; } String solution(int[]nums) { int last = nums.length-1; Deque<Integer> left = new ArrayDeque<>(); Deque<Integer> right = new ArrayDeque<>(); left.offer(Integer.MAX_VALUE); right.offer(Integer.MAX_VALUE); while (last > 0) { int min = Math.min(nums[last], nums[last - 1]); int max = Math.max(nums[last], nums[last - 1]); if(max>left.peekLast()||max> right.peekLast()) return "NO"; left.offer(min); right.offer(max); last -= 2; } if(last==0&&(nums[0]>left.peekLast()||nums[0]>right.peekLast())) return "NO"; return "YES"; } } static class ru_ifmo_niyaz_arrayutils_ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } } static class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } } static class net_egork_misc_ArrayUtils { public static long sumArray(int[] array) { long result = 0; for (int element : array) result += element; return result; } } static class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } }
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
9a5ac333f0666ef56502328359bab3e3
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 { InputStream is; PrintWriter out = new PrintWriter(System.out); ; String INPUT = ""; void run() throws Exception { is = System.in; solve(); out.flush(); out.close(); } public static void main(String[] args) throws Exception { new Main().run(); } public byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; 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++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() { // continue; String; charAt; println(); ArrayList; Integer; Long; // long; Queue; Deque; LinkedList; Pair; double; binarySearch; // s.toCharArray; length(); length; getOrDefault; break; // Map.Entry<Integer, Integer> e; HashMap; TreeMap; int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public double nd() { return Double.parseDouble(ns()); } public char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private int ni() { return (int) nl(); } private 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(); } } class Pair { int first; int second; Pair(int a, int b) { first = a; second = b; } } int[] na(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = ni(); return arr; } long[] nal(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nl(); return arr; } void solve() { int test_case = ni(); while (test_case-- > 0) { int n = ni(); String s = ""; do{s=ns();}while(s.length()!=n); char[] arr = s.toCharArray(); int cc = 0; int count = 1; ArrayList<Character> list = new ArrayList<>(); for(int i=0; i<n-1; i++) { if((arr[i] == '0' && arr[i+1] == '1')||(arr[i] == '1' && arr[i+1] == '0')) { cc++; } else { if(list.size() > 0) { if(s.charAt(i) != list.get(list.size()-1)) { count++; } } list.add(s.charAt(i)); list.add(s.charAt(i+1)); } i++; } out.println(cc + " " + 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 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
9c59b613e4bf33f758fe9873b7724a03
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 B2 { void go() { int n = Reader.nextInt(); String s = Reader.next(); char[] c = s.toCharArray(); int ans = 0; int cnt = 0; List<int[]> two = new ArrayList<>(); for(int i = 0; i < n; ) { int j = i; while(i < n && c[i] == c[j]) { i++; } int curLen = i - j; if(curLen % 2 == 1) { if(i - j == 1) { if(!two.isEmpty() && two.get(two.size() - 1)[1] + 1 == j) { two.get(two.size() - 1)[1] = i; } else { two.add(new int[] {j, i}); } } else { c[i] = c[j]; } ans++; i++; } } if(!two.isEmpty()) { for(int[] p : two) { char bit = '0'; int l = p[0]; int r = p[1]; if(l > 0) { bit = c[l - 1]; } else if(r < n - 1) { bit = c[r + 1]; } for(int j = l; j <= r; j++) { c[j] = bit; } } } for(int i = 0; i < n; ) { int j = i; while(i < n && c[i] == c[j]) { i++; } cnt++; } Writer.println(ans + " " + cnt); } void solve() { for(int T = Reader.nextInt(); T > 0; T--) go(); } void run() throws Exception { Reader.init(System.in); Writer.init(System.out); solve(); Writer.close(); } public static void main(String[] args) throws Exception { new B2().run(); } public static class Reader { public static StringTokenizer st; public static BufferedReader br; public static void init(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(""); } public static String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new InputMismatchException(); } } return st.nextToken(); } public static int nextInt() { return Integer.parseInt(next()); } public static long nextLong() { return Long.parseLong(next()); } public static double nextDouble() { return Double.parseDouble(next()); } } public static class Writer { public static PrintWriter pw; public static void init(OutputStream os) { pw = new PrintWriter(new BufferedOutputStream(os)); } public static void print(String s) { pw.print(s); } public static void print(char c) { pw.print(c); } public static void print(int x) { pw.print(x); } public static void print(long x) { pw.print(x); } public static void println(String s) { pw.println(s); } public static void println(char c) { pw.println(c); } public static void println(int x) { pw.println(x); } public static void flush() { pw.flush(); } public static void println(long x) { pw.println(x); } public static void close() { 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 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
efefa1d6167e987f463e7bf9058dbf2b
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 c1678; // // Codeforces Round #789 (Div. 2) 2022-05-08 07:35 // B2. Tokitsukaze and Good 01-String (hard version) // https://codeforces.com/contest/1678/problem/B2 // time limit per test 1 second; memory limit per test 256 megabytes // public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*' // // Tokitsukaze has a binary string s of length n, consisting only of zeros and ones, n is . // // Now Tokitsukaze divides s into of 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 <= i <= // n). Can you tell her the minimum number of operations to make s good? // // Input // // The first contains a single positive integer t (1 <= t <= 10,000)-- the number of test cases. // // For each test case, the first line contains a single integer n (2 <= n <= 2 * 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 * 10^5. // // Output // // 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. // // Example /* input: 5 10 1110011000 8 11001111 2 00 2 11 6 100110 output: 3 2 0 3 0 1 0 1 3 1 */ // Note // // In 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. // import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.Random; import java.util.StringTokenizer; public class C1678B2 { static final int MOD = 998244353; static final Random RAND = new Random(); static int[] solve(String s) { int n = s.length(); int[] ans = new int[2]; char[] ca = s.toCharArray(); // System.out.println(s); // dp[i][0] is (#ops, #segs) to make prefix [0,i] end with 0 int[][][] dp = new int[n][2][2]; dp[1][0][0] = (ca[0] == '0' ? 0 : 1) + (ca[1] == '0' ? 0 : 1); dp[1][0][1] = 1; dp[1][1][0] = (ca[0] == '1' ? 0 : 1) + (ca[1] == '1' ? 0 : 1); dp[1][1][1] = 1; for (int i = 3; i < n; i += 2) { char p = ca[i-1]; char c = ca[i]; if (p == '0' && c == '0') { // * * 0 0 0 0 or * * 1 1 0 0 // ^ ^ ^ ^ // i i dp[i][0][0] = Math.min(dp[i-2][0][0], dp[i-2][1][0]); dp[i][1][0] = 2 + Math.min(dp[i-2][0][0], dp[i-2][1][0]); } else if (p == '0' && c == '1') { // * * 0 0 0 1 or * * 1 1 0 1 dp[i][0][0] = 1 + Math.min(dp[i-2][0][0], dp[i-2][1][0]); dp[i][1][0] = 1 + Math.min(dp[i-2][0][0], dp[i-2][1][0]); } else if (p == '1' && c == '0') { dp[i][0][0] = 1 + Math.min(dp[i-2][0][0], dp[i-2][1][0]); dp[i][1][0] = 1 + Math.min(dp[i-2][0][0], dp[i-2][1][0]); } else { myAssert(p == '1' && c == '1'); // * * 0 0 1 1 or * * 1 1 1 1 // ^ ^ ^ ^ // i i dp[i][0][0] = 2 + Math.min(dp[i-2][0][0], dp[i-2][1][0]); dp[i][1][0] = Math.min(dp[i-2][0][0], dp[i-2][1][0]); } if (dp[i-2][0][0] < dp[i-2][1][0]) { dp[i][0][1] = dp[i-2][0][1]; } else if (dp[i-2][0][0] == dp[i-2][1][0]) { dp[i][0][1] = Math.min(dp[i-2][0][1], 1 + dp[i-2][1][1]); } else { dp[i][0][1] = 1 + dp[i-2][1][1]; } if (dp[i-2][0][0] < dp[i-2][1][0]) { dp[i][1][1] = 1 + dp[i-2][0][1]; } else if (dp[i-2][0][0] == dp[i-2][1][0]) { dp[i][1][1] = Math.min(1 + dp[i-2][0][1], dp[i-2][1][1]); } else { dp[i][1][1] = dp[i-2][1][1]; } } ans[0] = Math.min(dp[n-1][0][0], dp[n-1][1][0]); if (dp[n-1][0][0] < dp[n-1][1][0]) { ans[1] = dp[n-1][0][1]; } else if (dp[n-1][0][0] == dp[n-1][1][0]) { ans[1] = Math.min(dp[n-1][0][1], dp[n-1][1][1]); } else { ans[1] = dp[n-1][1][1]; } return ans; } static boolean test = false; static void doTest() { if (!test) { return; } long t0 = System.currentTimeMillis(); System.out.format("%d msec\n", System.currentTimeMillis() - t0); System.exit(0); } public static void main(String[] args) { doTest(); MyScanner in = new MyScanner(); int T = in.nextInt(); for (int t = 1; t <= T; t++) { int n = in.nextInt(); String s = in.next(); int[] ans = solve(s); System.out.format("%d %d\n", ans[0], ans[1]); } } static void myAssert(boolean cond) { if (!cond) { throw new RuntimeException("Unexpected"); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", ""); cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"]
1 second
["3 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
7d0dfddcd35abea3975ac7121e80f09c
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 Solutions { public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int testCases = Integer.parseInt(br.readLine()); while(testCases-->0){ int n=Integer.parseInt(br.readLine()); String s=br.readLine(); int one=0; int zero=0; int total=0; char arr[]=s.toCharArray(); int seg=0; int last_num=-1; int count=0; for(int i=0;i<n;i+=2) { if(arr[i]==arr[i+1]) { if(arr[i]-'0'!=last_num) { last_num=arr[i]-'0'; seg++; } }else count++; } out.println(count+" "+Math.max(1, seg)); } out.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
70cd94291a4d4efd88d44d072ac60673
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 C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); char[] s = sc.next().toCharArray(); int i = 0; int k = 1; int ct = 0; int L = -1; int y = 0; while(i<n-1) { if(s[i+1]!=s[i]) { if(k%2!=0) { ct++; k=1; i++; } else { i++; k=1; continue; } } else { if(L!=s[i]) y+=1; k++; L=s[i]; } i++; } System.out.println(ct+ " " + 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 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