Exercise: Travel by car (multiply + pre-treatment)

Source: Internet
Author: User

Travel by car (NOIP Group 2012)
Description
Small A and small b decided to travel by holiday, they will want to go to the city from 1 to n number, and the number of smaller cities in the west of the larger number of cities, it is known that the altitude of each city is different, remember the city i's altitude is hi, city I and City J distance D[i,j] It happens to be the absolute value of the difference in altitude of these two cities, i.e. d[i,j] = | hi-hj|.
During the trip, small A and small b take turns driving, the first day small a drive, after each day rotation. They plan to choose a city as a starting point, keep going east, and travel at most x kilometers. Small A and small B's driving style is different, small b always along the way to choose a recent city as a destination, and small a always follow the direction of the second near the city as the destination (note: If the current city to two cities in the same distance, it is considered to be closer to the city of the lower elevation). If any one of them is unable to choose the destination city in accordance with their own principles, or if the total distance traveled exceeds x km, they will end the journey.
Before departure, little a wants to know two questions:
1. For a given x=x0, from which city, small a drive the total number of journeys and small B travel total distance of the smallest ratio (if the travel distance of small B is 0, this time the ratio can be considered infinite, and two infinity as equal). If you are departing from multiple cities, the total number of cars driving in small A is the smallest ratio of the total distance traveled by small B, then the city with the highest altitude is output.
2. For any given x=xi and departure city Si, the total number of road drives by small A and the total distance travelled by small B.
Format
"Input Format"
The first line contains an integer n, which represents the number of cities.
The second line has n integers, separated by a space between each of the two integers, which in turn represents the elevation of city 1 to City N, which is H1,h2,......,hn, and each hi is different.
The third line contains an integer X0.
The IV behaves as an integer m, representing the given m-group Si and XI.
The next M-line, each line contains 2 integers, Si and Xi, indicating a maximum of Xi kilometres from the city Si.
"Output Format"
Outputs a total of m+1 rows.
The first line contains an integer S0, indicating that for a given X0, starting from a city numbered S0, small a drives
The total number of journeys is the smallest of the total distance traveled by small B.
The next M-line, each line contains 2 integers, separated by a space, which in turn represents the total number of miles traveling under a given Si and XI, and the total number of miles traveled by small B.
"Sample 1"
Sample input 1[copy]
4
2 3 1 4
3
4
1 3
2 3
3 3
4 3
Sample output 1[Copy]
1
1 1
2 0
0 0
0 0
"Sample 2"
Sample input 2[copy]
10
4 5 6 1 2 3 7 8 9 10
7
10
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
10 7
Sample output 2[Copy]
2
3 2
2 4
2 1
2 4
5 1
5 1
2 1
2 0
0 0
0 0
Limit
1s per test point
Tips
For 30% of the data, there are 1≤n≤20,1≤m≤20;
For 40% of the data, there are 1≤n≤100,1≤m≤100;
For 50% of the data, there are 1≤n≤100,1≤m≤1,000;
For 70% of the data, there are 1≤n≤1,000,1≤m≤10,000;
For 100% of the data, there are 1≤n≤100,000,1≤m≤10,000,-1,000,000,000≤hi≤1,000,000,000,0≤x0≤1,000,000,000,1≤si≤n,0≤xi≤1, 000,000,000, Data assurance hi is different.
Source
Noip2012 improve the day1t3 of group rematch

Analysis:

Interesting multiplication questions, a walk once and then B walk once as a step, first preprocessing out for each point, east from its nearest and second near Point

 ProgramDrive ;varF,VA,VB:Array[0.. -,0..100000] ofInt64; G,l,r,foot1,foot2:Array[0..100000] ofLongint; B:Array[0..100000] ofInt64; N,i,m,t,x,ans:longint; S1,s2,ans1,ans2:int64;functionmin (x,y,v:longint): Longint;begin  ifABS (B[X]-V) <=abs (b[y]-v) ThenExit (x)Elseexit (y);End;procedureqsort (l,h:longint);varI,j,t,m:longint;beginI:=l; j:=h; M:=a[(I+J)Div 2]; Repeat whileA[i]<m DoInc (i); whileM&LT;A[J] DoDec (j);ifI<=j Then  beginT:=a[i]; A[I]:=A[J];  a[j]:=t; T:=g[i]; G[I]:=G[J]; g[j]:=T;inc (i); Dec (j); End;untilI>J;ifI ThenQsort (I,H);ifJ>l ThenQsort (L,J);End;procedureDone ;vari,l1,l2,r1,r2,x:longint; v:longint;beginFillchar (l,sizeof (l),0); Fillchar (r,sizeof (R),0); b[0]:=-Maxlongint;  fori:=1  toN Do    beginL[g[i]]:=g[i-1]; r[g[i]]:=g[i+1]; End;  fori:=1  toN Do    beginV:=B[i]; L1:=l[i]; R1:=r[i]; L2:=l[l[i]]; r2:=R[r[i]]; ifABS (B[L1]-V) =abs (b[r1]-v) Then beginFOOT1[I]:=L1; FOOT2[I]:=R1;End       Else ifABS (B[L1]-V) <abs (b[r1]-v) Then beginFOOT1[I]:=L1; Foot2[i]:=min (L2,R1,V);End        Else beginFOOT1[I]:=R1; Foot2[i]:=min (L1,R2,V);End; R[l[i]]:=R[i]; L[r[i]]:=L[i]; End;End;procedureMake ;varI,j:longint;beginFillchar (va,sizeof (VA),0); Fillchar (Vb,sizeof (VB),0); Fillchar (F,sizeof (f),0);  fori:=1  toN Do   beginf[0, I]:=foot1[foot2[i]]; va[0, I]:=abs (B[foot2[i]]-b[i]); vb[0, I]:=abs (b[foot1[foot2[i]]]-B[foot2[i]]); End;  fori:=0  toTrunc (ln (n)/ln (2))-1  Do     forj:=1  toN Do     beginF[i+1, J]:=f[i,f[i,j]]; va[i+1, J]:=va[i,f[i,j]]+va[i,j]; vb[i+1, j]:=vb[i,f[i,j]]+Vb[i,j]; End;End;procedurefind (x,t:longint);vari:longint; s:int64;beginS:=0;  forI:=trunc (ln (n)/ln (2))Downto 0  Do    begin      iff[i,x]=0  Thencontinue; ifS+va[i,x]+vb[i,x]<=t Then         beginInc (S,va[i,x]+vb[i,x]); Inc (S1,va[i,x]); Inc (S2,vb[i,x]); X:=F[I,X];End; End; ifFoot2[x]<>0  Then     ifS+abs (B[foot2[x]]-b[x]) <=t Then beginInc (S,abs (B[FOOT2[X]]-B[X)); Inc (S1,abs (b[foot2[x]]-b[x)); x:=foot2[x];End;End;beginREADLN (n);  fori:=1  toN Do beginRead (A[i]); G[i]:=i;End; b:=A; Qsort (1, N);  Done  Make  READLN (t); b[0]:=-maxlongint; ans:=0; ans1:=1; ans2:=0;  fori:=1  toN Do   beginS1:=0; s2:=0;     Find (I,t); ifS1*ans2<s2*ans1 Then  beginAns:=i; ANS1:=S1; ANS2:=S2;End      Else if(S1*ANS2=S2*ANS1) and( not((s1=0) and(s2=0))) Then ifB[i]>b[ans] Then beginAns:=i; ANS1:=S1; ANS2:=S2;End   End;  Writeln (ANS);  READLN (m);  fori:=1  toM Do    beginreadln (x,t); S1:=0; s2:=0;      Find (X,t); Writeln (S1,' ', S2); End;End.
View Code

Exercise: Travel by car (multiply + pre-treatment)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.