fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int long long
  6. #define nn "\n"
  7. #define pi pair<int, int>
  8. #define fi first
  9. #define se second
  10. #define lb lower_bound
  11. #define ub upper_bound
  12. #define all(a) (a).begin(), (a).end()
  13. #define pb push_back
  14. #define eb emplace_back
  15.  
  16. signed main() {
  17. ios::sync_with_stdio(false);
  18. cin.tie(nullptr);
  19.  
  20. int t;
  21. cin >> t;
  22.  
  23. while (t--) {
  24. int n;
  25. string s;
  26. cin >> n >> s;
  27.  
  28. if (s[0] == '1') {
  29. int ans = 0;
  30.  
  31. for (int i = 1; i < n; i++) {
  32. if (s[i] == '0') ans++;
  33. }
  34.  
  35. cout << ans << nn;
  36. continue;
  37. }
  38.  
  39. int total1 = 0;
  40. for (char c : s) {
  41. if (c == '1') total1++;
  42. }
  43.  
  44. int ans = total1;
  45.  
  46. int before1 = 0;
  47. int after0 = 0;
  48.  
  49. for (int i = 0; i < n; i++) {
  50. if (s[i] == '0') after0++;
  51. }
  52.  
  53. for (int i = 0; i < n; i++) {
  54. if (s[i] == '1') {
  55. ans = min(ans, before1 + after0);
  56. before1++;
  57. } else {
  58. after0--;
  59. }
  60. }
  61.  
  62. cout << ans << nn;
  63. }
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 5300KB
stdin
6
4
0011
4
1000
5
01000
8
01001101
7
0101010
7
0111101
stdout
0
3
1
2
3
1