【文字游戏题】

【文字游戏题】

注意看题 题干可能会和你理解的“常理”不一样

Switch Seats

https://atcoder.jp/contests/abc399/tasks/abc399_d

/*
【文字游戏】
题目意思:
数一数有多少对夫妇一开始没有挨着坐,两对夫妇最后可以通过在那四个人之间交换座位而挨着坐。
->只能在两对情侣 4个人之间互换!
*/
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef pair<int,int> PII;
typedef long long ll;
ll abss(ll a){return a>0?a:-a;}
ll max_(ll a,ll b){return a>b?a:b;}
ll min_(ll a,ll b){return a<b?a:b;}
bool cmpll(ll a,ll b){return a>b;}
int t;
int n;
/*
【思路】
有且只有两种情况是有救的:
... a b ... a b ...
... a b ... b a ...
*/
void solve(){
      cin>>n;
      vector<int> a(2*n+1);
      for(int i=1;i<=2*n;i++) cin>>a[i];
      vector<int> pos[n+1];
      for(int i=1;i<=2*n;i++){
            pos[a[i]].push_back(i);
      }
      set<PII> q;
      for(int i=1;i<=2*n-1;i++){
            int x=a[i],y=a[i+1];
            int pos1=pos[x][0],pos2=pos[x][1],pos3=pos[y][0],pos4=pos[y][1];
            //注意先判相邻 如有则不动
            if(pos1+1==pos2) continue;
            if(pos3+1==pos4) continue;
            vector<int> res{pos1,pos2,pos3,pos4};
            sort(res.begin(),res.end());
            if(res[0]+1==res[1] && res[2]+1==res[3]){
                  q.insert(minmax(x,y));
            }
      }
      cout<<q.size()<<endl;
}
signed main(){
      ios::sync_with_stdio(0);
      cin.tie(0);
      cout.tie(0);
      cin>>t;
      while(t--) solve();
      return 0;
}
posted @ 2025-04-01 14:56  White_ink  阅读(12)  评论(0)    收藏  举报