trafik

Untitled

May 8th, 2022
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <string>
  5. #include <map>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <set>
  9. #define ll long long
  10. #define len(v) (int)v.size()
  11. #define all(v) v.begin(), v.end()
  12. const ll maxn = 2e5 + 10;
  13. const int logn = 20;
  14. const ll inf = 1e9;
  15. const int mod = 1e9 + 7;
  16. using namespace std;
  17.  
  18. int n;
  19. string s;
  20. string t;
  21.  
  22. int f1(bool c) {
  23.     t = "";
  24.     for (int i = 0; i < len(s) - 1; i += 2) {
  25.         if (s[i] != s[i + 1]) {
  26.             if (c)
  27.                 t += "11";
  28.             else
  29.                 t += "00";
  30.         }
  31.         else if (s[i] == '1') {
  32.             c = true;
  33.             t += s[i];
  34.             t += s[i + 1];
  35.         }
  36.         else if (s[i] == '0') {
  37.             c = false;
  38.             t += s[i];
  39.             t += s[i + 1];
  40.         }
  41.     }
  42.    
  43.     int bl = 0, curcnt = 0, curbl;
  44.     if (t[0] == '0') curbl = 1;
  45.     else curbl = 0;
  46.     for (int i = 0; i < len(t); ++i) {
  47.         if (t[i] - '0' == curbl)
  48.             continue;
  49.         else {
  50.             bl++;
  51.             curbl = t[i] - '0';
  52.         }
  53.     }
  54.    
  55.     //cout << t << endl;
  56.     return bl;
  57. }
  58.  
  59.  
  60. void solve() {
  61.     cin >> n;
  62.     cin >> s;
  63.    
  64.     int ans = 0;
  65.     for (int i = 0; i < n - 1; i += 2) {
  66.         if (s[i] != s[i + 1])
  67.             ++ans;
  68.     }
  69.     cout << ans << " " << min(f1(true), f1(false)) << endl;
  70.    
  71.  
  72.     t = "";
  73.    
  74. }
  75.  
  76. int main() {
  77.     ios::sync_with_stdio(0);
  78.     cin.tie(0);
  79.     cout.tie(0);
  80.  
  81.     int t; cin >> t;
  82.     while (t--) {
  83.         solve();
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment