hdu 2217 Visit

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

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

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

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

8 9

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

答案应该是:8

AC代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 using namespace std;
 7 const int N = 2010;
 8 const int M = 200100;
 9 int lefT[N],righT[N];
10 int LL[M],RR[M];
11 int main()
12 {
13     int n,L,R,T,le,ri,ans,x,y;
14     while(scanf("%d %d",&n,&T)!=EOF)
15     {
16         L =1;R=1;le =0;ri =0 ;ans = -1;
17         memset(LL,0,sizeof(LL));
18         memset(RR,0,sizeof(RR));
19         for(int i =1; i<=n; i++)
20         {
21             scanf("%d",&x);
22             if(x<0)
23             {
24                  x = abs(x);
25                  lefT[L++] = x;
26                  LL[x]++;
27             }
28             else
29             {
30                 righT[R++] = x;
31                 RR[x]++;
32             }
33         }
34         lefT[0] = 0;
35         righT[0] = 0;
36         sort(lefT+1,lefT+L+1);
37         sort(righT+1,righT+R+1);
38         //预处理到达当前位置时共找到的点数,仔细退一下就出来了
39         for(int i = 1; i<=T; i++)
40         {
41             LL[i] =LL[i-1]+LL[i];
42             RR[i] = RR[i-1]+RR[i];
43         }
44         //下面是模拟首先向左跑
45         int xx = 0;
46         while(2*lefT[xx]<=T && xx<L)
47         {
48             ans = max(ans,LL[lefT[xx]]+RR[T - 2*lefT[xx]]);
49             xx++;
50         }
51         //下面模拟首先向右跑
52         xx = 0;
53         while(2*righT[xx]<=T && xx<R)
54         {
55             ans = max(ans,RR[righT[xx]]+LL[T - 2*righT[xx]]);
56             xx++;
57         }
58         printf("%d\n",ans);
59     }
60     return 0;
61 }
时间: 2024-11-06 16:01:07

hdu 2217 Visit的相关文章

HDU 2217 Data Structure?

C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice id=27724" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-

HDU 4607 Park Visit 求树的直径

Park Visit Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to

HDU 4607 Park Visit

Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all

hdu 4607 Park Visit(树的直径)

Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2506    Accepted Submission(s): 1128 Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The

题解报告:hdu 4607 Park Visit(最长路+规律)

Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all

HDU 4607 Park Visit(树的直径)

题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树的直径,也叫作最长简单路径.找出来这个直径之后,只需和k比较一下就能确定走多少步.设直径为maxx, 如果maxx + 1== k的话,说明刚好不用回来走完最长的这个路,所以当k小于等于maxx + 1的时候就是k-1,当k大于maxx + 1的时候,除了要走完不用回来的路,肯定还要走那些用回来的,

HDU 4607 Park visit (求树的直径)

解题思路: 通过两次DFS求树的直径,第一次以任意点作为起点,找到距离该点距离最远的点,则可以证明这个点一定在树的直径上,然后以该点为起点进行DFS得到的最长路就是树的直径. 最后的询问,如果K <= D + 1则可以沿着直径走,距离为K  -  1, 如果K >= D + 1,则需要走直径旁边的分支,每访问一个点距离为2(从直径到这个点,再返回到直径上). #include <iostream> #include <cstring> #include <cstd

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

HDU 1087 Super Jumping! Jumping! Jumping! 简单DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 题目大意:N个数字组成的一条路,每个数字是一个节点顺序连接,起点在第一个数之前,终点在第N个数之后.现让你选择一条路从起点跳到终点,只能往前且跳到比当前点大的那个点,可以认为终点是最大的,可以从起点直接跳到终点但是路的值就是0了,每条路的值为所经过的数字节点的值的和,问你值最大为多少. 解题思路:决策:在当前点i往i~N哪个点跳,反过来当前点i+1可以从1~i哪个点跳过来,那么a[i+1] >