全部TLE,求改!!!
查看原帖
全部TLE,求改!!!
921534
LiuYunfan5177楼主2024/7/27 21:21

全部TLE,求改!!!

//全部TLE,求改!!!
#include<bits/stdc++.h>
using namespace std;
int n,m,x;//数字个数、访问次数、访问的数字 
int a[1000005];
int check(int mid){
	int ter=-1; 
	for(int i=1;i<=n;i++){
		if(a[i]==x){
			ter=i;
			break;
		}
	}
	if(ter!=-1){
		if(ter>=mid){
			return 1;
		}else{
			return 0;
		}
	}else{
		return 2;
	}
}
int main(){
	int maxx=-1,minn=INT_MAX;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		maxx=max(maxx,a[i]);
		minn=min(minn,a[i]);
	}
	while(m--){
		cin>>x;
		bool flag=0;
		int l=minn,r=maxx,mid;
		while(l+1<r){
			mid=(l+r)/2;
			int t=check(mid);
			if(t==1){
				l=mid;
			}else if(t==0){
				r=mid;
			}else{
				flag=1;
				break;
			}
		}
		if(!flag){
			cout<<l<<' ';
		}else{
			cout<<"-1 ";
		}
	}
	return 0;
}
//写的如Sh*t一般,求改!

谢谢

2024/7/27 21:21
加载中...