我严重怀疑 P161 数据有问题。

我交了下我自己在洛谷上的 AC 代码得了 20 分,交了洛谷上的题解也得 20 分

My code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int n,m,a[maxn],x;
int find(int pos){
	int l=1,r=n;
	while(l<r){
		int mid=(l+r)/2;
		if(a[mid]>=x) r=mid;
		else l=mid+1;
	}
	if(a[l]==x) return l;
	return -1;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=m;i++){
		cin>>x;
		int ans=find(x);
		cout<<ans<<" ";
	}
	return 0;
}

http://111.229.165.30:8888/record/6aabe5fd16bce1be1e076425 题解:

#include<cstdio>
#include<algorithm>//用到lower_bound
using namespace std;
const int MAXN=1e6+10;//注意范围
int read(){//快读
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
int a[MAXN];
int main(){
	int n=read(),m=read();//读入
	for(int i=1;i<=n;i++) a[i]=read();
	while(m--){
		int x=read();
		int ans=lower_bound(a+1,a+n+1,x)-a;//二分搜,注意-a
		if(x!=a[ans]) printf("-1 ");//没有,输出-1
		else printf("%d ",ans);//有,输出ans
	}
	return 0;//华丽结束
}

http://111.229.165.30:8888/record/6ad05c85064c23f1cbce2a40

2 条评论

  • 1

信息

ID
762
时间
ms
内存
MiB
难度
2
标签
递交数
25
已通过
6
上传者