Problem Solving

[Topcoder] SRM181 ~ 196 연습

끄적끄적 2008. 9. 5. 16:49

SRM181 DIV2
: 쉬운 문제..

class KiloMan
{
public:
 int hitsTaken(vector <int> p, string j)
 {  
  int ret = 0;
  int n = p.size();
  REP(i,n)
  {
   if( j[i] == 'J' && p[i] > 2) ret++;
   else if( j[i] == 'S' && p[i] < 3) ret++;
  }
  return ret;
 }
};


SRM182 DIV2
: 평이한 문제

class IBEvaluator
{
public:
 vector <int> getSummary(vector <int> p, vector <int> a)
 {  
  int n = p.size();
  vector<int> ret(7,0);
  REP(i,n)  ret[abs(p[i]-a[i])]++;
  REP(i,7)  ret[i] = 1.0 * ret[i] / n * 100;
  return ret;
 }
};

SRM183 DIV2
: (goal-next+1)% (maxAdd+1) 로 생각할 수 있는 능력배양 필요.
머 readability는 내 소스가 더 낫지않나 싶지만서도;;

class CountGame
{
public:
 int howMany(int maxAdd, int goal, int next)
 {  
  while( goal - maxAdd - 1 >= next)
  {
   goal = goal - maxAdd - 1;
   }

  int ret = goal - next + 1;
  if( ret > maxAdd) return -1;
  return ret;
 }
};

SRM184 DIV2
출력하는 부분을 s1 << h<< ":" << m/10 << m % 10 << ":" << s/10 << s%10 으로 할 수 있는 연습필요.

class RaceApproximator
{
public:
 string timeToBeat(int d1, int t1, int d2, int t2, int r)
 {  
  string ret;
  int t = t1 * exp( log((double)t2/t1) * log((double)d1/r) / log((double)d1 / d2));
  int h = t / 60 / 60;
  int m = (t % 3600) / 60;
  int s = t % 60;
  stringstream s1;
 
  s1 << h << ":";
  if(m < 10) s1 << "0";
  s1 << m << ":";
  if(s < 10) s1 << "0";
  s1 << s;
  ret = s1.str();

  return ret;
 }
};

SRM185 DIV2
ret = (65 * p1 -1) / 100 +1 -e1 으로 짤 수 있는 사고의 유연성. ( 65*p1+99)/100 -e1

class PassingGrade
{
public:
 int pointsNeeded(vector <int> e, vector <int> p, int f)
 {  
  int n = e.size();
  int e1=0, p1 = f;
  REP(i,n)
  {
   e1 += e[i];
   p1 += p[i];
  }

  double ret1 = 65.0 * p1 / 100 - e1;
  if(ret1 < 0) return 0;
  int ret = ret1;
  if( ret1 - ret > 0) ret++;  
  if(ret > f) return -1;
  return ret;
 }
};

SRM186 DIV2
: 평이한 문제.

class GolfScore
{
public:
 int tally(vector <int> p, vector <string> s)
 {  
  int ret =0;
 
  REP(i,18)
  {
   if(s[i] == "triple bogey") ret += p[i]+3;
   else if(s[i] == "double bogey") ret += p[i]+2;
   else if(s[i] == "bogey") ret += p[i]+1;
   else if(s[i] == "par") ret += p[i];
   else if(s[i] == "birdie") ret += p[i]-1;
   else if(s[i] == "eagle") ret += p[i]-2;
   else if(s[i] == "albatross") ret += p[i]-3;
   else if(s[i] == "hole in one") ret += 1;
  }
  return ret;
 }
};

SRM187 DIV2
: 나는 vector<string>을 미리 50개로 만들었으나, insert와 erase로 했다면, 별도로 pos계산없이 p.size()로 리턴하면 됐었다.
마지막에 pos+1을 할 것이 아니라, pos를 넣을때 max(pos,j+1)로 했었어야 빈 백터일때를 처리할 수 있었음.

class OfficeParking
{
public:
 int spacesUsed(vector <string> e)
 {  
  int ret =0;
  vector<string> p(50);

  stringstream in;
  string n, a;
  int pos = 0;
  if(e.size() == 0) return 0;

  REP(i, e.size())
  {
   in << e[i];
   in >> n >> a;

   if(a == "arrives")
   {
    REP(j,50)
    {
     if(p[j].empty())
     {
      p[j] = n;
      pos = max( pos, j);
      break;
     }
    }
   }
   else
   {
    REP(j,50)
    {
     if(!p[j].empty() && p[j] == n)
     {
      p[j].clear();
      break;
     }
    }
   }
   in.clear();
  }
  return pos+1;
 }
};

SRM188 DIV2
: 우선 빨리 푸느라 일반화를 안시키고, 그냥 if로 나눴다.
 (pos/3)*3 + i 로 일반화 할 수 있는 능력 필요.

class MagicSquare
{
public:
 int missing(vector <int> s)
 {  
  int ret =0, cnt = 0, sum = 0, pos = 0;
  REP(i,9)
  {
   if(s[i] == -1)
   {
    if(i<3) i = 2;
    if(i<6) i = 5;
    cnt = 0;
    sum = 0;
    continue;    
   }
   else
   {
    sum += s[i];
    cnt++;
   }
   if(cnt == 3) break;
  }
  REP(i,9)   if(s[i] == -1) pos = i;

  if(pos<3)
  {
   for(int i=0; i<3; i++)
    sum -= s[i];
  }
  else if(pos < 6)
  {
   for(int i=3; i<6; i++)
    sum -= s[i];
  }
  else
  {
   for(int i=6; i<9; i++)
    sum -= s[i];

  }
  return sum-1;
 }
};

SRM189 DIV2
: stringstream(num) >> d_val;
  stringstream(cutoff) >> cut
으로 쓰는 방법을 익히자.

class CutoffRounder
{
public:
 int round(string num, string cutoff)
 {  
  double d_val, remain, cut;
  int i_val;
  stringstream in;
  in << num;
  in >> d_val;
 
  i_val = d_val;
  remain = d_val - i_val;

  in.clear();
  in << cutoff;
  in >> cut;

  if( remain >= cut ) i_val++;

  return i_val;
 }
};

SRM191 DIV2
: 심플한 문제

class BettingMoney
{
public:
 int moneyMade(vector <int> a, vector <int> c, int f)
 {
  int ret=0;
  REP(i, a.size())
  {
   if(i != f)
    ret += a[i] * 100;
   else
    ret -= a[i] * c[i];
  } 
  return ret;
 }
};

SRM192 DIV2
: 곱하기와 나누기 썩인 부분을 괄호를 안쳐주는 바람에 한가지 예제가 결과가 틀리게 나왔다..
연산의 순서가 바뀌면 값이 틀려질 수 있다는 걸 명심하자;;;
가끔 반복 돌리면서 next_permutation 을 쓰는 사람들이 있던데.. 언제 한번 이 함수에 대해 정리를 해야겠다.

class BoxLoader
{
public:
 int mostItems(int x, int y, int z, int ix, int iy, int iz)
 {
  int ret=0;
  vector<int> sum(6,0);
  sum[0] = (x/ix) * (y/iy) * (z/iz);
  sum[1] = (x/ix) * (y/iz) * (z/iy);
  sum[2] = (x/iy) * (y/ix) * (z/iz);
  sum[3] = (x/iy) * (y/iz) * (z/ix);
  sum[4] = (x/iz) * (y/ix) * (z/iy);
  sum[5] = (x/iz) * (y/iy) * (z/ix);

  REP(i,6)
   ret = max(ret, sum[i]);  
  return ret;
 }
};

SRM194 DIV2
: 간만에 쉬운 문제.. 4분이내 돌파;

class Soccer
{
public:
 int maxPoints(vector <int> wins, vector <int> ties)
 {
  int ret=0;
  int n = wins.size();
  REP(i,n)
     ret = max( ret, 3*wins[i]+ties[i]);
   
  return ret;
 }
};

SRM195 DIV2
: 심플한 문제..

class Rounder
{
public:
 int round(int n, int b)
 {
  int r = n - (n % b);
  if( n%b >= b/2.0)
   r = r+b;

  return r;
 }
};

SRM196 DIV2
: 평이한 문제.

class Education
{
public:
 int minimize(string d, vector <int> t)
 {
  int ret=0;
  int n = t.size();
  int sum = 0;
  REP(i, n)  sum += t[i];

  int lab = 0;
  if( d == "A") lab = 90;
  if( d == "B") lab = 80;
  if( d == "C") lab = 70;
  if( d == "D") lab = 60;

  ret = lab * (n+1) - sum;

  if(ret > 100) return -1;
  if(ret < 0) return 0;
  return ret;
 
 }
};

반응형