样例输出或者测试点有问题!
原题链接:455.lower and upper bound2025-02-24 20:18:21
发布于:上海
反透视--------------------------------------------反透视--------------------------------------------反透视--------------------------------------------反透视-------------------------------
正片开始!
按照以下代码运行:↓
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,a[1000000],m;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    cin>>m;
    while(m--){
        int b;
        cin>>b;
        int x=lower_bound(a+1,a+1+n,b)-a;
        if(a[x]!=b){
            cout<<-1<<" ";
        }else{
            cout<<x<<" ";
        }
        int y=upper_bound(a+1,a+n+1,b)-a-1;
        if(a[y]!=b){
            cout<<-1<<" ";
        }else{
            cout<<y<<" ";
        }
        y+=1;
        if(a[y]>b){
            cout<<y<<endl;
        }else{
            cout<<-1<<endl;
        }
    }
    return 0;
}
再放入样例:
10
1 3 5 7 7 7 7 9 10 11
6
1
0
7
8
11
12
运行后的结果对照:
AC代码:
1 1 2
-1 0 1 <-中间是0
4 7 8
-1 -1 8
10 10 -1
-1 -1 -1
题目中的样例输出:
1 1 2
-1 -1 1   <--中间是-1
4 7 8
-1 -1 8
10 10 -1
-1 -1 -1
建议修改一下样例
顶
全部评论 4
?你有没有好好看 数组中有0吗
2025-02-23 来自 广东
0你应该特判0的情况
2025-02-23 来自 广东
0复制代码:
#include<bits/stdc++.h> using namespace std; int main(){ int n,a[1000000],m; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } cin>>m; while(m--){ int b; cin>>b; //这里加特判? int x=lower_bound(a+1,a+1+n,b)-a; if(a[x]!=b ){ // 是不是在这里加特判 cout<<-1<<" "; }else{ cout<<x<<" "; } int y=upper_bound(a+1,a+n+1,b)-a-1; if(a[y]!=b){ // 还是在这里? cout<<-1<<" "; }else{ cout<<y<<" "; } y+=1; if(a[y]>b){ // 或者是在这里? cout<<y<<endl; }else{ cout<<-1<<endl; } } return 0; }在哪里特判呢
2025-02-24 来自 上海
0直接特判b==0就行
2025-02-24 来自 广东
0
顶
2025-02-23 来自 上海
0顶
2025-02-23 来自 上海
0顶
2025-02-23 来自 上海
0


















有帮助,赞一个