বুধবার, ১৫ জুন, ২০১৬

LOJ Bisection + Geometry 1137


Handling Tricky / Corner cases while bisecting is important .
When , some parameters are extremely small 0   ,   -THRESHOLD or too big   +THRESHOLD
It's  a good idea to check them separately to avoid WA s .


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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
/*
Idea:
Always look into triangles
You need a function F such that
F(h) = L'
Now,idea is Work with radius of the circle
Find R
Then , S = R*theta
and , L' = S
So,from h I need to find R , work with the triangle adjacent to circle
do some replacement and
    (x+h)^2 = x^2 + h^2


acos() function returns in radian ... Wasted some time on this
*/
double L,n,C,S;
double F(double h)
{
    double l = L/2.00;
    double x = ((l*l)-(h*h))/(2.00*h);
    double R = x+h;

    double expr = ( 2.00*(R*R) - (L*L)) /(2.00*R*R) ;

    double angA = acos( expr ); //This is already in radians


    S = R*angA;

    return S;
}
int main()
{
    //write();
    int tc,cas =0;
    sfi(tc);
    while(tc--)
    {


        cin>>L>>n>>C;

        double h_lo = 0.0000000;
        double h_hi = L/2.000000;
        double h_mid;

        double L_p = (1.00+n*C)*L; 
        if(L==0.00 || n==0.00 || C==0.00000) //This was IMPORTANT
        {                         //Without it Got WA
            h_mid = 0.00;
        }
        else
        
        while(h_lo<=h_hi)
        {
            h_mid = (h_hi+h_lo)/2.00000;

            if(fabs(h_hi-h_lo)<EPS)
                break;
            F(h_mid);
            if( S > L_p )
            {
                //too much h
                h_hi = h_mid;

            }
            else
            {
                h_lo = h_mid;

            }




        }


        CASE(cas);
        pf("%.8lf\n",h_mid);

    }

    return 0;
}

EASY CF 279 A Spiral






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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
/*
IDEA:
Simulating a spiral is tricky .I simulated this earlier and
had to handle many tricky cases.
Now,I have come up with much easier solution with
only 4 cases .
See the figure in my blog to understand.

*/

int main()
{
    int x,y;
    sfii(x,y);

    int max_layer; //Which layer I'm in actually
    int layer = max(abs(x),abs(y));

    int xp = 1 , xn = 2 , yp = 3 , yn = 4;
    if(x>0 && ( (y>=0 && abs(x)>=abs(y)) || (y<0 && (abs(x) - abs(y))>=2))) // less symmetric side
    {
        max_layer = xp;
    }
    else if(x<0 && (abs(x)>=abs(y))) //much more symmetric side
    {                             //taking advantage of extra handling to make symmetric
        max_layer = xn;
    }
    else if(y>0 && (abs(y)>=abs(x))) // another symmetric side
    {
        max_layer = yp;
    }
    else //less symmetric  side
    {
        max_layer = yn;
    }
    int ans;





    //Another approach


    if(x==0 && y==0)
    {
        ans = 0; //special case
    }
    else if(x>0 && y<=0 && x==abs(y)+1) // another special case
    {
        ans = (layer-1)*4;
    }
    else if(x<0 && y>=0 && abs(x)==y)
    {
        ans = (layer-1)*4 + 2; // safety
    }
    else if(x>0 && y>0 && (x==y))
    {
        ans = (layer-1)*4 + 1;
    }
    else if(x<0 && y<0 && x==y)
    {
        ans = (layer-1)*4 +3;
    }
    else if(max_layer==xp)
    {
        ans = (layer-1)*4 + 1;
    }
    else if(max_layer==yp)
    {
        ans = (layer-1)*4 + 2;
    }
    else if(max_layer==xn)
    {
        ans = (layer-1)*4 + 3;
    }
    else
    {
        ans = (layer-1)*4 + 4;
    }



    pf("%d\n",ans);

    return 0;
}

EASY CF 157 B


  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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
/*
IDEA:
Just take the radius , sort .
And start adding areas of the disc +=2
*/
vector<int>radius;
int main()
{
    int N;
    sfi(N);
    loop(i,1,N)
    {
        int tmp;
        sfi(tmp);
        radius.pb(tmp);
    }
    radius.pb(0); //The center //for generalization in code
    sort(all(radius));

    double area = 0.00000000;
    if(N%2==1)
    for(int i=1;i<=SZ(radius)-1;i+=2)
    {
        int curRad = radius[i];
        int lastRad = radius[i-1];
        area+=PI*(sqr(curRad)*1.0000 - sqr(lastRad)*1.0000) ;
    }
    else
    for(int i=2;i<=SZ(radius)-1;i+=2)
    {
        int curRad = radius[i];
        int lastRad = radius[i-1];
        area+=PI*(sqr(curRad)*1.0000 - sqr(lastRad)*1.0000) ;
    }

    pf("%.9lf\n",area);

    return 0;
}

EASY CF 6 A


  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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
/*
IDEA:
A degenerate triangle is the "triangle" formed by
three collinear points. It doesn't look like a
triangle, it looks like a line segment. A parabola
may be thought of as a degenerate ellipse with
one vertex at an infinitely distant point.
______.___________   It's a degenerate triangle As sides are 3 , 5 , 8
3 + 5 = 8
*/
int tri_type(int x,int y,int z)
{
    int mx = max(max(x,y),z);
    int mn_sum = x+y+z - mx;
    if(mn_sum>mx)
    {
        return 1; // good triangle
    }
    else if(mn_sum==mx)
    {
        return 2; // Degenerate triangle
    }
    else
    {
        return 3; //IMPOSSIBLE to construct a triangle
    }

}
int main()
{
    int a,b,c,d;
    sfii(a,b);
    sfii(c,d);
    //4C3 = 4 possible 3 tuples is a possibility for a triangle
    int pos1 = tri_type(a,b,c);
    int pos2 = tri_type(a,b,d);
    int pos3 = tri_type(a,c,d);
    int pos4 = tri_type(c,b,d);
    //First priority triangle 1 > Then Degenerate > Then Impossible

    if(pos1==1 || pos2==1 || pos3==1 || pos4==1)
    {
        cout<<"TRIANGLE"<<endl;
    }
    else if(pos1==2 || pos2==2 || pos3==2 || pos4==2)
    {
        cout<<"SEGMENT"<<endl;
    }
    else
    {
        cout<<"IMPOSSIBLE"<<endl;
    }

    return 0;
}

EASY CF 127 A


  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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
/*
IDEA:
Simply add all line distance .
Then , time = distance / speed
*/
int main()
{
    int n,k;
    sfii(n,k);

    int lastX,lastY;
    int curX,curY;

    sfii(lastX,lastY);


    double dist_lines = 0.00000000f;


    loop(i,1,n-1)
    {
        sfii(curX,curY);
        double diffX = (curX - lastX)*1.0000000;
        double diffY = (curY - lastY)*1.0000000;
        dist_lines+=hypot(diffX,diffY);
        lastX = curX;
        lastY = curY;
    }

    dist_lines*=(k*1.00000000);

    double time = (dist_lines/50.00000000000);

    pf("%.9lf\n",time);

    return 0;
}

EASY CF 270 A


  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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
/****************************************

@_@
Cat Got Bored *_*
#_#
*****************************************/

#include <bits/stdc++.h>


#define loop(i,s,e) for(int i = s;i<=e;i++) //including end point

#define pb(a) push_back(a)

#define sqr(x) ((x)*(x))

#define CIN ios_base::sync_with_stdio(0); cin.tie(0);

#define ll long long

#define ull unsigned long long

#define SZ(a) int(a.size())

#define read() freopen("input.txt", "r", stdin)

#define write() freopen("output.txt", "w", stdout)


#define ms(a,b) memset(a, b, sizeof(a))

#define all(v) v.begin(), v.end()

#define PI acos(-1.0)

#define pf printf

#define sfi(a) scanf("%d",&a);

#define sfii(a,b) scanf("%d %d",&a,&b);

#define sfl(a) scanf("%lld",&a);

#define sfll(a,b) scanf("%lld %lld",&a,&b);

#define sful(a) scanf("%llu",&a);

#define sfulul(a,b) scanf("%llu %llu",&a,&b);

#define sful2(a,b) scanf("%llu %llu",&a,&b); // A little different

#define sfc(a) scanf("%c",&a);

#define sfs(a) scanf("%s",a);


#define mp make_pair

#define paii pair<int, int>

#define padd pair<dd, dd>

#define pall pair<ll, ll>

#define fs first

#define sc second

#define CASE(t) printf("Case %d: ",++t) // t initialized 0

#define cCASE(t) cout<<"Case "<<++t<<": ";

#define INF 1000000000   //10e9

#define EPS 1e-9

#define flc fflush(stdout); //For interactive programs , flush while using pf (that's why __c )

using namespace std;
/*
IDEA:
we know , for any regular polygon
sum of angles , (n-2)*180
If, all angles are equal then , = ((n-2)*180)/n [n angles]
(n-2)*180/n = A
so, n = 360/(180-A)
*/
int main()
{
    int T;
    sfi(T);
    while(T--)
    {
        int A;
        sfi(A);
        if(360%(180-A)==0)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }


    }



    return 0;
}