只有20分啊,大佬为什么
查看原帖
只有20分啊,大佬为什么
1552231
y1zi楼主2025/1/9 21:24

#include<bits/stdc++.h>
#define lowbit(x) ((x)&-(x)) 
using namespace std;
typedef long long ll;
const int N=1e5+1;
ll n,m; 
ll a[N],ans[N];
bool find(ll x){
	ll left=1,right=n;
	while(left<right){
		ll mid=left+(right-left)/2;
		if(a[mid]>=x){
		right=mid;
		} else {
		left=mid+1;
		}	
	}
	if(a[left]==x){return true;}
	else return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	//排序
	sort(a+1,a+1+n);
	
	ll cnt=0;
	for(int i=1;i<=m;i++){
		ll y;
		cin>>y;
		if(find(y)){ans[++cnt]=y;}
	}	
	sort(ans+1,ans+1+cnt);
	
	for(int i=1;i<=cnt;i++){
		cout<<ans[i]<<" ";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	return 0 ;
}

2025/1/9 21:24
加载中...