题解
2024-09-17 15:49:53
发布于:广东
1阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for (int i=0;i<n;i++)
{
cin >> a[i];
}
int m;
cin >> m;
while (m--)
{
int question;
cin >> question;
int l = 0,r = n-1,flag=0,ans=0;
while (l <= r)
{
int mid = l + r >> 1;
if (a[mid] == question)
{
r = mid-1;
flag = 1;
ans = mid;
}
else if (a[mid] < question)
{
l = mid+1;
}
else
{
r = mid-1;
}
}
if (!flag)
{
cout << -1 << ' ';
}
else
{
cout << ans+1 << ' ';
}
l = 0,r = n-1,flag=0;
ans = 0;
while (l <= r)
{
int mid = l + r >> 1;
if (a[mid] == question)
{
flag=1;
l = mid+1;
ans = mid;
}
else if (a[mid] < question)
{
l = mid+1;
}
else
{
r = mid-1;
}
}
if (!flag) cout << -1 << ' ';
else cout << ans+1 << ' ';
int answer = upper_bound(a,a+n,question)-a;
if (answer != n)cout << answer+1;
else cout << -1;
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个