fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. int t;
  7. cin>>t;
  8. while(t--){
  9. int n;
  10. cin>>n;
  11. string s;
  12. cin>>s;
  13. queue<pair<int,string>> q;
  14.  
  15. for(int i = 0;i<s.size();i++) {
  16. pair <int, string> team ;
  17. if(i == 0 || i%2 == 0){
  18. team = make_pair(0,s[i]);
  19. q.push(team); // b team
  20. } else {
  21. team = make_pair(1,s[i]);
  22. q.push(team); //a team
  23. }
  24. }
  25.  
  26. int countA = 0;
  27. int countB = 0;
  28. int remA = 0;
  29. int remB = 0;
  30. int count =0;
  31. bool flag = true;
  32. while(!q.empty()){
  33. count++;
  34. pair <int, string> team ;
  35. team = q.front();
  36. q.pop();
  37. if(team.first == 0) {
  38. remB++;
  39. if(team.second == "1"){
  40. countB++;
  41. }
  42. } else {
  43. remA++;
  44. if(team.second == "1"){
  45. countA++;
  46. }
  47. }
  48.  
  49. if(countB - countA > n-remA){
  50. flag = false;
  51. cout<<count<<endl;
  52. break;
  53. }
  54.  
  55. if(countA - countB > n-remB){
  56. flag = false;
  57. cout<<count<<endl;
  58. break;
  59. }
  60. }
  61.  
  62. if(flag){
  63. cout<<s.size()<<endl;
  64. }
  65.  
  66. }
  67.  
  68.  
  69. // your code goes here
  70. return 0;
  71. }
Success #stdin #stdout 0s 4160KB
stdin
2
3
101011
3
100001
stdout
4
6