hdu 2217 Visit

来源:互联网 发布:net 查看端口 编辑:程序博客网 时间:2024/06/11 01:54
Problem Description
Wangye is interested in traveling. One day, he want to make a visit to some
different places in a line. There are N(1 <= N <= 2000) places located at points x1, x2, ..., xN (-100,000 ≤ xi ≤ 100,000). Wangye starts at HDU (x = 0), and he travels 1 distance unit in 1 minute . He want to know how many places he could visit at most, if he has T (1 <= T <= 200000 ) minutes.
Input
The input contains several test cases .Each test case starts with two number N and T which indicate the number of places and the time respectively. Then N lines follows, each line has a number xi indicate the position of i-th place.
Output
For each test case, you should print the number of places,Wangye could visit at most, in one line.
Sample Input
5 16
-3
-7
1
10
8
Sample Output
4

题目大意:在数轴上给n个点,让你从点0开始走,算出在规定时间T内能拜访最多点的个数。

思路:开始我居然用dfs去写,虽然意料到会超时,但是我还是死脑筋的去想减支,后来发现完全可以不需要dfs!!

从0点开始走,要么左要么右,而且走到最右或者最左那么0到最左最右的点肯定拜访过了,当你望左走了后,如果你在往右是可以的,但是不能再往左,既不能左右左,因为这是徒劳的走法,路程肯定多,(仔细想一想)。所以走法只有:4种:一直向左,一直向右,先左后右,先右后左。

<span style="font-family:Arial;">#include <iostream>#include<stdio.h>#include<algorithm>using namespace std;int n,T,a[2005];int main(){    int s,q,p,ma,x,y;    while(scanf("%d %d",&n,&T)!=EOF)    {        a[0]=0;        for(int i=1; i<=n; i++)            scanf("%d",&a[i]);        sort(a,a+n+1);//从小到大排序,        s=lower_bound(a,a+n+1,0)-a;//找到点0        ma=0;        for(q=s;q>=0;q--)//q是往左        {            for(p=s;p<=n;p++)//p是往右            {                x=-a[q],y=a[p];                if(2*x+y<=T&&p-q>ma) ma=p-q;//左右                if(2*y+x<=T&&p-q>ma) ma=p-q;//右左            }        }        cout<<ma<<endl;    }    return 0;}</span>


1 0
原创粉丝点击