P1:题意
给定 和大小为 的集合 。原先存在大小为 的集合 ,将 中元素升序排列,依次与不在 中的最小数配对。配对完后,不在 中的最小未匹配数必须恰好等于 。求出所有可能的 。
P2:思路
将 升序排列为 。 假设 ,则初始集合 ,大小为 。在 中,每个数与 中最小的未匹配数配对。这个过程会消耗 中最小的 个数,因此配对完成后,剩下的最小数就是 中第 小的数。 题目要求它恰好是 ,即 必须是 中第 小的数。 由于 ,所以 。要使 在 中排名第 ,严格小于 且在 中的数必须恰有 个。也就是 中不属于 的数恰好有 个。 在 中,属于 的有 个(即 ),因此不属于 的有 个。 令 ,化简得 。 因此,遍历 的所有元素,若 ,则 是一个合法答案。
P3:实现
不要复制代码
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T;cin>>T;
while(T--){
int n,m;cin>>n>>m;
vector<int> a(m),ans;
for(int i=0;i<m;i++){
cin>>a[i];
if(a[i]==i+m)ans.push_back(a[i]);
}
cout<<ans.size()<<"\n";
for(int i=0;i<ans.size();i++)cout<<ans[i]<<(i==ans.size()-1?"":" ");
cout<<"\n";
}
return 0;
}- 时间复杂度:
- 空间复杂度: