fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. #define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
  7. #define print2(a,x,y) for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
  8. #define printInf(a) for(auto x : a) { if(x==INT32_MAX) cerr << "∞ "; else if(x==INT32_MIN) cerr << "-∞ "; else cerr << x << " "; } cerr << endl
  9. #ifndef ONLINE_JUDGE
  10. #endif
  11. inline int power(int a, int b) {
  12. int x = 1;
  13. while (b) {
  14. if (b & 1) x *= a;
  15. a *= a;
  16. b >>= 1;
  17. }
  18. return x;
  19. }
  20.  
  21.  
  22. const int M = 1000000007;
  23. const int N = 3e5+9;
  24. const int INF = 2e9+1;
  25. const int LINF = 2000000000000000001;
  26.  
  27. //_ ***************************** START Below *******************************
  28.  
  29.  
  30.  
  31.  
  32. vector<int> a;
  33. void consistency(int n){
  34.  
  35. int ones = 0;
  36. for(int i=0; i<n; i++) if(a[i] == 1) ones++;
  37. if(ones==0){
  38. cout << -1 << endl;
  39. return;
  40. }
  41.  
  42. int s= 0, e =0;
  43. int ans = n+1, zeroes = 0;
  44. while(e<n){
  45. if(a[e] == 0) zeroes++;
  46. if(e-s+1<ones){
  47. e++;
  48. }
  49. else{
  50. ans = min(ans, zeroes);
  51. if(a[s] == 0) zeroes--;
  52. s++;
  53. e++;
  54. }
  55. }
  56. if(ans==n+1){
  57. cout << -1 << endl;
  58. return;
  59. }
  60. cout << ans << endl;
  61. }
  62.  
  63.  
  64. void solve() {
  65.  
  66.  
  67. int n;
  68. cin>>n;
  69. a.resize(n+1);
  70. for(int i=1; i<=n; i++) cin >> a[i];
  71. consistency(n) ;
  72.  
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80. int32_t main() {
  81. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  82.  
  83. // #ifndef ONLINE_JUDGE
  84. // freopen("inputf.txt", "r", stdin);
  85. // freopen("outputf.txt", "w", stdout);
  86. // #endif
  87.  
  88. int t = 1;
  89. cin >> t;
  90. while (t--) {
  91. solve();
  92. }
  93.  
  94. return 0;
  95. }
Success #stdin #stdout 0.01s 5288KB
stdin
2
6
1 0 1 0 1 1
3
0 0 0
stdout
1
-1