fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. //*****************************
  7. #define ll long long
  8. #define endl "\n"
  9. #define mod 1000000007
  10. #define cases long int t;cin>>t;while(t--)
  11. #define pb push_back
  12. #define vi vector<int>
  13. #define vli vector<long int>
  14. #define vll vector<long long>
  15. #define all(v) v.begin(),v.end()
  16. //******************************
  17.  
  18.  
  19.  
  20. int main()
  21. {
  22.  
  23. ios_base::sync_with_stdio(false);
  24. cin.tie(NULL);
  25.  
  26. cases
  27. {
  28. ll n,i;
  29.  
  30. cin>>n;
  31.  
  32. int a[n];
  33.  
  34. for(i=0;i<n;i++)
  35. {
  36. cin>>a[i];
  37.  
  38. }
  39. ll x=1;
  40. vll v;
  41. bool f1=false,f2=true;
  42.  
  43. ll mx=0,one=0;
  44.  
  45.  
  46. for(i=0;i<n-1;i++)
  47. {
  48. if(a[i]==0)
  49. {
  50.  
  51. if(a[i]==a[i+1])
  52. x++;
  53.  
  54. else
  55. {
  56. v.pb(x);
  57.  
  58.  
  59. if(x%2!=0&&x!=1)
  60. f1=true;
  61.  
  62. if(mx<x&&x%2!=0&&x!=1)
  63. {
  64. mx=x;
  65.  
  66. }
  67. if(x==1)
  68. one++;
  69. if(x>1)
  70. f2=false;
  71. x=1;
  72.  
  73. }
  74.  
  75. }
  76.  
  77.  
  78. }
  79.  
  80.  
  81. if(a[n-1]==0){
  82. if(x%2!=0&&x!=1)
  83. f1=true;
  84.  
  85. if(mx<x&&x%2!=0&&x!=1)
  86. {
  87. mx=x;
  88.  
  89. }
  90. if(x==1)
  91. one++;
  92. if(x>1)
  93. f2=false;
  94. v.pb(x);
  95. }
  96. /*
  97.   cout<<mx<<endl;
  98.   n=v.size();
  99.   for(i=0;i<n;i++)
  100.   cout<<v[i]<<' ';
  101.   cout<<endl;
  102.   */
  103.  
  104. if(f2&&one>0)
  105. {
  106. //cout<<one<<endl;
  107. if(one%2!=0)
  108. cout<<"Yes"<<endl;
  109. else
  110. cout<<"No"<<endl;
  111. continue;
  112. }
  113.  
  114.  
  115. if(f1)
  116. {
  117. x=0;
  118. n=v.size();
  119. f1=true;
  120. for(i=0;i<n;i++)
  121. {
  122.  
  123. if(v[i]==mx)
  124. {v.erase(v.begin()+i);
  125. //cout<<v[i]<<" ";
  126. break;
  127. }
  128. }
  129. n=v.size();
  130. for(i=0;i<n;i++)
  131. {
  132.  
  133.  
  134. if(v[i]>mx/2)
  135. {
  136. f1=false;
  137. break;
  138. }
  139. }
  140.  
  141. if(f1)
  142. cout<<"Yes";
  143. else
  144. cout<<"No";
  145. }
  146. else
  147. {
  148. cout<<"No";
  149. }
  150. cout<<endl;
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. return 0;
  159.  
  160. }
  161.  
Success #stdin #stdout 0s 4360KB
stdin
2
11
1 0 0 0 0 0 1 0 0 0 1
4
0  0 0 0 
stdout
No
No