Showing posts with label Topcoder. Show all posts
Showing posts with label Topcoder. Show all posts

Saturday, December 13, 2014

TCHS SRM 56 250 pt - Strange Comparator solution

TCHS SRM 56 250 pt - Strange Comparator: http://community.topcoder.com/stat?c=problem_statement&pm=10010&rd=13525

TCHS SRM 56 250 pt - Strange Comparator editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm56

TCHS SRM 56 250 pt - Strange Comparator solution:


#include <iostream>
#include <vector>

class StrangeComparator {
 public: std::vector <std::string> compareString(std::vector <std::string> a, std::vector <std::string> b) {
  int cnt=0, f=0;
  std::vector <std::string> v;
  for(int i=0; i<a.size(); i++, cnt=0, f=0) {
   if(a[i].length()!=b[i].length()) f=1;
   if(f==0) for(int j=0; j<a[i].length(); j++) if(a[i][j]!=b[i][j]) cnt++;   
   if(cnt>1 || f==1) v.push_back("No");
   else v.push_back("Yes");
  }
  return v;
 }
};



TCHS SRM 55 250 pt - Very Interesting Movie solution

TCHS SRM 55 250 pt - Very Interesting Movie: http://community.topcoder.com/stat?c=problem_statement&pm=9955&rd=13524

TCHS SRM 55 250 pt - Very Interesting Movie editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm55

TCHS SRM 55 250 pt - Very Interesting Movie solution:


#include <iostream>
#include <vector>

class VeryInterestingMovie {
 public: int maximumPupils(std::vector <std::string> s) {
  int a=0, f=0;
  for(int i=0; i<s.size(); i++, f=0) for(int j=0; j<s[i].length(); j++) {
   if(s[i][j]=='Y' && f==0) a++, f=1;
   else if((s[i][j]=='Y' && f==1) || s[i][j]=='N') f=0;
  }
  return a;
 }
};

TCHS SRM 54 250 pt - Problem Setter solution

TCHS SRM 54 250 pt - Problem Setter: http://community.topcoder.com/stat?c=problem_statement&pm=9946&rd=13523

TCHS SRM 54 250 pt - Problem Setter editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm54

TCHS SRM 54 250 pt - Problem Setter solution: 


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

class ProblemSetter {
 public: std::vector <int> chooseProblems(std::vector <int> d) {
  double dif, d1, d2, mi=1005;
  std::vector <int> v;
  std::sort(d.begin(), d.end());
  for(int i=0; i<d.size()-2; i++) {
   for(int j=d.size()-1; j>i+1; j--) {
    for(int k=i+1; k<d.size()-1; k++) {
     d1=d[k]-d[i], d2=d[j]-d[k];
     dif=std::abs(d1-d2);
     if(dif<mi) {
      mi=dif, v.clear();
      v.push_back(d[i]), v.push_back(d[k]), v.push_back(d[j]);
     }
    }
   }
  }
  return v;   
 }
};

for sorting vectors in C++: http://www.cplusplus.com/reference/algorithm/sort/

for clearing or removing elements from vector in C++: http://www.cplusplus.com/reference/vector/vector/clear/

for absolute value of a number in C++: http://www.cplusplus.com/reference/cmath/abs/

Friday, December 12, 2014

TCHS SRM 53 250 pt - DNAConstruction solution

TCHS SRM 53 250 pt - DNAConstruction: http://community.topcoder.com/stat?c=problem_statement&pm=9818&rd=13486

TCHS SRM 53 250 pt - DNAConstruction editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm53

TCHS SRM 53 250 pt - DNAConstruction solution: 

 
#include <iostream>
#include <cstring>
#include <algorithm>

class DNAConstruction {
 public: int maxLength(std::string n) {
  int a=0, t=0, c=0, g=0, tot=0;
  for(int i=0; i<n.length(); i++) {
   if(n[i]=='A') a++;
   else if(n[i]=='T') t++;
   else if(n[i]=='C') c++;
   else if(n[i]=='G') g++;
  }
  tot=std::min(a, t) + std::min(c, g);
  return tot;
 }
};

for proper use of "min" and "max" in c++:

1 - http://www.cplusplus.com/reference/algorithm/max/

2 - http://stackoverflow.com/questions/1632145/use-of-min-and-max-functions-in-c


and for iterating through string in c++: http://stackoverflow.com/questions/1315041/how-can-i-iterate-through-a-string-and-also-know-the-index-current-position

TCHS SRM 52 250 pt - Tournament Judging solution

TCHS SRM 52 250 pt - Tournament Judging: http://community.topcoder.com/stat?c=problem_statement&pm=9822&rd=13485

TCHS SRM 52 250 pt - Tournament Judging editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm52

TCHS SRM 52 250 pt - Tournament Judging solution: 

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

class TournamentJudging {
 public: int getPoints(std::vector <int> rs, std::vector <int> cf) {
  int tot=0;
  double db;
  for(int i=0; i<rs.size(); i++) {
   db=(double)rs[i]/cf[i]; tot+=round(db);
  }
  return tot;
 }
};


for "round"ing to the nearest number in C++: http://www.cplusplus.com/reference/cmath/round/

TCHS SRM 51 250 pt - Missing Digits solution

TCHS SRM 51 250 pt - Missing Digits: http://community.topcoder.com/stat?c=problem_statement&pm=7920&rd=13484

TCHS SRM 51 250 pt - Missing Digits editorial:  http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm51

TCHS SRM 51 250 pt - Missing Digits solution:


#include <iostream>
#include <sstream>
#include <vector>

class MissingDigits {
 public: std::string isAllowed(std::vector <int> na, int r) {
  std::ostringstream ss;
  std::string s1, s2;
  ss<<r, s1=ss.str();
  for(int i=0; i<na.size(); i++) {
   std::ostringstream ss2;
   ss2<<i<<na[i], s2=ss2.str();
   if(s1.find(s2)!=std::string::npos) return "NO";
  }  
 return "YES";
 }
};
 
 
for "ostringstream" or to convert int to string in C++, see:  
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
 
 
and for "find" substring in a string in C++, see: 
http://www.cplusplus.com/reference/string/string/find/

TCHS SRM 50 250 pt - Level one - Funny Birds solution

TCHS SRM 50 250 pt - Level one - Funny Birds: http://community.topcoder.com/stat?c=problem_statement&pm=9769&rd=13483

TCHS SRM 50 250 pt - Level one - Funny Birds editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm50

TCHS SRM 50 250 pt - Level one - Funny Birds solution: 


#include <iostream>

class FunnyBirds{
    public: int gameTime(int n) {
        int a=1, cnt=0;
        while(n) {
            while((a*(a+1)/2)<=n) a++;
            a--, n-=a*(a+1)/2, cnt+=a, a=1;
        }
        return cnt;
    }
};

Friday, August 22, 2014

TC SRM 630 div2 250 pt - Double Letter solution

TC SRM 630 div2 250 pt - Double Letter solution: 


#include <iostream>
#include <string>

class DoubleLetter {
    public: std::string ableToSolve(std::string s) {
        int flag=0, cnt=0;
        while(!flag) {
            for(int i=1; i<s.length(); i++) if(s[i-1]==s[i]) s.erase(i-1, 2);
            cnt++;
            if(!s.length()) flag=1;
            if(s.length()%2 || cnt==100) break;
        }
        if(flag) return "Possible";
        else return "Impossible";
    }
};

Friday, August 15, 2014

TCHS SRM 49 - Level one - 250 pt Deposit Profit solution

TCHS SRM 49 - Level one - 250 pt Deposit Profit: http://community.topcoder.com/stat?c=problem_statement&pm=8313&rd=10809

TCHS SRM 49 - Level one - 250 pt Deposit Profit editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm49

TCHS SRM 49 - Level one - 250 pt Deposit Profit solution:


#include <iostream>

class DepositProfit{
    public: int depositTerm(int a, int b, int p) {
        int cnt, st=a*100, m=0;
        a*=100, p*=100;
        while(cnt<p) a+=a*b/1200, cnt=a-st, m++;
        return m;
    }
};

Wednesday, July 30, 2014

TCHS SRM 48 Level one - 250 pt - Sending Cards solution

TCHS SRM 48 Level one - 250 pt - Sending Cards: http://community.topcoder.com/stat?c=problem_statement&pm=8456&rd=10807

TCHS SRM 48 Level one - 250 pt - Sending Cards editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm48

TCHS SRM 48 Level one - 250 pt - Sending Cards solution: 


#include <iostream>
#include <vector>

class SendingCards{
    public: int howMany(std::vector <std::string> f) {
        int cnt=0;
        for(int i=0; i<f.size(); i++) for(int j=0; j<f[i].length(); j++) if(f[i][j]=='Y' && f[j][i]=='N') cnt++;
        return cnt;
    }
};

TCHS SRM 47 Level one - 250 pt - Cards shuffle solution

TCHS SRM 47 Level one - 250 pt - Cards shuffle statement: http://community.topcoder.com/stat?c=problem_statement&pm=8295&rd=10803

TCHS SRM 47 Level one - 250 pt - Cards shuffle editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm47

TCHS SRM 47 Level one - 250 pt - Cards shuffle solution: 


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

class CardsShuffle {
    public: string shuffle(string c, int f, int l, int t) {
        std::rotate(c.begin(), c.begin() + ((f-1)*t % l), c.begin() + l);
        return c;       
    }
};

my solution:
#include <iostream>
#include <cstring>

class CardsShuffle{
    public: std::string shuffle(std::string c, int f, int l, int t) {
        std::string s;
        while(t--) s=c.substr(f-1, l-(f-1)), s+=c.substr(0, f-1), s+=c.substr(l), c=s;
        return s;
    }
};

Tuesday, July 29, 2014

TCHS SRM 46 Level one - 250 pt - Pawns solution

TCHS SRM 46 Level one - 250 pt - Pawns statement: http://community.topcoder.com/stat?c=problem_statement&pm=8108&rd=10801

TCHS SRM 46 Level one - 250 pt - Pawns editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm46

TCHS SRM 46 Level one - 250 pt - Pawns solution: 


#include <iostream>
#include <vector>

class Pawns{
    public: int pawnsAttack(std::vector <std::string> p) {
        int a[8][8]={0}, b[8][8]={0}, cnt=0;
        for(int i=0; i<p.size(); i++) a[8-(p[i][1]-'0')][p[i][0]-'a']++;
        for(int i=1; i<8; i++) for(int j=0; j<8; j++) if(a[i][j]) {
            if(j==0) {
                if(!a[i-1][j+1] && !b[i-1][j+1]) cnt++, b[i-1][j+1]++;
            }
            else if(j==7) {
                if(!a[i-1][j-1] && !b[i-1][j-1]) cnt++, b[i-1][j-1]++;
            }
            else {
                if(!a[i-1][j-1] && !b[i-1][j-1]) cnt++, b[i-1][j-1]++;
                if(!a[i-1][j+1] && !b[i-1][j+1]) cnt++, b[i-1][j+1]++;
            }
        }
        return cnt;
    }
};

TCHS SRM 45 Level one - 250 pt - Solving Equation solution

TCHS SRM 45 Level one - 250 pt - Solving Equation statement: http://community.topcoder.com/stat?c=problem_statement&pm=8141&rd=10799

TCHS SRM 45 Level one - 250 pt - Solving Equation editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm45

TCHS SRM 45 Level one - 250 pt - Solving Equation solution: 


#include <iostream>
#include <climits>

class SolvingEquation{
    public: int solve(int a, int b, int c, int w) {
        int mi=INT_MAX, flag=0;
        for(int i=0; i<=100; ++i) for(int j=0; j<=100; ++j) for(int k=0; k<=100; ++k) {
            if(a*i + b*j + c*k == w && i+j+k<mi) mi=i+j+k, flag=1; 
        }
        if(flag==1) return mi;
        else return -1;
    }
};

Monday, July 28, 2014

TCHS SRM 44 Level one - 250 pt - Young Brother solution

TCHS SRM 44 Level one - 250 pt - Young Brother statement: http://community.topcoder.com/stat?c=problem_statement&pm=8252&rd=10795

TCHS SRM 44 Level one - 250 pt - Young Brother editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm44

TCHS SRM 44 Level one - 250 pt - Young Brother solution: 

#include <iostream>
#include <vector>

class YoungBrother{
 public: std::vector <std::string> restoreWords(std::vector <std::string> l, int n, int k) {
  std::string s="";
  std::vector <std::string> ans;
  for(int i=0; i<l.size(); i++) s+=l[i];
  for(int i=0, j=0; i<n; i++, j+=k) ans.push_back(s.substr(j, k));
  return ans;
 }
};

Sunday, July 20, 2014

TopCoder High School SRM 43 Level one - 250 pt - Transform Array solution

TopCoder High School SRM 43 Level one - 250 pt - Transform Array statement: http://community.topcoder.com/stat?c=problem_statement&pm=6692&rd=10792

TopCoder High School SRM 43 Level one - 250 pt - Transform Array editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm43

TopCoder High School SRM 43 Level one - 250 pt - Transform Array solution: 


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

class TransformArray {
    public: int doTransform(vector <int> a) {
        int cnt=0;
        sort(a.begin(), a.end());
        for(int i=1; i<a.size(); i++) cnt+=a[i-1], a[i]-=a[i-1], a[i-1]=0;
        return cnt;
    }
};

TopCoder High School SRM 42 Level one - 250 pt - Fancy GUI solution

TopCoder High School SRM 42 Level one - 250 pt - Fancy GUI statement: http://community.topcoder.com/stat?c=problem_statement&pm=7888&rd=10790

TopCoder High School SRM 42 Level one - 250 pt - Fancy GUI editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm42

TopCoder High School SRM 42 Level one - 250 pt - Fancy GUI solution: 

#include <iostream>
#include <vector>

class FancyGUI{
    public: int totalDarkArea(int n, std::vector <int> x1, std::vector <int> y1, std::vector <int> x2, std::vector <int> y2) {
        int cnt=0, a[105][105]={0};
        for(int i=0; i<x1.size(); i++) for(int j=x1[i]; j<=x2[i]; j++) for(int k=y1[i]; k<=y2[i]; k++) a[j][k]++;
        for(int i=1; i<=100; i++) for(int j=1; j<=100; j++) if(a[i][j]<=n) cnt++;
        return 100*100-cnt;
    }
};

TopCoder High School SRM 41 Level one - 250 pt - Majority Element solution

TopCoder High School SRM 41 Level one - 250 pt - Majority Element statement: http://community.topcoder.com/stat?c=problem_statement&pm=8137&rd=10788

TopCoder High School SRM 41 Level one - 250 pt - Majority Element editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm41

TopCoder High School SRM 41 Level one - 250 pt - Majority Element solution: 

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

class MajorityElement {
    public: int findMajorityElement(vector <int> a) {
        int cnt=1, idx=0;
        sort(a.begin(), a.end());
        for(int i=1; i<a.size(); i++) {
            if(a[i]==a[i-1]) {
                cnt++; 
                if(cnt>a.size()/2) { idx=i; break; }
            }
            else cnt=1, idx=0;
        }
        if(idx==0 && a.size()>1) return -1;
        else return a[idx];
    }
};


TopCoder High School SRM 40 Level one - 250 pt - Secretary solution and C++ vector reverse

TopCoder High School SRM 40 Level one - 250 pt - Secretary statement: http://community.topcoder.com/stat?c=problem_statement&pm=8055&rd=10784

TopCoder High School SRM 40 Level one - 250 pt - Secretary editorial: http://community.topcoder.com/tc?module=Static&d1=hs&d2=match_editorials&d3=hs_srm40

TopCoder High School SRM 40 Level one - 250 pt - Secretary solution: 


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

class Secretary {
    public: string wrongOrdering(vector <string> f) {
        string s;
        for(int i=0; i<f.size(); i++) reverse(f[i].begin(), f[i].end()); 
        sort(f.begin(), f.end());
        reverse(f[0].begin(), f[0].end());
        s=f[0];
        return s;
    }
};

C++ vector reverse: http://www.cplusplus.com/reference/algorithm/reverse/