Spiderman poj-1925

Source: Internet
Author: User

DP

D [I] indicates that it takes at least a few steps from apartment to x-axis I .. P [I] the abscissa of building I, the ordinate of building I in H [I]

Note that the ordinate of Spiderman is always the height of the apartment !! So you don't need to worry about the ordinate !!

Traverse every building ..

The following requirements must be met when building I can be skipped from X coordinate J: (P [I]-j) ^ 2 <= H [I] ^ 2-(H [I]-H [1]) ^ 2

After the x-axis J passes through the building I, it reaches the x-axis 2 * P [I]-J ..

In summary, d [2 * P [I]-J] = min (d [2 * P [I]-J], d [J] + 1 )...

Finally, pay attention to the number of steps to reach the West tower.

/*
* 1925.cpp
*
* Created on: 2011-7-8
* Author:
*/
# Include <cstdio>
# Include <cmath>
# Include <cstring>
Using namespace STD;

Const int maxn = 5000 + 5;
Const int max_x = 2000000 + 5;
Constint INF = 1000000;

Int N;
Int P [maxn], H [maxn];
Double scale [maxn];
Int d [max_x];

Int main (){
Int tot;
Scanf ("% d", & ToT );
While (TOT --){
Scanf ("% d", & N );

For (INT I = 1; I <= N; I ++ ){
Scanf ("% d", & P [I], & H [I]);
// Scale [I] = SQRT (double) H [I] * H [I]-(H [I]-H [1]) * (H [I]-H [1]); // do not use SQRT; otherwise, TLE !! (Square is used for comparison later)
Scale [I] = H [I] * H [I]-(H [I]-H [1]) * (H [I]-H [1]);
}

Memset (D,-1, sizeof (INT) * max_x); // it can also be a for loop .. D [I] = inf to save time, set it to-1 and then judge ..
D [p [1] = 0;

//......
For (INT I = 2; I <= N; I ++ ){
For (Int J = P [I]-1; j> = P [1]; j --){
If (d [J] =-1) continue ;//!!!! Skip directly ~

Int Gap = P [I]-J;
If (GAP * gap> scale [I]) break; // compare the sum of squares of gap and Scale

Int aim = 2 * P [I]-J;
If (d [aim] =-1 | D [aim]> d [J] + 1 )){
D [aim] = d [J] + 1;
}

If (aim> = P [N] & (d [p [N] =-1 | D [p [N]> d [aim]) {// final processing
D [p [N] = d [aim];
}
}
}

If (d [p [N] <= 0)
Printf ("-1 \ n ");
Else
Printf ("% d \ n", d [p [N]);

}

Return 0;
}

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.