Showing posts with label Geometry. Show all posts
Showing posts with label Geometry. Show all posts

Thursday, 4 February 2021

Triangle Partitioning Lightoj 1043

 We know that,

Area of triangle ABC=sqrt(s*(s-ab)*(s-ac)*(s-bc));

so, we can find the height of the triangle by area=1/2 * base*height

Then we will find the position of DE by doing binary search. Then we will find the Distance A to D. 

 /// Bismillahir Rahmanir Rahim
/* Mohammad Morsalin
   Dept of ICE, NSTU
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define mp make_pair
#define endl "\n"
#define int long long
#define f0(n) for(int i=0;i<n;i++)
#define ms(x) memset(x,0,sizeof(x))
#define ms2d(x,m,n) memset(x, 0, sizeof(x[0][0]) * m * n)
#define uniq(vec) vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end())))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define pi pair<int,int>
#define tc(t) int t;cin>>t;while(t--)
#define bits(n) __builtin_popcount(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int> >
#define ins insert
#define ALL(v) v.begin(),v.end()
#define highest(x) numeric_limits<x>::max()
#define lowest(x) numeric_limits<x>::min()
#define Inf INFINITY
#define minv(v) *min_element(v.begin(),v.end())
#define maxv(v) *max_element(v.begin(),v.end())
#define fi first
#define se second
#define PI acos(-1)
#define sz(a) (int)a.size();
#define IOS ios::sync_with_stdio(false);
using namespace std;
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
const long long MOD = 1000000007;
double sq(double x) {return x*x;}
template<typename T>inline T Bigmod(T base, T power, T MOD){
    T ret=1;
    while(power)
    {
        if(power & 1)ret=(ret*base)%MOD;
        base=(base*base)%MOD;
        power>>=1;
    }
    return ret;
}

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}
double const range=1e-10;

signed main()
{
    IOS
    int tn=0;
    tc(t){

        double ab,ac,bc,rat;
        cin>>ab>>ac>>bc>>rat;
        double s=(ab+bc+ac)/2;
        double ABC=sqrt(s*(s-ab)*(s-ac)*(s-bc));
        double theta=asin(ABC/(ab*bc));
        double highet=(ABC*2)/bc;
        double DEBC=ABC/(rat+1);
        double ADE=(ABC-DEBC);
        double low=0,high=bc,H=0,de;
        while(fabs(highet-H)>range){
            de=(low+high)/2;
            H=((2*ADE)/de)+((2*DEBC)/(de+bc));
            if(H>highet+range) low=de;
            else high = de;
        }
        double ad=(ADE/(de*sin(theta)));
        cout << "Case "<< ++tn << ": ";
        cout << fixed<<setprecision(10) << ad << endl;
       // printf("Case %d: %0.10lf\n",++tn,ad);
    }
    return 0;
}
///Alhamdulillah


Circle in Square Lightoj 1022

 The equation used in this problem is:

Shaded area= area of the Square - Area of the circle

#include<iomanip>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int t,i;
    cin>>t;
    for(i=1;i<=t;i++){
        double r,pi,ca,sa,ra;
        cin>>r;
        pi=2*acos(0.0);
       ca=pi*(r*r);
       sa=(2*r)*(2*r);
       ra=sa-ca;
        cout<<"Case "<<i<<": "<<fixed<<setprecision(2)<<ra<<endl;
}
    return 0;
}

Tuesday, 16 April 2019

Juice in the Glass LightOj 1216

In this problem we need to just find a equation,
a equation called conic  frustrum volume:
         
           V  = (pi*h /3) *(R*R + R*r + r*r)

code(c++):

/*
Author : Mohammad Morsalin Hossain
Dept of ICE,NSTU
*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define pi acos(-1)
#define IOS ios::sync_with_stdio(false);
using namespace std;
int main()
{
    IOS
    int test,to=0;
    cin>>test;
    while(test--)
    {
        int r1,r2,h,p;
        cin>>r1>>r2>>h>>p;
        double pr= r2 +(r1-r2)*((double)p/h);
        double v;
        v = (pi*p)*(((double)pr*(double)pr)+((double)pr*(double)r2)+((double)r2*(double)r2))/3.0;
        printf("Case %d: %lf\n",++to,v);
    }
    return 0;
}



Intersection of Cubes LightOj 1211

To calculate intersection volume of n cubes, we need to find maximum value of first corner coordinates (x1,y1,z1) and minimum value of another corner coordinates(x2,y2,z2). There will be intersection between these cubes if and only if the difference between (x2-x1),(y2-y1),(z2-z1) is positive.

Code(c++):
/*
Author : Mohammad Morsalin Hossain
Dept of ICE,NSTU
*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ms(x) memset(x,0,sizeof(x))
#define ins insert
#define IOS ios::sync_with_stdio(false);
using namespace std;
int main()
{
    IOS
    int test,tc=0;
    cin>>test;
    while(test--)
    {
        int n,a=0,b=0,c=0,d=INT_MAX,e=INT_MAX,f=INT_MAX;
        cin>>n;
        for(int i = 0;i<n;i++)
        {
            int x1,y1,z1,x2,y2,z2;
            cin>>x1>>y1>>z1>>x2>>y2>>z2;
            a = max(a,x1);
            b = max(b,y1);
            c = max(c,z1);
            d = min(d,x2);
            e = min(e,y2);
            f = min(f,z2);
        }
        if(d>a && e>b && f>c)
        {
            ll res = (d-a)*(e-b)*(f-c);
            cout << "Case "<< ++tc<< ": "<<res <<endl;
        }
        else
            cout << "Case "<< ++tc <<": 0\n";
    }
    return 0;
}

Monkey Banana Problem lightoj 1004

  In this problem we will check which adjacent cell will benefits the monkey best. For this, we will check all possible solution of this pro...