test1WA,test2~5AC,test6TLE,求解

P2249 【深基13.例1】查找

start_672 @ 2024-04-12 22:50:44

这个二分,test1WA,test2~5AC,test6TLE,求解

#include<bits/stdc++.h>
using namespace std;
int n,m,a[1000001];
int halfandfind(int le,int ri,int mi,int number){
    if(a[le]==number)for(int i=le;;i--)if(a[i]!=number)return i+1;
    if(a[mi]==number)for(int i=mi;;i--)if(a[i]!=number)return i+1;
    if(a[ri]==number)for(int i=ri;;i--)if(a[i]!=number)return i+1;
    if(a[mi]>number&&a[ri]>number&&a[le]<number)return halfandfind(le,mi-1,(mi-1+le)/2,number);
    if(a[mi]<number&&a[ri]>number&&a[le]<number)return halfandfind(mi+1,ri,(mi+1+ri)/2,number);
    return -1;
}
int main(){
    cin>>n>>m;
    int willfindnumbers[m+1]={0},where[m+1]={0};
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=m;i++)cin>>willfindnumbers[i];
    for(int i=1;i<=m;i++)where[i]=halfandfind(1,n,(1+n)/2,willfindnumbers[i]);
    for(int i=1;i<=m;i++)cout<<where[i]<<" ";
    return 0;
}

by start_672 @ 2024-04-12 22:51:10

P2249


|