noip2012 Travel by car

Source: Internet
Author: User

P1081 Travel by car
      • 139 through
      • 484 Submit
    • Topic provider Rokua Onlinejudge
    • Label multiplier noip Increase group
    • Difficulty Increase +/province selection-

Submit a discussion of the problem record

Title Description

Small A and small B decide to take a vacation trip, they will want to go to the city from 1 to N numbered, and the number of the smaller

Cities in the west of the larger number of cities, it is known that the altitude of each city is different, the altitude of the city I is

Hi, the distance between city I and City J D[i,j] 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

Select a city as the starting point, keep going east, and travel up to X kilometers to end the journey. Small A and small B

Different driving styles, small B always chooses a nearest city as a destination along the way, while small A is always along

The direction of the second near the city as a destination (note: If the current city to two cities distance

The same, it is considered to be closer to the city with a lower elevation). If any one of them is unable to choose the purpose according to their own principles

City, or arrive at a destination that will drive the total distance beyond 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 drive

The total distance of the journey is minimal (if the travel distance for small B is 0, the ratio can be considered infinite, and two infinity is considered equal). If you are departing from a number of cities, the total number of cars driving in small A is more than the total distance traveled by small B

Values are minimal, the city with the highest elevation is output.

    1. For any given X=xi and departure city Si, small A drive the total number of journeys and small B driving distance

Total.

Input output 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, representing the city 1 to the sea of the city N

Pull height, 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 given Si and

The total number of miles and the total number of miles traveled by small B under XI.

Input and Output Sample input example # #:
DRIVE14 2 3 1 4 3 4 1 3 2 3 3 3 4 3drive2 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 # # of output:
DRIVE11 1 1 2 0 0 0 0 0 Drive22 3 2 2 4 2 1 2 4 5 1 5 1 2 1 2 0 0 0 0 0
Description

"Input and Output Example 1 description"

The altitude of each city and the distance between the two cities are shown.

If you are departing from City 1, you can reach the city of 2,3,4, the city 1 distance from the city is 1,1,2,

But since the city's 3 altitude is below the city's 2, we think the city is 3 away from the City 1 recently, the City 2 away from the city

1 second near, so small A will walk to the City 2. After arriving in the City 2, the city ahead can reach 3, 4, these two cities

City and City 2 distance is 2, 1, so the city 4 away from the City 2 recently, so small B will go to the City 4. Arrival City

City 4, the front has no reachable city, so the trip is over.

If you are departing from City 2, you can reach the city of 3, 4, two cities and the City 2, respectively, 2, 1, by

In the City 3 away from the City 2 second near, so small A will walk to the City 3. After arriving in the city 3, the city that has not travelled before is

4, so the city 4 away from the City 3 recently, but if you want to reach the city 4, the total distance is 2+3=5>3, so small B will

The tour ends directly in the City 3.

If you are departing from City 3, you can reach the city of 4, because there is no City 3 second near the city, so travel

It's over before it starts.

If you depart from City 4, there is no city to reach, so the journey is over before it begins.

"Input and Output Example 2 description"

When X=7,

If you are departing from City 1, the route is 1, 2, 3, 8, 9, and small A walking distance of 1+2=3, small B Walk

The distance is 1+1=2. (In the City 1 o'clock, the nearest city to Little A is 2 and 6, but the city's 2 is higher, depending on

For with the City 1 second near the city, so small a finally choose the City 2; walk to 9, small a only city 10 can walk,

No 2nd choice, so you can't make a choice, end the trip.

If departing from City 2, the route is 2, 6, 7, the distance between small A and small B is 2, 4, respectively.

If departing from City 3, the route is 3, 8, 9, the distance between small A and small B is 2, 1, respectively.

If departing from City 4, the route is 4, 6, 7, the distance between small A and small B is 2, 4, respectively.

If departing from City 5, the route is 5, 7, 8, the distance between small A and small B is 5, 1, respectively.

If departing from City 6, the route is 6, 8, 9, the distance between small A and small B is 5, 1, respectively.

If departing from City 7, the route is 7, 9, 10, the distance between small A and small B is 2, 1, respectively.

If you are departing from City 8, the route is 8-10, and the distance between small A and small B is 2, 0, respectively.

National Information Science Olympic League (NOIP2012) rematch

Improve group Day1

7th Page Total 7 page

If you are departing from City 9, the route is 9, and the distance between small A and small B is 0,0 (The trip begins

Bundle up).

If you are departing from City 10, the route is 10, and the distance between small A and small B is 0, 0, respectively.

The total distance traveled from City 2 or City 4 to small A is the smallest of the total distance traveled by small B,

But the city is 2 above sea level, so the first act of output is 2.

"Data Range"

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, the data guarantees that hi is different.

NOIP 2012 Raising the first day of the group the third question

Analysis: This problem will not do, look at other people's problems to see the afternoon, is a little experience it, to share, first look at the data, it seems to be violent part of the points, each time you choose a point to start the simulation walk. Every time to find the first near and second near, very waste of time, then pretreatment, how to pre-treatment Somewhat complex, the nearest must be the height difference is similar, then can sort and look for in the similar elements of the data structure is what? STL's set! Find two precursors and successors each time, and then sort the top two. However, be aware that the insertion set is to be inverted, if a sequence is three-to-one, if you follow the interpolation, then the Begin () is a three-to-three, inverted plug is 3,2,1, because if the distance is going to go to high altitude, So I'm going to plug it backwards. But after this preprocessing still can't pass all the data, we come to the algorithm careful analysis, find the first near and second near optimization, the enumeration point does not seem to be optimized, then can only optimize the simulation walk. You'll find that two walks can be merged into one, is there any faster enumeration skills? Two points? It doesn't seem to be a good thing. Set F[i][j] for the position I walk 2^j wheel position, a round is a walk and B walk once, Fa[i][j] is a in position I walk 2^j wheel distance, Fb[i][j] similar, then how to calculate? Remember an important property: A^i-1 + a^i-1 = a^i, recursion can also be so, f[i][j] = f[f[i][j-1]][j-1],fa[i][j] = fa[i][j-1] + fa[f[i][j-1]][j-1],fb similar, because n <=100000, so 2^i,i starting from 19 enumeration, from large to small enumeration, in order to determine the line can not. (It's complicated ...)

#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<Set>using namespacestd;Const intMAXN =100005;intN, x0, M, K,x1, NA[MAXN], Nb[maxn],ans;Long LongAnsa = 1E5,ANSB = 0ll,fa[maxn][ -], fb[maxn][ -], f[maxn][ -];structnode{intID, high; BOOL operator< (ConstNode & B)Const {        returnHigh <B.high; }}C[MAXN];structnode2{intID, Gaoducha; BOOL operator< (ConstNode2 & B)Const {        if(Gaoducha! =B.gaoducha)returnGaoducha <B.gaoducha; Else            returnC[id].high <C[b.id].high; }}temp[5];Set<node>s;voidInitinti) {    Set<node>:: Iterator it =S.find (C[i]); intj =0; if(It! =S.begin ()) {        --it; temp[++J] = (node2) {it->id, ABS (It->high-C[i].high)}; if(It! =S.begin ()) {            --it; temp[++J] = (node2) {it->id, ABS (It->high-C[i].high)}; ++it; }        ++it; }    if((++it)! = S.end ())//S.end () is after the end!{temp[++J] = (node2) {it->id, ABS (It->high-C[i].high)}; if((++it)! =s.end ()) temp[++J] = (node2) {it->id, ABS (It->high-C[i].high)}; } Sort (Temp+1, temp + j +1); Nb[i]= temp[1].id; if(J = =1)        return; Na[i]= temp[2].id; return;}voidQueryintStintXLong Long&ta,Long Long&TB) {     for(inti = +; I >=0; i--)        if(F[st][i] && fa[st][i] + fb[st][i] <=x) {ta+=Fa[st][i]; TB+=Fb[st][i]; X-= Fa[st][i] +Fb[st][i]; St=F[st][i]; }    if(!Na[st])return; intTempans = ABS (C[na[st]].high-C[st].high); if(Tempans <=x) Ta+=Tempans;}intMain () {scanf ("%d", &N);  for(inti =1; I <= N; i++) {scanf ("%d", &C[i].high); C[i].id=i; }     for(inti = n; i;i--) {S.insert (c[i]); if(I! =N) init (i); }     for(inti =1; I <= N; i++)    {        intP1 = Na[i], p2 =Nb[na[i]]; fa[i][0] = P1? ABS (C[p1].high-c[i].high):0; fb[i][0] = P2? ABS (C[p2].high-c[p1].high):0; f[i][0] =P2; }     for(intj =1; J < -; j + +)//after the update is J so first enumerate J         for(inti =1; I <= N; i++) {F[i][j]= F[f[i][j-1]][j-1]; FA[I][J]= Fa[i][j-1] + fa[f[i][j-1]][j-1]; FB[I][J]= Fb[i][j-1] + fb[f[i][j-1]][j-1]; } scanf ("%d", &x0); Ans=0;  for(inti =1; I <= N; i++)    {        Long LongTA = 0ll, TB =0LL;        Query (i, x0, TA, TB); if(TB && (!ans | | ANSA * TB > ANSB *ta)) {Ansa=Ta; ANSB=TB; Ans=i; }} printf ("%d\n", ans); scanf ("%d", &m);  while(m--) {scanf ("%d%d", &k, &x1); Long LongTA = 0ll, TB =0LL;        Query (k, X1, TA, TB); printf ("%d%d\n", TA, TB); }    return 0;}

Orz God Ben Raffica

noip2012 Travel by car

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.