Saturday, May 31, 2014

Topcoder High School SRM 5 250 pt - Level one - TV Size problem solution

Topcoder High School SRM 5 250 pt - Level one - TV Size problem statement: http://community.topcoder.com/stat?c=problem_statement&pm=6486&rd=10057

Topcoder High School SRM 5 250 pt - Level one - TV Size problem editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm5

Topcoder High School SRM 5 250 pt - Level one - TV Size problem solution: 

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

class TVSize {
    public: vector <int> calcSize(int d, int h, int w) {
        double a, r;
        vector <int> v;
        a=(double)sqrt(h*h + w*w);
        r=(double)d/a;
        v.push_back(h*r), v.push_back(w*r);
        return v;
    }
};

note: i suggest you to use (double), otherwise you won't get expected result. without (double) my prog returned 36, 64 instead of 25, 45 for sample 0. peek other AC codes also.

Topcoder High School SRM 4 250 pt - Level one - Winning Trick problem solution

Topcoder High School SRM 4 250 pt - Level one - Winning Trick problem statement: http://community.topcoder.com/stat?c=problem_statement&pm=6421&rd=10023

Topcoder High School SRM 4 250 pt - Level one - Winning Trick problem editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm4

Topcoder High School SRM 4 250 pt - Level one - Winning Trick problem solution: 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class WinningTrick {
    public: double minimumSpeed(vector <int> s, int y) {
        double a=0.0;
        sort(s.begin(), s.end());
        if(s[s.size()-1]>y) a=s[s.size()-1]-y, a/=2.00;
        return a;
    }
};

Friday, May 23, 2014

Topcoder high school SRM 3 250 pt - Level one - Best Seller solution

Topcoder high school SRM 3 250 pt - Level one - Best Seller statement: http://community.topcoder.com/stat?c=problem_statement&pm=6440&rd=10021

Topcoder high school SRM 3 250 pt - Level one - Best Seller editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm3

Topcoder high school SRM 3 250 pt - Level one - Best Seller solution:

#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>

using namespace std;

class BestSeller {
    public: string findBestSeller(vector <string> items) {
        int a[55]={0}, r=0, max=INT_MIN;
        sort(items.begin(), items.end());
        for(int i=0; i<items.size(); i++) {
            for(int j=0; j<=i; j++) {
                if(items[j]==items[i]) a[j]++;
                if(a[j]>max) max=a[j], r=j;
            }
        }   
        return items[r];
    }
};

note: i used sort() of <algorithm> library. after sorting find maximum(max) number sold per item. loop through items, if current item sold more than max then save it to max, and save its index to r.

Topcoder high school SRM 2 250 pt - Level one - Fountain Of Life solution

Topcoder high school SRM 2 250 pt - Level one - Fountain Of Life statement: http://community.topcoder.com/stat?c=problem_statement&pm=6252&rd=10024

Topcoder high school SRM 2 250 pt - Level one - Fountain Of Life editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm2

Topcoder high school SRM 2 250 pt - Level one - Fountain Of Life solution:

#include <iostream>

using namespace std;

class FountainOfLife {
    public: double elixirOfDeath(int elixir, int poison, int pool) {
        if((poison < elixir) || ((poison==elixir) && pool>0)) return -1.0;
        else {
            double a=elixir, b=poison, c=pool;
            return c/(b-a);
        }
    }
};

note: if elixir is greater than poison, fountain will never become deadly, so we return -1.0 . Otherwise, if elixir is smaller than poison then time taken to fountain to become deadly is closing the gap(difference) between elixir and poison, and that gap is pool, so we just return pool/(poison - elixir);

Sunday, May 18, 2014

Topcoder high school SRM 1 250 pt - Level one - Speed Radar solution

Topcoder high school SRM 1 250 pt - Level one - Speed Radar solution problem statement: http://community.topcoder.com/stat?c=problem_statement&pm=6474&rd=10022

Topcoder high school SRM 1 250 pt - Level one - Speed Radar editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm1

Topcoder high school SRM 1 250 pt - Level one - Speed Radar solution: http://ideone.com/mnEM17

#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>

using namespace std;

int cnt=0, sz;
double ans, tot=0, a;

class SpeedRadar {
    public:
    double averageSpeed(int minLimit, int maxLimit, vector <int> readings) {
        sz=readings.size();
        for(int i=0; i<sz; i++) {
            if(readings[i]>maxLimit || readings[i]<minLimit) cnt++;
            else tot += readings[i];
        }
        if(sz<cnt*10) return 0.0;
        else {
            a=sz-cnt;
            ans=tot/a;
            return ans;
        }
    }
};

note: it is really awful to be able to solve Topcoder problems. it is my first ever Topcoder problem that I was able to solve. i suggest you to learn C++ "class" and "method": http://bit.ly/1o2A0zu
Launch Topcoder Arena. If you encounter problem when trying to launch Topcoder Arena then i suggest you to install Java. after installing Java activate Java web start by clicking on "javaws.exe" application in folder which might look like: C:\Program Files\Java\jre7\bin
Topcoder Arena itself automatically inputs the variables, one does not need to scanf() or cin>> it. and you just need to "return" it not printf() or cout<<.
You can also use KawigiEdit or some other editors: http://community.topcoder.com/tc?module=Static&d1=applet&d2=plugins


Topcoder high school SRM 1 250 pt - Level one - Speed Radar solution version 2:

#include <iostream>
#include <vector>

using namespace std;

class SpeedRadar {
    public: double averageSpeed(int minLimit, int maxLimit, vector <int> readings) {
        double cnt=0, tot=0;
        for(int i=0; i<readings.size(); i++) {
            if(readings[i]<=maxLimit &&  readings[i]>=minLimit) tot+=readings[i];
            else cnt++;
        }
        if(cnt*10>readings.size()) return 0.0;
        else return tot/(readings.size()-cnt);
    }
};

Thursday, May 15, 2014

Codechef May Challange 2014 OJUMPS - Chef-jumping solution

Codechef May Challange 2014 OJUMPS - Chef-jumping: http://www.codechef.com/MAY14/problems/OJUMPShttp://www.codechef.com/problems/OJUMPS/

Codechef May Challange 2014 OJUMPS - Chef-jumping editorial: http://discuss.codechef.com/questions/42547/ojumps-editorial

Codechef May Challange 2014 OJUMPS - Chef-jumping solution: http://ideone.com/h9JOWO

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    long long int a, b=1;
    scanf("%I64d", &a);
    a%=6;
    while(a>0) {
        a-=b;
        b++;
        if(b==4) b=1;
    }
    if(a==0) printf("yes");
    else printf("no");
    return 0;
}