fork download
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5. void solve() {
  6. int n;
  7. cin>>n;
  8. vector<int> b(n);
  9. map<int,int> mp;
  10. for(int i=0;i<n;i++){
  11. int x;
  12. cin>>x;
  13. mp[x]++;
  14. }
  15. for(int i=0;i<n;i++){
  16. cin>>b[i];
  17. }
  18. int i=0,j=0;
  19. int cnt=0;
  20. set<pair<int,int>> sp;
  21. while(mp.size()!=0 && j<n){
  22. bool f=false;
  23. for(auto z:mp){
  24. int z1=z.first;
  25. pair<int,int> p1={z1,b[j]};
  26. pair<int,int> p2={b[j],z1};
  27. if(sp.find(p1)==sp.end() && sp.find(p2)==sp.end()){
  28. f=true;
  29. j++;
  30. mp[z1]--;
  31. sp.insert(p1),sp.insert(p2);
  32. if(mp[z1]==0){
  33. mp.erase(z1);
  34. }
  35. cnt++;
  36. break;
  37. }
  38. }
  39. if(!f){
  40. j++;
  41. }
  42.  
  43. }
  44. if(cnt>=3){
  45. cout<<"YES"<<endl;
  46. }
  47. else{
  48. cout<<"NO"<<endl;
  49. }
  50.  
  51. }
  52.  
  53. signed main() {
  54. int t;
  55. cin >> t;
  56. while (t--) {
  57. solve();
  58. }
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0.01s 5288KB
stdin
5
4
1 2 1 2
1 2 1 2
6
1 2 3 3 2 1
1 1 1 1 1 1
3
1 1 1
1 1 1
6
1 52 52 3 1 3
59 4 3 59 3 4
4
100 1 100 1
2 2 2 2
stdout
YES
YES
NO
YES
NO