Algorithm


B. Reverse Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
Problem link- https://codeforces.com/problemset/problem/1605/B
input
standard input
output
standard output

Ashish has a binary string s� of length n� that he wants to sort in non-decreasing order.

He can perform the following operation:

  1. Choose a subsequence of any length such that its elements are in non-increasing order. Formally, choose any k� such that 1≤k≤n1≤�≤� and any sequence of k� indices 1≤i1<i2<…<ik≤n1≤�1<�2<…<��≤� such that si1≥si2≥…≥sik��1≥��2≥…≥���.
  2. Reverse this subsequence in-place. Formally, swap si1��1 with sik���, swap si2��2 with sik−1���−1, …… and swap si⌊k/2⌋��⌊�/2⌋ with si⌈k/2⌉+1��⌈�/2⌉+1 (Here ⌊x⌋⌊�⌋ denotes the largest integer not exceeding x�, and ⌈x⌉⌈�⌉ denotes the smallest integer not less than x�)

Find the minimum number of operations required to sort the string in non-decreasing order. It can be proven that it is always possible to sort the given binary string in at most n� operations.

Input

The first line contains a single integer t� (1≤t≤1000)(1≤�≤1000)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n� (1≤n≤1000)(1≤�≤1000)  — the length of the binary string s�.

The second line of each test case contains a binary string s� of length n� containing only 00s and 11s.

It is guaranteed that the sum of n� over all test cases does not exceed 10001000.

Output

For each test case output the following:

  • The minimum number of operations m� in the first line (0≤m≤n0≤�≤�).
  • Each of the following m� lines should be of the form: k� i1�1 i2�2 ... ik��, where k� is the length and i1<i2<...<ik�1<�2<...<�� are the indices of the chosen subsequence. For them the conditions from the statement must hold.
Example
input
Copy
3
7
0011111
5
10100
6
001000
output
Copy
0
1
4 1 3 4 5 
1
3 3 5 6 
Note

In the first test case, the binary string is already sorted in non-decreasing order.

In the second test case, we can perform the following operation:

  • k=4:�=4: choose the indices {1,3,4,5}{1,3,4,5}

    1–1_ 00 1–1_ 0–0_ 0–0_ →→ 0–0_ 00 0–0_ 1–1_ 1–1_

In the third test case, we can perform the following operation:

  • k=3:�=3: choose the indices {3,5,6}{3,5,6}

    00 00 1–1_ 00 0–0_ 0–0_ →→ 00 00 0–0_ 00 0–0_ 1–1_

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include<bits/stdc++.h>
using namespace std;


#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    string s, k;
    cin>>s;
    k = s;
    sort(s.begin(), s.end());
    int arr[n];
    int m = 0;
    for(int i = 0; i < s.length(); i++){
      if(k[i] != s[i]){
        arr[m] = i+1;
        m++;
      }
    }
    if(m != 0){
      cout<<1<<endl;
    }
    cout<<m<<" ";
    for(int i = 0; i < m; i++){
      cout<<arr[i]<<" ";
    }
    cout<<endl;
  }
}
Copy The Code & Try With Live Editor

Input

x
–
+
cmd
3
7
0011111
5
10100
6
001000

Output

x
–
+
cmd
0
1
4 1 3 4 5
1
3 3 5 6
Advertisements

Demonstration


Codeforcess Solution 1605-B B. Reverse Sort ,C++, Java, Js and Python ,1605-B,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+