二分+dfs死循环怎么办
查看原帖
二分+dfs死循环怎么办
490651
zcy942楼主2022/6/30 15:08
#include <iostream>
#include <cstdio>
using namespace std;

int n,d,k,a[500010],ans=114514,maxint=0,disd,disu,farthest;

void dfs(int x,int store){
	maxint=max(maxint,store);
	if(x+disd>farthest) return;
	for(int i=x+disd;i<=farthest && i<=x+disu;i++){
		if(a[i]==0) continue;
		dfs(i,store+a[x]);
	}
	return;
}

bool check(int x){
	disd=d-x,disu=d+x;
	if(disd<=0) disd=1;
	maxint=-114514;
	dfs(0,0);
	return maxint>=k;
}

int main(){
	freopen("jump.in","r",stdin);
	freopen("jump.out","w",stdout);
	
	scanf("%d%d%d",&n,&d,&k);
	for(int i=1;i<n;i++){
		int l,s;
		scanf("%d%d",&l,&s);
		a[l]=s;
		maxint=max(maxint,maxint+s);
	}
	int l,s;
	scanf("%d%d",&l,&s);
	a[l]=s;
	farthest=l;
	maxint=max(maxint,maxint+s);
	if(maxint<k){
		printf("-1");
		return 0;
	}
	l=0;
	int r=max(d,farthest-d);
	
	while(l<=r){
		int mid=(l+r)>>1;
		if(check(mid)){
			ans=min(ans,mid);
			r=mid-1;
		}
		else l=mid+1;
	}
	printf("%d",ans);
	
	return 0;
}
/*
7 4 10
2 6
5 -3
10 3
11 -3
13 1
17 6
20 2

2
*/
2022/6/30 15:08
加载中...