pdpd123

Problem 5

Feb 17th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
  4. using namespace std;
  5.  
  6. const int maxN = 2e5 + 226;
  7.  
  8. int ttttttt, N, arr[maxN];
  9.  
  10. int main(){
  11.     ericxiao;
  12.     cin >> ttttttt;
  13.     while(ttttttt--){
  14.         cin >> N;
  15.         int totones = 0, tottwos = 0;
  16.         for(int i = 0; i < 2 * N; i++){
  17.             cin >> arr[i];
  18.             if(arr[i] == 1) totones++;
  19.             else if(arr[i] == 2) tottwos++;
  20.         }
  21.         map<int,int> mp;
  22.         int ones = 0, twos = 0, cnt = 0;
  23.         mp[totones] = 0;
  24.         for(int i = N - 1; i >= 0; i--){
  25.             cnt++;
  26.             if(arr[i] == 1) ones++;
  27.             else if(arr[i] == 2) twos++;
  28.             if(mp.count(totones + twos - ones)) continue;
  29.             mp[totones + twos - ones] = cnt;
  30.         }
  31.         ones = twos = cnt = 0;
  32.         int ans = 2 * N;
  33.         if(mp.count(tottwos)){
  34.             ans = min(ans, mp[tottwos]);
  35.         }
  36.         for(int i = N; i < 2 * N; i++){
  37.             cnt++;
  38.             if(arr[i] == 1) ones++;
  39.             else if(arr[i] == 2) twos++;
  40.             if(mp.count(tottwos + ones - twos)) ans = min(ans, mp[tottwos + ones - twos] + cnt);
  41.         }
  42.         cout << ans << endl;
  43.     }
  44. }
Add Comment
Please, Sign In to add comment