站外题求条,悬棺玄关
查看原帖
站外题求条,悬棺玄关
1059234
FamousKillerconan楼主2024/12/17 20:47

原题

我知道正解是two-pointer,但我觉的二分应该也能过,代码如下(Wrong Answer on test 21)。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
const int INF=0x3f3f3f3f;
const double EPS=10e-6;
ll n,m,a[N],b[N];
void solve(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=m;i++)cin>>b[i];
	for(int i=1;i<=m;i++){
		ll pos=lower_bound(a+1,a+n+1,b[i])-a-1;
		if(b[i]==a[pos])pos--;
		cout<<pos<<" ";
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int T=1;
//	cin>>T;
	while(T--)solve();
	return 0;
}
2024/12/17 20:47
加载中...