Description

Bessie is traveling on a road teeming with interesting landmarks. The road is labeled just like a number line, and Bessie starts at the "origin" (x = 0). A total of N (1 ≤ N ≤ 50,000) landmarks are located at points x1, x2, ..., xN (-100,000 ≤ xi ≤ 100,000). Bessie wants to visit as many landmarks as possible before sundown, which occurs in T (1 ≤ T ≤ 1,000,000,000) minutes. She travels 1 distance unit in 1 minute.

Bessie will visit the landmarks in a particular order. Since the landmarks closer to the origin are more important to Farmer John, she always heads for the unvisited landmark closest to the origin. No two landmarks will be the same distance away from the origin.

Help Bessie determine the maximum number of landmarks she can visit before the day ends.

Input

* Line 1: Two space-separated integers: T and N
* Lines 2..N+1: Line i+1 contains a single integer that is the location of the ith landmark: xi

Output

* Line 1: The maximum number of landmarks Bessie can visit.

Sample Input

25 5
10
-3
8
-7
1

Sample Output

4

Source

USACO 2007 November Bronze

水题,对绝对值排序

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int a[50005];
bool cmp(int a,int b)
{
    return abs(a)<abs(b);
}
int n,t;
int main()
{
    scanf("%d%d",&t,&n);
    for(int i=1;i<=n;i++)
    {scanf("%d",&a[i]);

    }
    sort(a+1,a+n+1,cmp);
    int w=0;
    int ans=0;
    int sum=0;
    a[0]=0;
   for(int i=1;i<=n;i++)
   {
       sum=abs(a[i]-a[i-1]);
       if(sum>t)
       break;
       else if(sum<=t)
       {
           t-=sum;

           ans++;
       }

   }
printf("%d\n",ans);
return 0;
}

 

10-03 11:19