【POJ】[3258]River Hopscotch

Problem Description

Loading...

Problem Analysis

It is also a problem of **optimizing the extrem*e value
Once you master the technique, this kind of problem is still quite simple

The idea is always to use binary search to find the answer
then check whether this answer meets the requirement
this way you can keep bisecting
to shrink the interval containing the answer

The way this problem checks whether an answer is valid
if the minimum value is x
then for the answer to hold
some rocks need to be removed
obviously
if you want the answer to satisfy the condition
the number of rocks to remove must satisfy cnt<=m

Through binary search
you can find the one that makes the answer valid
the largest res

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[50200];
int l,n,m;
bool judge(int x) {
	if(x==0)
		return false;
	int cnt=0,last=0;
	for(int i=1; i<n; i++) {
		int t=a[i]-a[last];
		if(t<x)
			cnt++;
		else
			last=i;
	}
	if(cnt>m)
		return false;
	else
		return true;
}
int main() {
	while(scanf("%d %d %d",&l,&n,&m)!=EOF) {
		a[0]=0;
		for(int i=1; i<=n; i++)
			scanf("%d",&a[i]);
		a[(++n)++]=l;
		sort(a,a+n);
		int la=0,lb=l;
		int mid,res;
		while(lb>=la) {
			mid=(la+lb)/2;
			if(judge(mid)) {
				res=mid;
				la=mid+1;
			} else
				lb=mid-1;
		}
		printf("%d\n",res);
	}
	return 0;
}

Problem link: 【POJ】[3258]River Hopscotch

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...