fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve(){
  5.  
  6. int n;
  7. cin>>n;
  8. vector <int> a(n);
  9.  
  10. int i =0;
  11. int j =0;
  12. int count = 0;
  13.  
  14. for(int x = 0; x < n; x++){
  15. cin>>a[x];
  16. }
  17. for(int x = 0; x < n; x++){
  18. if(a[x] == -1){
  19. i++;
  20. }else if(a[x] == 1){
  21. j++;
  22. }
  23. }
  24.  
  25. while(i>j){
  26. i--;
  27. j++;
  28. count++;
  29. }
  30.  
  31. // cout<<i<<" "<<j<<endl;
  32. if( j-i>=0 && i%2 == 0 ){
  33. cout << count <<endl;
  34. }else cout<<count+1<<endl;
  35.  
  36. }
  37.  
  38. int main() {
  39. int t;
  40. cin>>t;
  41. while(t--){
  42. solve();
  43. }
  44. return 0;
  45. }
Success #stdin #stdout 0s 5292KB
stdin
7
4
-1 -1 1 -1
5
-1 -1 -1 1 1
4
-1 1 -1 1
3
-1 -1 -1
5
1 1 1 1 1
1
-1
2
-1 -1
stdout
1
1
0
3
0
1
2