hdu 2217 Visit

本文介绍了一道关于旅行者在限定时间内能访问多少地点的问题,并给出了解决方案。通过将地点进行排序并采用不同的旅行策略,如一直向左、一直向右、先左后右或先右后左等,来确定在规定时间内可以访问的最大地点数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值