Multiply
Https://www.luogu.org/problem/P1081 description link] [luogu
Description [title]
A small small B and decided to use the holiday to travel, they will want to go to the city from 1 to N number and the number of smaller cities in the west of the large number of the city, known to all the city's altitude different from each other, remember city elevation is between the Hi i, j urban city i and distance D [i, j] is exactly the absolute value of difference of these two cities altitude, i.e., d [i, j] = | Hi-Hj | .
Travel process, small A and small B turns driving, the first day of A small car, after rotation once a day. They plan to select a city as a starting point S, has been traveling east, and travel up to the end of X kilometers to travel. A different small and small B driving style, a small B always choose the nearest city as a destination in the forward direction, and always choose A small forward along the direction of the second near the city as a destination (in this question if the current from the city to the same two cities, it is considered closer to the city's low altitude). If a person can not choose any destination city in accordance with their own principles, or destination would exceed the total distance traveled X kilometers, will end their trip.
Prior to departure, wondering A small two questions:
(1) for a given X = X0, from which a departure city, the ratio of the total distance A small car with a small distance B with the total number of minimum (if small travel distance B is 0, then the ratio can be considered infinite, and both are considered equal to infinity). If starting from a number of cities, the ratio of the total distance traveled by car from the small A small B and the total number of travel are minimized, the city's highest output.
(2) Xi departure city and Si, the total distance traveled by car and the small A total distance for any given X = small B traveling.
[Enter a description]
The first row contains an integer N, the number of cities;
The second line has N integers, each separated by a space between two integers, respectively for city to city altitude 1 N, i.e., H1, H2, ······, Hn, Hi and are each It is different;
The third row contains an integer X0;
A fourth line integer M, M represents a given Si and Xi;
The next M lines, each containing two integers Xi and Si, Si represents a departure from the city, most travel Xi kilometers.
Description [output]
The output common line M + 1;
The first row contains an integer S0, means that for a given X0, numbered from S0 departure city, from the minimum ratio of the total distance traveled by car A small total number of the small traveling B;
The next M rows, each row comprising two integers, separated by a space between successively represents the total number in a given mileage Si Xi and A with a small total mileage traveled and small B.
[Sample input]
Example 1:
4
2 3 1 4
3
4
1 3
2 3
3 3
4 3
Sample 2:
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]
Example 1:
1
1 1
2 0
0 0
0 0
Sample 2:
2
3 2
2 4
2 1
2 4
5 1
5 1
2 1
2 0
0 0
0 0
Tucao strong pretreatment, and long long overflow at any time
#include<bits/stdc++.h> #define re return #define ll long long #define inc(i,l,r) for(int i=l;i<=r;++i) #define dec(i,l,r) for(int i=l;i>=r;--i) const ll maxn=100005,inf=1e11; using namespace std; template<typename T>inline void rd(T&x) { char c;bool f=0; while((c=getchar())<'0'||c>'9')if(c=='-')f=1; x=c^48; while((c=getchar())>='0'&&c<='9')x=x*10+(c^48); if(f)x=-x; } ll n,pos[maxn],nt[maxn][2]; ll f[maxn][28],ff[maxn],dis[maxn][28][2],dd[maxn]; double ans,da,db; struct node{ ll val; int id,lf,rf; bool operator<(node a)const { Re Val < a.val; } } H [MAXN]; inline int Check ( int X, int Y, int Z) // Check near the city { IF (ABS (H [X] .val-H [Z ] .val) <= ABS (H [Y] .val- H [Z] .val)) X Re; Re Y; } // ------------------ --------- pretreatment ------------------------- inline void PreS () { Sort (H + . 1 , H + + n- . 1 ); inc is (I, . 1 , n-) { POS [H [I] .id] =I; H [I] .lf = I- . 1 ; H [I] .rf = I + . 1 ; } // doubly linked list sorted int L, R & lt; inc is (I, . 1 , N- . 1 ) { int P = POS [ I]; L = H [P] .lf; R & lt = H [P] .rf; NT [I] [ . 1 ] Check = (L, R & lt, P); // small opening to the nearest point B IF (NT [ I] [ . 1 ] == L) NT [I] [ 0 ] = Check (H [L] .lf, R & lt, P); the else NT [I] [ 0 ] = Check (L, H [R & lt] .rf , P); // initializes the first day of if(nt[i][0]) { f[i][0]=h[nt[i][0]].id; dis[i][0][0]=abs(h[nt[i][0]].val-h[p].val); } if(nt[i][1]) { ff[i]=h[nt[i][1]].id; dd[i]=abs(h[nt[i][1]].val-h[p].val); } if(l)h[l].rf=r; if(r)h[r].lf=l; } } //----------------------- doubled ------------------------- inline void Bz () { inc is (I, . 1 , n-) { F [I] [ . 1 ] = FF [F [I] [ 0 ]]; DIS [I] [ . 1 ] [ 0 ] = DIS [I] [ 0 ] [ 0 ]; DIS [I] [ . 1 ] [ . 1 ] dd = [F [I] [ 0 ]]; // day A small opening, then the next day, a small opening B } inc is (J, 2 , 27 ) inc is (I, . 1 , n-) { // so //The first day was finished 4,8,16 ......, A small opening remains F [I] [J] = F [F [I] [J- . 1 ]] [J- . 1 ]; DIS [I] [J] [ 0 ] = DIS [I] [J- . 1 ] [ 0 ] + DIS [F [I] [J- . 1 ]] [J- . 1 ] [ 0 ]; DIS [I] [J] [ . 1 ] = DIS [I] [J- . 1 ] [ . 1 ] + DIS [F [I] [J- . 1 ]] [J- . 1 ] [ . 1 ]; } } // ------------- A small multiplication query ----------- small distance B traveled ---------------------------- inline void ASK (X LL, int A) { On Dec (I,27,0) if(f[a][i]&&x>=dis[a][i][0]+dis[a][i][1]) { x=x-dis[a][i][0]-dis[a][i][1]; da+=dis[a][i][0]; db+=dis[a][i][1]; a=f[a][i]; } } inline void X0() { int x,ansp=0; ans=inf; //ans为比值 //ansp为最佳位置 rd(x); inc is (I, . 1 , n-) { DA = DB = 0 ; // DA A small traveled // DB small B traveled ASK (X, I); IF (DB == 0 ) { IF (ANS == INF && H [POS [I]] Val>. H [POS [ANSP]] Val.) ANSP = I; } the else IF (DA / DB <ANS) ANS = DA / DB, ANSP = I; the else IF (DA / DB ANS && == H [POS [I]] Val>. H [POS [ANSP]] Val.) ANS = DA / DB, ANSP = I; } the printf ( " % D \ n- ",ansp); } int main() { freopen("in.txt","r",stdin); h[0].val=-inf; rd(n); inc(i,1,n) { rd(h[i].val); h[i].id=i; } h[n].rf=0; Pre(); Bz(); X0(); int x,opt; rd(n); while(n--) { da=db=0; rd(opt),rd(x); ask(x,opt); printf("%.0lf %.0lf\n",da,db); } re 0; }