求助(全RE)

P2249 【深基13.例1】查找

Wooden_head @ 2023-04-13 12:38:34

#include<bits/stdc++.h>
#define next 100000
using namespace std;
long long n,m,num1[next],num2[next];
int ef(int zs){
    int bs=0,es=n-1;
    while(bs<es){
        int zj=(es+bs)/2;
        if(num1[zj]>=num2[zs]) es=zj;
        else bs++;
    }
    if(num1[es]==num2[zs]) return es+1;
    else return 1;
}
int main(){
    cin>>n>>m;
    int j[m];
    for(int s=0;s<n;s++)
        cin>>num1[s];
    for(int s=0;s<m;s++){
        cin>>num2[s];
        j[s]=ef(s);
    }
    for(int s=0;s<m;s++)
        cout<<j[s]<<' ';
    return 0;
}

样例过了,提交就RE


by Wooden_head @ 2023-04-13 12:43:08

求助


by zjhzs666 @ 2023-04-13 12:47:51

@tanyouyu 你确定你样例过了?


by Wooden_head @ 2023-04-13 12:49:17

YES


by Wooden_head @ 2023-04-13 12:50:24

粘错了


by zjhzs666 @ 2023-04-13 12:50:26

#include<bits/stdc++.h>
using namespace std;
int n,q,a[1000001],l,r,s,m;
int f(int s){
    l=1;r=n+1;
    while(l<r){
        m=l+(r-l)/2;
     if(a[m]>=s)r=m;
        else l=m+1;
    }
    if(a[l]==s)return l;
    else return -1;
}
int main(){
    cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=q;i++){
        cin>>s;
        cout<<f(s)<<' ';
    }
}

拿去吧,可怜的孩子 @tanyouyu


by zjhzs666 @ 2023-04-13 12:51:06

@tanyouyu nb


by Wooden_head @ 2023-04-13 12:51:14

#include<bits/stdc++.h>
using namespace std;
int n,m,num1[100000],num2[10000];
int ef(int zs){
    int bs=0,es=n-1;
    while(bs<es){
        int zj=(es+bs)/2;
        if(num1[zj]>=num2[zs]) es=zj;
        else bs++;
    }
    if(num1[es]==num2[zs]) return es+1;
    else return -1;
}
int main(){
    cin>>n>>m;
    for(int s=0;s<n;s++)
        cin>>num1[s];
    for(int s=0;s<m;s++){
        cin>>num2[s];
        cout<<ef(s)<<' ';
    }
    return 0;
}

by liupan2010 @ 2023-04-13 12:51:31

#include<iostream>
#ifdef ONLINE_JUDGE
    #define getchar getchar_unlocked
#endif
int a[1000000];
inline int read()
{
    int get=0,w=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
            w=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        get=get*10+c-'0';
        c=getchar();
    }
    return get*w;
}
inline void write(int x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>=10)
        write(x/10);
    putchar(x%10+'0');
}
void find(int x,int n)
{
    int left=0,right=n,mid;
    while(left<right)
    {
        mid=(left+right)>>1;
        if(a[mid]>=x)
            right=mid;
        else
            left=mid+1;
    }
    if(a[left]!=x)
        printf("-1 ");
    else
    {
        write(left+1);
        fputs(" ",stdout);
    }
}
int main()
{
    int n=read(),m=read();
    for(register int i=0;i<n;i++)
        a[i]=read();
    for(register int i=0;i<m;i++)
    {
        int q=read();
        find(q,n);
    }
    return 0;
}

by zjhzs666 @ 2023-04-13 12:51:51

@tanyouyu 别复制,看不懂问我


by Wooden_head @ 2023-04-13 12:55:00

自己看时间 而且深入浅出里的···


| 下一页