joj2308
贪心,弄个2重循环,每次取最优就可以了。
#include<iostream>
#include<stdio.h>
#include<math.h>
#include <algorithm>
using namespace std;
int a[52];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
int i,j;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int mark=0,maxx=10000,j1,j2;
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
{
if(fabs(a[i]-a[j])>=k)
{
mark=1;
int a=min(i,j);
int b=max(i,j);
if(maxx==max(i,j))
{
if((a/2+1+(b-a+1)/2)<(j1/2+1+(j2-j1+1)/2))
{
j1=a;
j2=b;
}
}
else if(maxx>max(i,j))
{
maxx=max(i,j);
j1=min(i,j);
j2=max(i,j);
}
}
}
if(mark==0)
printf("%d\n",n);
else
printf("%d\n",j1/2+1+(j2-j1+1)/2);
}
}