Saturday, December 13, 2014

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/

No comments:

Post a Comment