D. Pairs
题目链接:https://codeforces.ml/problemset/problem/1463/D
题意:有1~2n 2n个整数 两两匹配 问有多少种取x次 其中的最小值 和n-x次 其中的最大值 得到b序列
思路: 用set模拟得到最多能用几次 x 和最少能用几次x 那么中间的 范围都可以贪心得到一定存在
所以 最终的答案即是R-L+1 的数
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn =4e5+10; 6 const int mod=1e9+7; 7 int b[maxn],vis[maxn]; 8 9 10 11 12 int main() 13 { 14 ios::sync_with_stdio(false); 15 cin.tie(0); 16 int t; 17 cin>>t; 18 while(t--) 19 { 20 int n; 21 cin>>n; 22 for(int i=1;i<=n*2;i++) 23 vis[i]=0; 24 for(int i=1;i<=n;i++) 25 { 26 cin>>b[i]; 27 vis[b[i]]=1; 28 } 29 set<int>s1,s2; 30 for(int i=1;i<=n*2;i++) 31 { 32 if(!vis[i]) 33 { 34 s1.insert(i); 35 s2.insert(i); 36 } 37 } 38 int l=0,r=0; 39 for(int i=1;i<=n;i++) 40 { 41 auto it1=s1.upper_bound(b[i]); 42 if(it1!=s1.end()) 43 { 44 s1.erase(it1); 45 r++; 46 } 47 auto it2=s2.upper_bound(b[i]); 48 if(it2!=s2.begin()) 49 { 50 it2--; 51 s2.erase(it2); 52 l++; 53 } 54 } 55 l=n-l; 56 int ans=r-l+1; 57 cout<<ans<<'\n'; 58 59 } 60 61 62 63 }

浙公网安备 33010602011771号