二分变形二,求调

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int a[100005];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
while(cin>>m){
int l=1,r=n;
while(l<r){
int mid=(l+r)/2;
if(a[mid]>m)r=mid;
if(a[mid]<=m)l=mid+1;
}
if(a[r-1]!=m){
cout<<“0”<<" “;
}
else{
if(r==n){
cout<<r<<” “;
}
else{
cout<<r-1<<” ";
}
}
}
return 0;
}[题目链接](https://www.xinyoudui.com/ac/contest/747003DC800053401C73286/problem/6819)

阿米诺斯,题目链接无法访问,截屏(win+shift+s)吧

1 个赞

我可以
image


image

1 个赞
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int a[100005];
int main(){
  cin>>n;
  for(int i=1;i<=n;i++){
    cin>>a[i];
  }
  while(cin>>m){
    int l=1,r=n;
    while(l<r){
        int mid=(l+r)/2;
        if(a[mid]>m)r=mid;
        if(a[mid]<=m)l=mid+1;
    }
    if(a[r-1]!=m){
    cout<<"0"<<" ";
    }
    else{
      if(r==n){
          cout<<r<<" ";
      }
      else{
          cout<<r-1<<" ";
      }
    }
  }
  return 0;
}

顺便帮忙格式化一下

2 个赞