Your teacher has given you some problems to solve. You must first solve problem 0. After solving each problem i, you must either move on to problem i+1 or skip ahead to problem i+2. You are not allowed to skip more than one problem. For example, {0, 2, 3, 5} is a valid order, but {0, 2, 4, 7} is not because the skip from 4 to 7 is too long.
You are given N integers, where the ith integer indicates how much you like problem i. The teacher will let you stop solving problems once the range of pleasantness you've encountered reaches a certain threshold. Specifically, you may stop once the difference between the maximum and minimum pleasantness of the problems you've solved is greater than or equal to a integer K. If this never happens, you must solve all the problems.
Input
The input contains several test cases. For each case it contains two positive integer N, K (1<=N<=50, 1<=K<=1000), then N integers follow in the next line, the ith integer ( >=1 and <= 1000 ) indicates how much you like problem i.
Output
For each test case, output the minimum number of problems you must solve to satisfy the teacher's requirements.
Sample Input
3 2
1 2 3
5 4
1 2 3 4 5
Sample Output
2
3
很笨但是很实用!
#include<stdio.h>
int a[51];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
int i,j;
for(i=0;i<n;i++)
scanf("%d",a+i);
int min=0xfffffff;
int flag=0;
for(i=1;i<n;i++)
for(j=0;j<i;j++)
{
if(a[i]-a[j]>=k||a[j]-a[i]>=k)
{
flag=1;
int step;
if(j==0)
{
step=(i+1)/2+1;
if(min>step)
min=step;
}
else
{
step=(i-j+1)/2+(j+1)/2+1;
if(min>step)
min=step;
}
}
}
if(flag)
printf("%d\n",min);
else
printf("%d\n",n);
}
return 0;
}
posted on 2009-06-28 22:26
luis 阅读(241)
评论(0) 编辑 收藏 引用 所属分类:
给我启发题