Problem Solving

[Topcoder] SRM250 ~ 264 연습

끄적끄적 2008. 9. 16. 16:40

SRM250 DIV2
: 소스를 간단히 하는거 보다 우선 빨리 풀기 위해 다 if문으로 나열해서 풀었다..
간단히 하는 방법중에 괜찬아 보이는 방법은 map을 써서 각 color별로 인덱스 값을 넣어둔 후에
(long long)(10*map[code[0]] + map[code[1]]) * pow(10, map[code[2]])

class ColorCode
{
public:

 long long getOhms(vector <string> c)
 {
  long long third = 0;
  long long ret = 0;
  if(c[2] == "black")   third = 1;
  else if(c[2] == "brown") third = 10;
  else if(c[2] == "red")  third = 100;
  else if(c[2] == "orange") third = 1000;
  else if(c[2] == "yellow") third = 10000;
  else if(c[2] == "green") third = 100000;
  else if(c[2] == "blue")  third = 1000000;
  else if(c[2] == "violet") third = 10000000;
  else if(c[2] == "grey")  third = 100000000;
  else if(c[2] == "white") third = 1000000000;


  if(c[0] == "black")   ret = third * 0;
  else if(c[0] == "brown") ret = third * 1;
  else if(c[0] == "red")  ret = third * 2;
  else if(c[0] == "orange") ret = third * 3;
  else if(c[0] == "yellow") ret = third * 4;
  else if(c[0] == "green") ret = third * 5;
  else if(c[0] == "blue")  ret = third * 6;
  else if(c[0] == "violet") ret = third * 7;
  else if(c[0] == "grey")  ret = third * 8;
  else if(c[0] == "white") ret = third * 9;

  if(c[1] == "black")   ret = ret * 10 + third * 0;
  else if(c[1] == "brown") ret = ret * 10 + third * 1;
  else if(c[1] == "red")  ret = ret * 10 + third * 2;
  else if(c[1] == "orange") ret = ret * 10 + third * 3;
  else if(c[1] == "yellow") ret = ret * 10 + third * 4;
  else if(c[1] == "green") ret = ret * 10 + third * 5;
  else if(c[1] == "blue")  ret = ret * 10 + third * 6;
  else if(c[1] == "violet") ret = ret * 10 + third * 7;
  else if(c[1] == "grey")  ret = ret * 10 + third * 8;
  else if(c[1] == "white") ret = ret * 10 + third * 9;

  return ret;
 }
};

SRM251 DIV2
: 정수를 나눈 값을 크기를 비교할 때 조심해야 할 부분. Visual Studio랑 topcoder랑 결과값이 틀린 경우 생김.
될 수 있으면 분모를 없애서, 나눠지는 부분이 없도록 비교할 것

class Elections
{
public:

 int visit(vector <string> l)
 {
  int ret = 0;
  int c1 = 100, t1 = 0;
  REP(i, l.size())
  {
   string s = l[i];  
   int cnt = 0;
   REP(j, s.size()) if(s[j] == '1') cnt++;

   if( c1 * s.size() > cnt * t1)
   {
    c1 = cnt;
    t1 = s.size();
    ret = i;
   }
  }

  return ret;
 }
};

SRM252 DIV2

class WarCry
{
public:

 int alertTime(string o)
 {
  VI f;
  REP(i, o.size())
   if(o[i] == 'x') f.pb(i);

  int ret = 0;
  REP(i, o.size())
  {
   if(o[i] == 'x') continue;
   int m = INT_MAX;
   REP(j, f.size())
    m = min( m, abs(f[j]-i));

   ret = max( ret, m);
  }

  return ret;
 }
};

SRM253 DIV2

class ObjectPacking
{
public:

 int smallBox(int ow, int ol, vector <int> bw, vector <int> bl)
 {
  int n = bw.size();

  int ret = 1000001;
  REP(i, n)
  {
   if( (ow <= bw[i] && ol <= bl[i]) || (ow <= bl[i] && ol <= bw[i]))
    ret = min(ret, bw[i]*bl[i]);
  }

  if(ret == 1000001 ) return -1;

  return ret;
 }
};

SRM254 DIV2

class BalancedGame
{
public:

 int result(vector <string> c, int p, int q)
 {
  REP(i, c.size())
  {
   string s = c[i];
   int n = s.size();
   int win = 0, lose = 0;
   REP(j, n)
   {
    if(s[j] == 'W') win++;
    else if(s[j] == 'L') lose++;
   }

   if( ( ((n-1) * p + 99)/100 > win) ||  (((n-1) * q + 99)/100 > lose))
    return i;
  }
 
  return -1;
 }
};

SRM255 DIV2
: 3분만에 풀다..기록이군;

class SequenceOfNumbers
{
public:

 int toInt(string s)
 {
  stringstream s1(s);
  int t;
  s1 >> t;
  return t;
 }

 string toStr(int num)
 {
  stringstream s;
  s << num;
  return s.str();
 }

 vector <string> rearrange(vector <string> seq)
 {
  int n = seq.size();
  VI r;
  REP(i, n)
   r.pb(toInt(seq[i]));

  sort(ALL(r));
  vector<string> ret;
  REP(i, n)
   ret.pb(toStr(r[i]));

  return ret;
 }
};

SRM256 DIV2
: 뭔가 규칙이 있을법도 하지만, 경우의 수가 얼마 없으니 그냥 반복돌리면서 구하는게 심플..

class GridGenerator
{
public:

 int generate(vector <int> r, vector <int> c)
 {
  int n = r.size();

  vector< vector<int> > s;
  s.pb(r);
  for(int i=1; i<n; i++)
  {
   VI sub;
   sub.pb(c[i]);  
   for(int j=1; j<n; j++)
    sub.pb( s[i-1][j-1] + s[i-1][j] + sub[j-1]);

   s.pb(sub);
  }

  return s[n-1][n-1];
 }
};

SRM257 DIV2

class SubstitutionCode
{
public:

 int getValue(string key, string c)
 {
  int ret = 0;
  REP(i, c.size())
  {
   char sub = c[i];
   for(int j=0; j<10; j++)
    if(key[j] == sub)
      ret = 10*ret + (j+1) % 10;      
  }
  return ret;
 }
};

SRM258 DIV2
: count함수를 제대로 활용할 수 있었던 문제.
count를 못쓸 경우 map으로 100까지 개수를 담아놓은 다음 max와 일치하는 것만 push_back할 수도 있고, unique함수로 새로운 vector에 넣는 방법도 있음.

class ClassScores
{
public:

 vector <int> findMode(vector <int> s)
 {
  int n = 0;
  REP(i, s.size())
   n = max(n, count(ALL(s), s[i]));

  vector<int> ret;
  REP(i, s.size())
   if( count(ALL(s), s[i]) == n && count(ALL(ret), s[i]) == 0 )
    ret.pb(s[i]);

  sort(ALL(ret));
  return ret;
 }
};

SRM259 DIV2

class CompetitionStatistics
{
public:

 int consecutiveGrowth(vector <int> r)
 {
  int ret = 0, cnt = 0;
  REP(i, r.size())
   if( r[i] > 0)
   {
    cnt++;
    ret = max(ret, cnt);
   }
   else cnt = 0;

  return ret;
 }
};

SRM260 DIV2

class IsingModel
{
public:

 int energy(vector <string> s)
 {
  int ret = 0;
  REP(i, s.size())
  {
   REP(j, s[0].size()-1)
   {
    if( s[i][j] == s[i][j+1] ) ret -= 1;
    else ret += 1;
   }
  }

  REP(i, s[0].size())
  {
   REP(j, s.size()-1)
   {
    if( s[j][i] == s[j+1][i] ) ret -= 1;
    else ret += 1;
   }
  }

  return ret;
 }
};

SRM261 DIV2

class SpreadsheetColumn
{
public:

 string getLabel(int c)
 {
  c = c--;
  int a = c / 26;
  int b = c % 26;

  string ret;
  if( a > 0) ret += a -1 + 'A';
  ret += b + 'A';

  return ret;
 }
};

SRM262 DIV2
if( cnt < 10) s << '0'; 와 같이 stringstream에 '0'을 넣어줬어도 됨.
ret = cnt /10 + '0';
ret = cnt%10 + '0'; 처럼 구현하는 것도 괜찮은 듯


class DivToZero
{
public:

 string lastTwo(int num, int f)
 {
  int n = (num / 100)*100;
  int cnt = 0;
  while( (n+cnt) % f != 0)
   cnt++;

  stringstream s;
  s << cnt;
  string ret = s.str();
  if(cnt < 10) ret = '0' + ret;
  return ret;
 }
};

SRM263 DIV2
: map안에 vector가 들어간 구조로 짜다보니 좀 복잡해졌다.
차라리 vector< set<int> > 구조로 짰더라면 비교적 간단하게 나올 수 있는 문제.
set<int>에서는 그냥 insert로 구현가능

class Party
{
public:

 double averageNames(int n, vector <int> A, vector <int> B)
 {
  map<int, vector<int> > mat;
 
  REP(i, A.size())
  {
   int a = A[i];
   int b = B[i];
   if( count(ALL(mat[a]), b) == 0 ) mat[a].pb(b);

   REP(j, mat[b].size())
    if(count( ALL(mat[a]), mat[b][j]) == 0 && a != mat[b][j]) mat[a].pb(mat[b][j]);

   if( count(ALL(mat[b]), a) == 0 ) mat[b].pb(a);

   REP(j, mat[a].size())
    if(count( ALL(mat[b]), mat[a][j]) == 0  && b != mat[a][j]) mat[b].pb(mat[a][j]);

 }

  int sum = 0;
  REP(i, 100)  
   sum += mat[i].size();

  return (double)sum / n;
 }
};

vector< set<int> > 로 다시 짜본 소스
class Party
{
public:

 double averageNames(int n, vector <int> A, vector <int> B)
 {
  vector< set<int> > mat(100);
  REP(i, 100)
   mat[i].insert(i);
 
  REP(i, A.size())
  {
   int a = A[i];
   int b = B[i];
   mat[a].insert(b);
   mat[b].insert(a);
   mat[a].insert( mat[b].begin(), mat[b].end());
   mat[b].insert( mat[a].begin(), mat[a].end());
  }

  int sum = 0;
  REP(i, 100)  
   sum += mat[i].size();

  return (double)(sum-100) / n;
 }
};

SRM264 DIV2

class GradingSystem
{
public:

 int fairness(vector <int> s, vector <int> g)
 {
  int ret = 0;
  int m = g[0];
  for(int i = 1; i<g.size(); i++)
   if( m > g[i]) ret += m - g[i];
   else m = g[i];

  m = g[g.size()-1];
  for(int i= g.size()-1; i>=0; i--)
   if(m < g[i] ) ret += g[i] -m;
   else m = g[i];

  return ret;
 }
};

반응형