疯牛&&青蛙过桥586

来源:互联网 发布:java native关键字 编辑:程序博客网 时间:2024/04/29 15:27

疯牛

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000,000,000).
但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢?
输入
有多组测试数据,以EOF结束。
第一行:空格分隔的两个整数N和C
第二行——第N+1行:分别指出了xi的位置
输出
每组测试数据输出一个整数,满足题意的最大的最小值,注意换行。
样例输入
5 312849
样例输出

3

这一题是要找牛与牛之间最大距离的最小值 

要保证牛不互相伤害的办法就是他们之间要隔开,距离必须要大于等于1和隔间!!!

用了二分查找,所以数列必须是有序的,就要先把数列进行排序处理

#include<stdio.h>
#include<algorithm>
using namespace std;
#define M 100000
int s[M],n,c;;
int f(int x)//此时传来的值是牛栏的距离
{
int niu,t,i;
niu=1;
t=s[0];//先把牛放在第一个位置
for(i=1;i<n;i++)
{
if(s[i]-t>=x)
{
t=s[i];
niu++;
if(niu>=c)//在符合牛不互相伤害的基础上让所有牛都有地方
return 1;
}
}
return 0;

}
int bsearch()//二分函数,为什么用二分法呢?

               因为牛与牛之间的距离就是要么在一起要么离得最远即是在两头
{
int l,r,mid;
l=0;
r=s[n-1]-s[0];
while(l<=r)
{
mid=(l+r)/2;
if(f(mid))//在满足f函数的基础上找位置
l=mid+1;
else
r=mid-1;
}
return r;
}
int main()
{

int i;
while(~scanf("%d%d",&n,&c))
{
for(i=0;i<n;i++)
{
scanf("%d",&s[i]);
}
sort(s,s+n);//一定不要忘记排序,这是很重要的
printf("%d\n",bsearch());
}
return 0;
}



下面是青蛙过河的

青蛙过桥

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
输入
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
输出
For each case, output a integer standing for the frog's ability at least they should have.
样例输入
6 1 2225 3 311 218
样例输出
411
解题思路:
这一题为什么用二分法呢?因为青蛙要想跳过桥不踩石子的情况下就是一下就跳过去的
也就是青蛙跳过去时候的跳的能力的大小,就类似于我们跳远,每个人的身体弹性能力不一样,限度不一样
这题就是根据青蛙的跳远限度写的 例如桥长6 有1个石子 青蛙最多跳2次
1.要么青蛙最大能力能跳6米,一次就跳过去了
2就是青蛙借助石子先跳2米,在跳4米;
这时青蛙要跳过桥的最大能力的最小值就是4了,
#include<stdio.h>#include<algorithm>using namespace std;#define MAX 500005int p[MAX];int pass(int l,int n,int m,int min)//不想传这么变量也可以设置为全局变量{int s=0,now=0,i;//now是当前位置,就是青蛙跳后的现在的位置,for(i=0;i<=n;){if(++s>m||p[i]-now>min)//用到++s是青蛙最少就要跳一次啊,m是最多跳的次数return 0;while(p[i]-now<=min&&i<=n)    i++;  now=p[i-1];每次都要记录青蛙的当前位置}return 1; } int main(){int l,n,m,min,max;int i,mid;while(scanf("%d%d%d",&l,&n,&m)!=EOF){for(i=0;i<n;i++){scanf("%d",&p[i]);}p[n]=l;sort(p,p+n);min=0;max=l;while(min<max){mid=(min+max)/2;if(pass(l,n,m,mid))max=mid;elsemin=mid+1;}printf("%d\n",max);}return 0;}

0 0