题目链接: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