80超时求助

P2249 【深基13.例1】查找

yuyuelong @ 2023-11-04 23:02:22

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const long long ap=1e6+5;
long long x[ap],y[ap];
int main(){
    int a,b;
    int mid;
    int t,z;
    cin>>a>>b;
    for(int i=0;i<a;i++){
        cin>>x[i];
    }
    for(int i=0;i<b;i++){
        cin>>y[i];
        t=0;
        z=a-1;
    while(t<z){
        mid=(t+z)/2;
        if(x[mid]>y[i]){
            z=mid;
        }
        else if(x[mid]<y[i]){
            t=mid+1;
        }
        else{
            break;
        }
    }
    if(x[mid]==y[i]){
        if(mid!=0){
            while(x[mid-1]==x[mid]&&mid>0){
                mid-=1;
            }
        }

        cout<<mid+1<<" ";
    }
    else{
        cout<<"-1"<<" ";
    }
}
cout<<endl;
    return 0;
} 

最后一个计分点超时了,求大佬给点优化方案


by LFRED2023 @ 2023-11-05 14:29:37

题目里说了:建议使用较快的in/out方式,建议你去网上找快读快写模板学习一下


|