BaiJiezi_Huaji @ 2023-12-09 14:22:47
#include<bits/stdc++.h>
using namespace std;
long long a,b,c[1000005],d[100005],in;
int main() {
cin>>a>>b;
for(long long i=1;i<=a;i++) scanf("%lld",&c[i]);
for(long long i=1;i<=b;i++)
{
scanf("%lld",&in);
if(bool(binary_search(c,c+a+1,in)))
d[i]=lower_bound(c,c+a,in)-c;
else
d[i]=-1;
if(a==1 && c[1]==in) d[i]=1;
}
for(long long i=1;i<=b;i++)
printf("%lld ",d[i]);
return 0;
}
by shenzixin @ 2023-12-20 22:57:03
给你个数据: 1 1 1 1
另外最后一个TLE知道为什么吗
by BaiJiezi_Huaji @ 2024-01-02 10:58:06
@shenzixin 输入1 1 1 1 输出1 没问题吧,还是我哪里想错了
然后TLE是可能没用printf和scanf吧
by shenzixin @ 2024-01-06 08:23:07
@BaiJiezi_Huaji 我是因为算法有些暴力 你的问题我也不知道了 这是我a的码
#include<bits/stdc++.h>
using namespace std;
long long n, m;
long long a[1000009];
vector<long long> o;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
long long x;
int l = 0,r = n;
for (int i = 0; i < m; i++) {
scanf("%lld", &x);
l = -1, r = n-1;
while (l < r-1) {
if (a[(l + r) / 2] >= x) {
r = (l + r) / 2;
}
else {
l = (l + r) / 2;
}
}
if (a[r] == x) {
o.push_back(r+1);
}
else {
o.push_back(-1);
}
}
for (int i = 0; i < m; i++) {
cout << o[i]<<" ";
}
}