题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2217

题目解释:起始位置在原点,给你固定的时间,让你左右跑,求在规定的时间内你最多能跑多少个点;

解决本题,一个是要统计经过的点的个数,一个是全局只有一个拐,所以枚举所有的拐点即可解决;

五个小时卡到一组数据上:

8 9

-5 -4 -3 -2 2 2 2 2

答案应该是:8

AC代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int N = ;
const int M = ;
int lefT[N],righT[N];
int LL[M],RR[M];
int main()
{
int n,L,R,T,le,ri,ans,x,y;
while(scanf("%d %d",&n,&T)!=EOF)
{
L =;R=;le =;ri = ;ans = -;
memset(LL,,sizeof(LL));
memset(RR,,sizeof(RR));
for(int i =; i<=n; i++)
{
scanf("%d",&x);
if(x<)
{
x = abs(x);
lefT[L++] = x;
LL[x]++;
}
else
{
righT[R++] = x;
RR[x]++;
}
}
lefT[] = ;
righT[] = ;
sort(lefT+,lefT+L+);
sort(righT+,righT+R+);
//预处理到达当前位置时共找到的点数,仔细退一下就出来了
for(int i = ; i<=T; i++)
{
LL[i] =LL[i-]+LL[i];
RR[i] = RR[i-]+RR[i];
}
//下面是模拟首先向左跑
int xx = ;
while(*lefT[xx]<=T && xx<L)
{
ans = max(ans,LL[lefT[xx]]+RR[T - *lefT[xx]]);
xx++;
}
//下面模拟首先向右跑
xx = ;
while(*righT[xx]<=T && xx<R)
{
ans = max(ans,RR[righT[xx]]+LL[T - *righT[xx]]);
xx++;
}
printf("%d\n",ans);
}
return ;
}
 

hdu 2217 Visit的更多相关文章

  1. 【HDOJ】2217 Visit

    挺好的一道DP. /* 2217 */ #include <iostream> #include <cstdio> #include <cstring> #incl ...

  2. HDU 2217 Data Structure?

    C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  3. hdu 4612 Warm up(缩点+树上最长链)

    本来就是自己负责图论,结果水了= = 题目其实很裸,就是求桥的数量,只是要新加上一条边罢了.做法:先缩点.再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲 ...

  4. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  5. hdu 4607 Park Visit

    http://acm.hdu.edu.cn/showproblem.php?pid=4607 先求树的直径 方法:两遍bfs ,任选一点 a  求到a点最远的一点b ,然后 求到b点最远点 c 这样 ...

  6. hdu 4607 Park Visit (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先如果k小于等于直径长度,那么答案为k−1.如果k大于直径长度,设直径长度为r,那么答案为r− ...

  7. HDU 4607 Park Visit (树的最长链)

    Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. 题解报告:hdu 4607 Park Visit(最长链)

    Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...

  9. HDU 4607 Park Visit 两次DFS求树直径

    两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R:   ans = ...

随机推荐

  1. HDOJ 4903 The only survival

    Discription: There is an old country and the king fell in love with a devil. The devil always ask th ...

  2. POJ 3177 Redundant Paths(边双连通分量)

    [题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化 ...

  3. 【AC自动机】【矩阵乘法】【等比数列】hdu2243 考研路茫茫——单词情结

    题解:http://blog.csdn.net/xingyeyongheng/article/details/10005923 这里采用了二分法求等比数列前n项和. 等比数列前n项和也可以用矩乘快速幂 ...

  4. 【并查集+离散化】BZOJ4195- [Noi2015]程序自动分析

    [题目大意] 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的 ...

  5. 小白的Python之路 day5 os,sys模块详解

    os模块详解 1.作用: 提供对操作系统调用的接口 2.常用方法: os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname" ...

  6. [Android]--RadioGroup+RadioButton实现底部导航栏

    RadioGroup+RadioButton组合方式打造简单实用的底部导航栏 代码块: <?xml version="1.0" encoding="utf-8&qu ...

  7. 如何订阅Linux相关的邮件列表

    转:http://blog.163.com/sunshine_linting/blog/static/44893323201282114012845/ 1.google"linux kern ...

  8. [转] Matlab与C++混合编程,添加OpenCV库

    原文地址 峰回璐转 最近在做运动医学软件优化工作,此款软件框架及算法语言全由matlab实现,虽然matlab矩阵运算.数值计算能力强大,但速度让人难以忍 受.软件立刻移植到C++上又不太实际,故采用 ...

  9. iptables禁止外网访问redis server服务默认端口6379的命令

    //只允许127.0.0.1访问6379 iptables -A INPUT -s 127.0.0.1 -p tcp --dport 6379 -j ACCEPT //其他ip访问全部拒绝 iptab ...

  10. Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface解决方案

    原文:http://pimin.net/archives/432 环境:Eclipse LUNA.Spring 4.1.1.或Spring 4.3.3.Spring Data Elasticsearc ...