显示代码纯文本
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int a[50010],i,t,n,sum,j,b[50010],ia,ib,x,ja,jb,p;
int comp(const void*a,const void*b)
{
return *(int*)a-*(int*)b;
}
int main()
{
freopen("explore.in","r",stdin);
freopen("explore.out","w",stdout);
scanf("%d%d",&t,&n);
ia=ib=0;
memset(a,0x3f3f3f3f,sizeof(a));
memset(b,0x3f3f3f3f,sizeof(b));
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(x>0)
{
a[ia]=x;
ia++;
}
else
{
b[ib]=x;
ib++;
}
}
p=j=ja=sum=0;
jb=ib-1;
qsort(a,ia,sizeof(int),comp);
qsort(b,ib,sizeof(int),comp);
while(sum<t)
{
if(ja>=ia&&jb<0)
{
printf("%d",j);
return 0;
}
if((a[ja]<abs(b[jb])||jb<0)&&ja<ia)
{
sum+=a[ja]-p;
p=a[ja];
ja++;
}
else if((a[ja]>abs(b[jb])||ja>=ia)&&jb>=0)
{
sum+=abs(b[jb]-p);
p=b[jb];
jb--;
}
j++;
}
if(sum==t) printf("%d",j);
else printf("%d",j-1);
return 0;
}