poj3618

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 50005

int map[N];
int n;

bool cmp(int a,int b){
	return abs(a)<abs(b);
}

int main(){
	__int64 sum_t,used_t;
	int i,j,ans;
	while(cin>>sum_t>>n){
	for(i=1;i<=n;i++)
		cin>>map[i];//scanf("%I64d",&map[i]);
	map[0]=0;
	used_t=0;
	ans=0;	

	j=1;
	if(map[1]==0)
	{
		ans++;
		j=2;
	}

	sort(map,map+n+1,cmp);

	for(;j<=n;j++){
		if(abs(map[j]-map[j-1])<=(sum_t-used_t)){
			ans++;
			used_t=used_t+abs(map[j]-map[j-1]);
		}
		else
			break;//time已经用完了
	}
	cout<<ans<<endl;//printf("%d\n",ans);
	}
	return 0;
}

  很巧妙地利用abs函数;

题意就是从0出发,向左或向右走,花费相应的时间;

即每一次判断是向左走更短还是向右,这就是abs函数运用巧妙的地方。

posted @ 2012-07-15 10:59  xxx0624  阅读(276)  评论(0编辑  收藏  举报