castal_ @ 2023-01-10 18:31:35
大佬们,问问这个代码为什么第一个测试点wa了,最后一个测试点TLE了
using namespace std;
const int N = 1e6+10;
typedef pair<int,int> PII;
long long a[N];
long long n,m;
long long sweet(long long x)
{
long long l = 1,r=n;
while(l <= r)
{
long long mid=l+(r-l)/2;
if( a[mid] < x ) l=mid+1;
else if(a[mid] > x ) r=mid-1;
else if(a[mid] == x)
{
while(a[mid]==a[mid-1] && a[mid-1] == x) mid--;
return mid;
}
}
if(a[l] == 1) return 1;
return -1;
}
int main()
{
scanf("%lld %lld",&n,&m);
for(int i = 1;i <= n; i++ ) scanf("%lld",&a[i]);
while(m--)
{
long long x;
scanf("%lld",&x);
printf("%lld ",sweet(x));
}
return 0;
}
by InversionShadow @ 2023-01-10 18:34:59
@castal_
a[mid] == xx
可以不判断。
是 if(a[l] == x) return l
而不是 if(a[l] == 1) return l;
by castal_ @ 2023-01-11 00:32:09
@yuandingquan1101
哦哦好,谢谢