Wednesday, October 28, 2015

Dr Wu 4 uva 2363

Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to erosion caused by the Mississippi River. Since Fred is hoping to live in this house the rest of his life, he needs to know if his land is going to be lost to erosion.
After doing more research, Fred has learned that the land that is being lost forms a semicircle. This semicircle is part of a circle centered at (0, 0), with the line that bisects the circle being the X axis. Locations below the X axis are in the water. The semicircle has an area of 0 at the beginning of year 1. (Semicircle illustrated in the Figure.)
$\textstyle \parbox{.5\textwidth}{
\begin{center}
\mbox{}
\epsfxsize=2.5in \epsfbox{p2363a.eps}
\end{center}}$$\textstyle \parbox{.49\textwidth}{
\begin{center}
\mbox{}
\epsfxsize=2.5in \epsfbox{p2363b.eps}
\end{center}}$

Input 

The first line of input will be a positive integer indicating how many data sets will be included (N).
Each of the next N lines will contain the X and Y Cartesian coordinates of the land Fred is considering. These will be floating point numbers measured in miles. The Y coordinate will be non-negative. (0, 0) will not be given.

Output 

For each data set, a single line of output should appear. This line should take the form of:


Property N: This property will begin eroding in year Z.


Where N is the data set (counting from 1), and Z is the first year (start from 1) this property will be within the semicircle AT THE END OF YEAR Z. Z must be an integer. After the last data set, this should print out `END OF OUTPUT.'.


Notes:

  1. No property will appear exactly on the semicircle boundary: it will either be inside or outside.
  2. This problem will be judged automatically. Your answer must match exactly, including the capitalization, punctuation, and white-space. This includes the periods at the ends of the lines.
  3. All locations are given in miles.

Sample Input 

2
1.0 1.0
25.0 0.0

Sample Output 

Property 1: This property will begin eroding in year 1.
Property 2: This property will begin eroding in year 20.
END OF OUTPUT. 
 
Easy problem but can be tricky .
 
Code : -
 
Complexity : O(1) per test case.
 
 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using namespace std;

int main()
{
    int Property_No;
    cin>>Property_No;
    for(int p = 1; p<=Property_No; p++)
    {
        int year;
        double x,y;
        cin>>x>>y;
        double r = sqrt( (x*x) + (y*y)  ); //Finding the radius of the semicircle
        double area = PI*(r*r)/2*1.000; //area of semicircle
        //In k years we will have actual area k*50 (each year area += 50)
        //So, area_p = k*50 // we find the lower bound of area_p
        //Then , k = ceil(area_p/50) ;
        year = ceil( area/50*1.000  );
        pf("Property %d: This property will begin eroding in year %d.\n",p,year);


    }
    pf("END OF OUTPUT.\n");


    return 0;
}


Approach 2 :
Code :-

Complexity : O(log2(range)) per test case



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using namespace std;
int comp(double a,double b)
{
    double diff = a - b;
    if(diff>EPS)
        return 1; //a is bigger
    if(diff<-EPS)
        return -1; // b is bigger
    else return 0; //equal
}
int main()
{
    int Property_No;
    cin>>Property_No;
    for(int p = 1; p<=Property_No; p++)
    {
        int year;
        double x,y;
        cin>>x>>y;
        double r = sqrt( (x*x) + (y*y)  ); //Finding the radius of the semicircle
        double actual_area = PI*(r*r)/2*1.000; //area of semicircle



        //Binary search on year
        int year_lo = 0;
        int year_hi = 999999;
        int ans_year = 0;
        while(year_lo<=year_hi)
        {
            //In the last code I implemented a buggy binary search
            //Never do this ... if(..){low = mid+1;}...if(..){hi = mid+1;} //Both are mid+1 that's a bug
            //Instead always do ..lo = mid; hi = mid; ....That's safer
            int year_mid = ( year_hi + year_lo )/2;
            //If this year s area is small but next years area is big that's the answer
            int year_mid_next = year_mid + 1;
            double area1 = (year_mid*50); //each year area is increasing by 50
            double area2 = (year_mid_next*50);
            if(  comp(area1,actual_area) == -1 && comp(area2,actual_area) == 1 )
            {
                ans_year = year_mid_next;
                break;
            }
            if(comp(area1,actual_area)==1) //year is too big thats why area is also big
            {
                year_hi = year_mid ;
            }
            if(comp(area1,actual_area)==-1)
            {
                year_lo = year_mid ; //year is too small thats why area is also small
            }

        }
pf("Property %d: This property will begin eroding in year %d.\n",p,ans_year);

    }
    pf("END OF OUTPUT.\n");


    return 0;
}

No comments:

Post a Comment