首页 > 代码库 > poj 3618 Exploration

poj 3618 Exploration

Exploration
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3826 Accepted: 1850

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 x1x2, ..., 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

题意:给你一堆点,从原点出发,到点的规律为优先达到距离原点最近的点,问在t秒的时间内可以达到的点的最大数量;


#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 50000+5
struct point{
	int landmark,distance;
}p[MAX];
bool cmp(point a,point b){
	if (a.distance<b.distance)
		return true;
	return false;
}
int main(){
	int t,n;
	while (cin>>t>>n){
		for (int i=0;i<n;i++){
			cin>>p[i].landmark;
			p[i].distance=p[i].landmark>0?p[i].landmark:(-p[i].landmark);
		}
		sort(p,p+n,cmp);
		/*Test
		for (int i=0;i<n;i++)
			cout<<p[i].landmark<<" "<<p[i].distance<<endl;
		Test*/
		int last=0,i;
		for (i=0;i<n;i++){
			int d=p[i].landmark-last>0?(p[i].landmark-last):(last-p[i].landmark);
			if (t>=d){
				t-=d;
				last=p[i].landmark;
			}
			else
				break;
		}
		cout<<i<<endl;
	}
	return 0;
}



poj 3618 Exploration