poj 3258 River Hopscotch && poj 2456 Aggressive cows(最大化最小值)

来源:互联网 发布:如何在淘宝挑选枸杞 编辑:程序博客网 时间:2024/06/08 17:21

题目链接:http://poj.org/problem?id=3258


Description

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end,L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks,N (0 ≤N ≤ 50,000) more rocks appear, each at an integral distanceDi from the start (0 <Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up toMrocks (0 ≤ MN).

FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set ofM rocks.

Input

Line 1: Three space-separated integers: L,N, andM
Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

Output

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removingM rocks

Sample Input

25 5 2214112117

Sample Output

4

Hint

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

Source

USACO 2006 December Silver


题意:长度为L的河,有n块石头,要求移除m块后,最近的石头的距离最大化。。

思路:首先将石头排序,然后C(x)表示最小距离为x,二分x,然后贪心选取n-m个来判断x是否满足条件,注意特判两种情况。。

代码:
#include <algorithm>#include <cstdlib>#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <cctype>#include <cmath>#include <stack>#include <queue>#include <list>#include <map>#include <set>using namespace std;#define clr(x, y)      memset(x, y, sizeof(x))#define fr(i,n)        for(int i = 0; i < n; i++)#define fr1(i,n)       for(int i = 1; i <= n; i++)#define upfr(i,j,n)    for(int i = j; i <= n; i++)#define dowfr(i,j,n)   for(int i = n; i >= j; i--)#define scf(n)         scanf("%d", &n)#define scf2(n,m)      scanf("%d %d",&n,&m)#define scf3(n,m,p)    scanf("%d %d %d",&n,&m,&p)#define ptf(n)         printf("%d",n)#define ptf64(n)       printf("%I64d",n)#define ptfs(s)        printf("%s",s)#define ptln()         printf("\n")#define ptk()          printf(" ")#define ptc(c)         printf("%c",c)#define srt(a,n)       sort(a,n)#define LL long long#define pi acos(-1.0)#define eps 0.00001#define maxn 50005#define mod 1000000007#define inf 1000000000int a[maxn];int l,n,m;int C(int d){int la = 0;int num = n - m;fr(i, num){int cur = la + 1;while(cur <= n && a[cur] - a[la] < d)cur++;if(cur > n )return 0;la = cur;}return 1;}int main(){// freopen("in.txt","r",stdin);while(scf3(l, n, m) == 3){fr1(i, n)scf(a[i]);if(n== 0 && m==0){printf("%d\n", l);continue;}if(n==m){printf("%d\n", l);continue;}a[0] = 0;a[++n] = l;sort(a+1, a+n);int lb = 0, ub = l;while(ub - lb > 1){int mid = (lb + ub) / 2;if(C(mid))lb = mid;elseub = mid;}printf("%d\n", lb);}return 0;}



题目链接:http://poj.org/problem?id=2456

题意:n个序列中,选取c个数,使其最近的数的距离最大化。。。

思路:同上。。。

代码:
#include <algorithm>#include <cstdlib>#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <cctype>#include <cmath>#include <stack>#include <queue>#include <list>#include <map>#include <set>using namespace std;#define clr(x, y)      memset(x, y, sizeof(x))#define fr(i,n)        for(int i = 0; i < n; i++)#define fr1(i,n)       for(int i = 1; i <= n; i++)#define upfr(i,j,n)    for(int i = j; i <= n; i++)#define dowfr(i,j,n)   for(int i = n; i >= j; i--)#define scf(n)         scanf("%d", &n)#define scf2(n,m)      scanf("%d %d",&n,&m)#define scf3(n,m,p)    scanf("%d %d %d",&n,&m,&p)#define ptf(n)         printf("%d",n)#define ptf64(n)       printf("%I64d",n)#define ptfs(s)        printf("%s",s)#define ptln()         printf("\n")#define ptk()          printf(" ")#define ptc(c)         printf("%c",c)#define srt(a,n)       sort(a,n)#define LL __int64#define pi acos(-1.0)#define eps 0.00001#define maxn 100005#define mod 1000000007#define inf 1000000000int a[maxn];int n,c;int C(int d){int la = 0;upfr(i, 1, c-1){int now = la + 1;while(now < n && a[now] - a[la] < d)now ++;if(now == n)return 0;la = now;}return 1;}int main(){// freopen("in.txt","r",stdin);while(scf2(n, c) == 2){fr(i, n)scf(a[i]);sort(a, a+n);int lb = 0, ub = inf;while(ub - lb > 1){int m = (lb + ub) / 2; if(C(m)) lb = m; else ub = m;}printf("%d\n", lb);}return 0;}



0 0
原创粉丝点击