LiuYunfan5177 @ 2024-07-27 21:21:30
全部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一般,求改!
谢谢
by LiuYunfan5177 @ 2024-07-27 21:22:53
只有样例过了
by haimingbei @ 2024-07-27 21:25:06
@LiuYunfan5177
代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,a[1000005],b[100005],t;
int two(int x){
int lt=1,rt=n;
while (lt<rt){
int mid=lt+(rt-lt)/2;
if (a[mid]>=x) rt=mid;
else lt=mid+1;
}
if (a[lt]==x) return lt;
else return -1;
}
int main(){
cin>>n>>m;
for (int i=1;i<=n;i++)cin>>a[i];
for (int i=1;i<=m;i++)cin>>b[i];
for(int i=1;i<=m;i++){
cout<<two(b[i])<<" ";
}
return 0;
}
by Gron_Lee_kkk @ 2024-08-03 17:54:23
要用scanf cin会超时
by return_second @ 2024-08-09 11:12:37
本题输入输出量较大,请使用较快的 IO 方式。