Saturday, October 31, 2015

Dr Wu 5 uva 2294 (Live)

Easy Binary Search Problem .

Code : -

Complexity : O(log2(range)) per 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
62
using namespace std;
double arr[1005];
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()
{
//pre-processing
arr[0] = 0.000;

loop(x,1,1000)
{
    //For x number of cards you will get overhang of arr[x]
    arr[x] = arr[x-1] + (double)(1.00000/(x+1))*1.0000 ;
}
while(true){
int num_cards; // Binary search on number of cards
double actual_overhang;
cin>>actual_overhang;
if(comp(actual_overhang,0.00000)==0)
{
    return 0 ;
}
int lo_cards = 0;
int hi_cards = 900; //pretty safe to assume
while(lo_cards<=hi_cards)
{
    int mid_cards = (lo_cards + hi_cards )/2;
    int mid_cards_plus_extra = mid_cards + 1;
    double lower_overhang = arr[mid_cards];
    double hi_overhang = arr[mid_cards_plus_extra];
    if(comp( lower_overhang,actual_overhang ) == -1 && comp(hi_overhang,actual_overhang)==1)
    {
        num_cards = mid_cards_plus_extra;
        break;
    }
    if(comp(lower_overhang,actual_overhang)==0)
    {
        num_cards = mid_cards;
        break;
    }
    if(comp(lower_overhang,actual_overhang)==-1) //Too less cards I have guess
    {
        lo_cards = mid_cards;
    }
    if(comp(lower_overhang,actual_overhang)==1  ) //Too many cards I have guessed
    {
        hi_cards = mid_cards;
    }

}

pf("%d card(s)\n",num_cards);
}
    return 0;
}

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;
}

Friday, October 23, 2015

Dr Wu 3 uva 3399 (Live)


Problem Link

Sieve + Prefix Sum + Binamry Search (STL Map)

Complexity : O(sieve) + O(X*log(X))  ; where X = number of prime numbers in the range

Code :-
 
 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
62
63
64
65
66
67
68
69
70
71
using namespace std;
bool isPrime[10009];
int prefix_sum[10009];
map<int,int>Bin_Search;
int num_primes;
void sieve(int N)
{
    int sqN = sqrt(N) + 5;
    for(int i = 2; i<=sqN; i++)
    {
        if(isPrime[i])
        {
            num_primes++;

            for(int j=2*i; j<=N; j+=i)
            {
                isPrime[j] = false;

            }

        }

    }
}
int main()
{

    ms(isPrime,true);
    num_primes = 0;
    sieve(10002);


    int idx = 1;
    prefix_sum[0] = 0;
    loop(x,2,10002)
    {
        if(isPrime[x])
        {
            prefix_sum[idx] = prefix_sum[idx-1] + x;
            int val = prefix_sum[idx];
            Bin_Search[val] = 1;
            idx++;

        }

    }

    while(true)
    {
        int inp;
        cin>>inp;
        if(inp==0)
            return 0;
        int ans = 0;
        loop(x,1,idx-2)
        {
            int leftToSearch = prefix_sum[x] - inp;
            if(leftToSearch==0)
                ans++;
            if(leftToSearch<0)
                continue;
            if(Bin_Search[leftToSearch]!=0)
            {
                ans++;
            }
        }

        cout<<ans<<endl;
    }
    return 0;
}

Saturday, October 10, 2015

Dr Wu 2 uva 2787

Loop style :
Style 1 ->
                       loop(x = 1 to N)
                          {
                               loop(y=1 to N)
                                    {
                                    }
                          }

(x,y) = 1 1 , 1 2 , 1 3, ....,2 2,......,3 3,.....N N
Style 2 ->
                       loop(x = 1 to N)
                          {
                               loop(y=x+1 to N)
                                    {
                                    }
                          }

(x,y) = 1 2 , 1 3 ,...,1 N,2 3 , 2 4 ,........ ,N-1 N  [x!=y]
If on a same list then Style 1 will include such (x,y) pairs that x==y meaning same index twice in the pair.
The second style only considers NC2 pairs meaning only 2-subsets and is useful in some problems.

Code :-


 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
using namespace std;
int arr[1000];
int main()
{

while(true)
{
    int idx = 1;

int temp = 5;
while(true)
{
    cin>>temp;
    if(temp==0)
        break;
    if(temp==-1)
        return 0;
    arr[idx++] = temp;

}
idx--;
int ans = 0;
for(int x  =1;x<=idx;x++)
{
    for(int y=x+1;y<=idx;y++)
    {
        if( arr[x] == 2*arr[y] || arr[y]==2*arr[x] )
            ans++;
    }
}

   cout<<ans<<endl;
}


    return 0;
}

Wednesday, October 7, 2015

Dr Wu 1 uva 2362

Code : -


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    double sum = 0.0;
    for(int i = 1;i<=12;i++)
    {
        double temp;
        cin>>temp;
        sum+=temp;
    }
    printf("$%.2f\n",sum/12*1.000000);
    return 0;
}