满江红求调,感谢

P2249 【深基13.例1】查找

Ryoo @ 2024-10-20 11:14:04

#include <bits/stdc++.h>
using namespace std;
int a[10000005], ans[100005];
char c[15];
int n,m;
int read() {
    char chr;
    int s = 0, w = 1;
    chr = getchar();
    if(chr == '-') {w = -1;chr = getchar();}
    while(chr >= '0' && chr <= '9') {
        s = s*10 + (chr-'0');
        chr = getchar();
    }
    return s*w;
}
void write(int x) {
    int len = 0, k1 = x;
    if(k1 < 0) {
        putchar('-');k1 = -k1;
    }
    while(k1) c[len++] = k1%10+'0',k1/=10;
    while(len--) putchar(c[len]);
}
int search(int x) {
    int l = 1, r = n,mid = (l+r)/2;
    while(l < r) {
        if(a[mid] >= x) {r = mid;mid = (l+r)/2;}
        else if(a[mid] < x) {l = mid + 1;mid = (l+r)/2;}

    }
    if(a[l] == x) return l;
    else return -1;
}
int main() {

    n = read();m = read();
    for(int i = 1; i <= n; i++) a[i] = read();
    for(int i = 1; i <= m; i++) {
        int b;
        b = read();
        ans[i] = search(b);
    }
    for(int i = 1; i <= m; i++) printf("%d ", ans[i]);
    return 0;

}

by claoday @ 2024-10-20 11:23:21

#include<iostream>
using namespace std;
const int maxn=1000005;
int n,m,b,a[maxn];
int finds(int x)
{
    int l=1,r=n;
    while(l<r)
    {
        int mid=l+(r-l)/2;
        if(a[mid]>=x)
        r=mid;
        else
        l=mid+1;
    }
    if(a[l]==x)
    return l;
    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;
        int pos=finds(b);
        cout<<pos<<" ";
    }
    return 0;
}

求关


by claoday @ 2024-10-20 11:23:34

@Ryoo


by Ryoo @ 2024-10-20 11:28:49

@claoday 大佬我刚刚输入输出改用scanf和printf就过了,能帮我看看快读快写有什么问题吗,感谢


by claoday @ 2024-10-20 11:33:00

快读模板

inline ll read()//快读模板
{
    ll x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9')
    f=(c=='-'?-1:f),c=getchar();
    while(c>='0'&&c<='9')
    x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}

快些模板

inline void write(ll x)//快写模板
{
    if(x<0)
    putchar('-'),x=-x;
    if(x>9)
    write(x/10);
    putchar(x%10+'0');
}

@Ryoo


by Ryoo @ 2024-10-20 11:34:51

@claoday 感谢大佬


|