[POI2006]KRA-The Disks

题目链接

Solution

开一个数组 \(mn(i)\) 记录第 \(i\) 个位置及之前的最窄的宽度,显然其单调不上升,每次我们二分找到最深能够到达的位置,记录为 \(ans\),然后第二次二分时的右端点就只要开到 \(ans-1\),就保证不超过前一个。
复杂度 \(O(n\log n)\),蓝题 \(15\) 分钟水过。

Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
void read(int &x)
{
	char ch=getchar();
	int r=0,w=1;
	while(!isdigit(ch))w=ch=='-'?-1:1,ch=getchar();
	while(isdigit(ch))r=(r<<3)+(r<<1)+(ch^48),ch=getchar();
	x=r*w;
}
const int N=3e5+7;
int mn[N];
bool bb[N];
main()
{
	memset(mn,63,sizeof mn);
	int n,m;
	read(n);read(m);
	int last=n;
	for(int i=1,x;i<=n;i++)
		read(x),mn[i]=min(mn[i-1],x);
	int ans;
	for(int i=1,x;i<=m;i++)
	{
		read(x);
		int l=1,r=last;
		ans=0;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(mn[mid]>=x)l=mid+1,ans=mid;
			else r=mid-1;
		}
		last=ans-1;
	}
	cout<<ans;
	return 0;
}
posted @ 2022-08-10 23:19  Epoch_L  阅读(30)  评论(0)    收藏  举报