Sunday, 19 January 2014

UVa 837 - Light and Transparencies




 Question

 Solution -
#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;

struct point{
    double x;
    int tci; //  index value of tcs[]
};

struct tc{ //transmission coefficients
bool effective;
double r;
};

bool compare_point(const point& p , const point& q)
{
    return p.x<q.x;

}

int main(){
    int t;
    cin >> t;
    while(t--){
        int nl;
        cin >> nl;
        int np = nl<<1;
        vector <point> points(np);
        vector <tc> tcs(nl);
        for(int i=0;i<nl ;++i){
            double y1,y2;
            cin >> points[i<<1].x>> y1 >> points[i<<1 +1].x>> y2 >> tcs[i].r;
            points[i<<1].tci= points[i<<1+1].tci = i;
            tcs[i].effective = false;
        }
        sort(points.begin(),points.end(),compare_point);
        cout << np+1 << endl;
        cout << fixed;
        for(int i =0;i<=np ; ++i)
        {
            if(i)
            cout << setprecision(3) << points[i-1].x;
            else
            cout << "-inf";
            if(i<np)
            cout<< ' ' << setprecision(3) << points[i].x;
            else
            cout << " +inf";
            double pl = 1.0 ;//%age of light
            if(i && i<np){
                for(int j =0 ;j<nl;++j)
                {
                    if(tcs[j].effective){
                        pl*=tcs[j].r;
                    }
                }
            }
                cout << ' ' << setprecision(3) << pl << endl;
                if(i<np)
                tcs[points[i].tci].effective = !tcs[points[i].tci].effective;
          
      
        }
        if(t)
        cout << endl;
  
    }
return 0;
}

No comments:

Post a Comment