样例过了,但全部wa,求调

P2249 【深基13.例1】查找

nizi15926 @ 2023-10-14 13:59:03

#include<bits/stdc++.h>
using namespace std;

int a[100001]={-1},b[100001],c;
int n,m,ans[100001];

void find(int x,int left,int right,int cixu)
{
   ans[cixu]=-1;

   while (left <= right) {
        int mid = (left + right) / 2;
        if(a[mid]==x)
            {
                ans[cixu]=b[mid];
                return;
            }

        if (a[mid] < x) {
            left = mid + 1;
        }
        else {
            right = mid-1;  
        }
    }
   return; 
}
int main()
{
    int q,k=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
      {
        cin>>q;
        if(q!=a[k])
          {
            a[++k]=q;
            b[k]=i;
          }
      }

    for(int i=1;i<=m;i++)
    {
         cin>>c;
         find(c,1,k,i);
    }    
    for(int i=1;i<=m;++i)
         cout<<ans[i]<<' ';
    cout<<endl;
    return 0;  
} 

by IAKIOI__ @ 2023-10-14 14:01:27

建议用lower_bound函数


|