fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. int n;
  10. cin >> n;
  11.  
  12. int a = 0, b = 0;
  13.  
  14. int cnt1 = 0, cnt2 = 0;
  15.  
  16. vector<int> arr(n);
  17.  
  18. for(int i = 0; i < n; i++){
  19. cin >> arr[i];
  20. }
  21.  
  22. for(int i = 0; i < n; i++){
  23. int x;
  24. cin >> x;
  25. if(x == 1 && arr[i] == 1)cnt1++;
  26. else if(x == -1 && arr[i] == -1)cnt2++;
  27. else{
  28. if(x == 0 && arr[i] == 1)a++;
  29. else if(x == 1 && arr[i] == 0)b++;
  30. else if(x == 1 && arr[i] == -1)b++;
  31. else if(x == -1 && arr[i] == 1)a++;
  32. }
  33. }
  34.  
  35. while(cnt1--){
  36. if(a > b)b++;
  37. else a++;
  38. }
  39.  
  40. while(cnt2--){
  41. if(a > b)a--;
  42. else b--;
  43. }
  44. cout << min(a, b) << "\n";
  45.  
  46. }
  47.  
  48. int main(){
  49. ios_base::sync_with_stdio(false);
  50. cin.tie(nullptr);
  51.  
  52. int t = 1;
  53. cin >> t;
  54.  
  55. for(int i = 1; i <= t; i++){
  56. solve();
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0.01s 5268KB
stdin
4
2
-1 1
-1 -1
1
-1
-1
5
0 -1 1 0 1
-1 1 0 0 1
4
-1 -1 -1 1
-1 1 1 1
stdout
0
-1
1
1