JZOJ 3101. 开车旅行 70
3101. 开车旅行 (Standard IO)
Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits
Goto ProblemSet1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #define eps 1e-8 7 #define N 100010 8 using namespace std; 9 int n,ans,m,k,x,x0,h[N],x1[N],x2[N],dis1[N],dis2[N]; 10 double num=1000000000; 11 int main() 12 { 13 scanf("%d",&n); 14 for (int i=1;i<=n;i++) scanf("%d",&h[i]); 15 for (int i=n-1;i>=1;i--) 16 { 17 int mn1=i+1,mn2=0; dis1[i]=abs(h[i]-h[mn1]); 18 for (int j=i+2;j<=n;j++) 19 if (dis1[i]>abs(h[i]-h[j])||(dis1[i]==abs(h[i]-h[j])&&h[j]<h[mn1])) dis2[i]=dis1[i],dis1[i]=abs(h[i]-h[j]),mn2=mn1,mn1=j; 20 else if (dis2[i]==0||dis2[i]>abs(h[i]-h[j])||(dis2[i]==abs(h[i]-h[j])&&h[j]<h[mn2])) dis2[i]=abs(h[i]-h[j]),mn2=j; 21 x1[i]=mn1,x2[i]=mn2; 22 } 23 scanf("%d",&x0),ans=0; 24 for (int i=1;i<=n;i++) 25 { 26 int d1=0,d2=0,d=0,k=i; 27 while (1) 28 { 29 if (d) 30 { 31 if (d1+d2+dis1[k]>x0||!x1[k]) break; 32 d2+=dis1[k],k=x1[k]; 33 } 34 else 35 { 36 if (d1+d2+dis2[k]>x0||!x2[k]) break; 37 d1+=dis2[k],k=x2[k]; 38 } 39 d^=1; 40 } 41 if (!ans||1.0*d1/d2-num<-eps||fabs(1.0*d1/d2-num)<=eps&&h[ans]<h[i]) num=1.0*d1/d2,ans=i; 42 } 43 printf("%d\n",ans); 44 scanf("%d",&m); 45 while (m--) 46 { 47 scanf("%d%d",&k,&x); 48 int d1=0,d2=0,d=0; 49 while (1) 50 { 51 if (d) 52 { 53 if (d1+d2+dis1[k]>x||!x1[k]) break; 54 d2+=dis1[k],k=x1[k]; 55 } 56 else 57 { 58 if (d1+d2+dis2[k]>x||!x2[k]) break; 59 d1+=dis2[k],k=x2[k]; 60 } 61 d^=1; 62 } 63 printf("%d %d\n",d1,d2); 64 } 65 }
为何要逼自己长大,去闯不该闯的荒唐