Codeforces Round #754(Div.2)- B

Revision en2, by ptraffic114514, 2021-11-18 15:26:19

The problem : 1605B - Reverse Sort

This problem has $$$2$$$ traditions.

  1. This array had already been sorted.

Now the $$$m$$$ is $$$0$$$.

  1. This array had not been sorted.

The $$$m$$$ is $$$1$$$.

Make $$$k$$$ is the number of 1s.

if has one $$$i(0<i<n+1):$$$

$$$s_i=1\ and\ i<n-k+1 : $$$ We need to add $$$i$$$ to ans.

$$$s_i=0\ and\ i>n-k : $$$ We need to add $$$i$$$ to ans, too.

The code is very short:

#include <bits/stdc++.h>
using namespace std;
char s[1005];
int n;
int a[1005];
signed main () {
    int t;
    cin >> t;
    while (t --) {
        cin >> n >> s + 1;
        int is = 0;
        for (int i = 1;i <= n; ++ i) {
            if (s[i] == '1') is ++;
        }
        int cnt = 0;
        memset (a, 0, sizeof a);
        for (int i = 1;i <= n; ++ i) {
            if (s[i] == '1' && i <= n - is) a[++ cnt] = i;
            if (s[i] == '0' && i > n - is) a[++ cnt] = i;
        }
        if (cnt == 0) cout << 0 << endl;
        else {
            cout << 1 << endl;
            cout << cnt << ' ';
            for (int i = 1;i <= cnt; ++ i) {
                cout << a[i] << ' ';
            }
            cout << endl;
        }
    }
    return 0;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English ptraffic114514 2021-11-18 15:26:19 2 Tiny change: 'number of $1$s.\n\nif h' -> 'number of 1s.\n\nif h'
en1 English ptraffic114514 2021-11-18 15:23:08 1236 Initial revision (published)