7.1 マージソート

解説どおりの実装です。「Runtime Error」が頻発して悩みましたが、配列LRをスタックメモリに確保していたのが原因でした。再帰だからあっという間にスタックを食い尽くすんですね。

#include <iostream>
#include <vector>
#include <cstdint>

using namespace std;

static void inputArray(uint32_t A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i]; 
  }
}

static void printArray(uint32_t A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << (i != 0 ? " " : "") << A[i];
  }
  cout << endl; 
}

static int compared = 0;

// ヒープメモリに確保すること.
static vector<uint32_t> L(250000);
static vector<uint32_t> R(250000);

static inline void merge(uint32_t A[], uint32_t left, uint32_t mid, uint32_t right) {
  uint32_t n1 = mid - left;
  uint32_t n2 = right - mid;

  L.assign(&A[left], &A[left + n1]);
  R.assign(&A[mid], &A[mid + n2]);
  L[n1] = R[n2] = UINT32_MAX;

  for (uint32_t i = 0, j = 0, k = left; k < right; k++) {
    compared++;
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  } 
}

static inline void mergeSort(uint32_t A[], uint32_t left, uint32_t right) {
  if (left + 1 < right) {
    uint32_t mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  } 
}

int main() {
  int n;
  cin >> n;

  vector<uint32_t> A(n);
  inputArray(&A[0], n);
  
  mergeSort(&A[0], 0, n);
  
  printArray(&A[0], n);
  
  cout << compared << endl;
  
  return 0;
}

6.3 コッホ曲線

なかなか再帰脳になれず。。なお座標計算は本書を参照しました。プログラミングコンテストって、ここまで数学の知識を問われるんですかね?

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

using namespace std;

struct P {
  double x;
  double y;
  P(double x, double y) : x(x) , y(y) {
  };
};

static inline void printP(const P& p) {
  cout << fixed << p.x << " " << p.y << endl; 
}

static inline double toRadian(double degree) {
  return degree / 180 * M_PI;
}

static inline void koch(int d, const P& p1, const P& p2) {
  if (d == 0) {
    return;
  }
  
  P s(p1.x + (p2.x - p1.x) / 3, p1.y + (p2.y - p1.y) / 3);
  P t(p1.x + 2 * ((p2.x - p1.x) / 3), p1.y + 2 * ((p2.y - p1.y) / 3));
  P u(
    (t.x - s.x) * cos(toRadian(60.0)) - (t.y - s.y) * sin(toRadian(60.0)) + s.x,
    (t.x - s.x) * sin(toRadian(60.0)) + (t.y - s.y) * cos(toRadian(60.0)) + s.y);
 
  koch(d - 1, p1, s);
  printP(s);
  koch(d - 1, s, u);
  printP(u);
  koch(d - 1, u, t);
  printP(t);
  koch(d - 1, t, p2);
}

int main() {
  int n;
  cin >> n;

  P p1(0.0, 0.0), p2(100.0, 0);

  printP(p1);
  koch(n, p1, p2);
  printP(p2);
  
  return 0;
}

6.2 全検索

再帰と計算結果をうまく組み合わる、この発想がなかなか出てきません。

#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static inline bool solve(int A[], int i, int n, int m) {
  if (m == 0) {
    return true;
  }
  
  if (i < n && m > 0) {
    return solve(A, i + 1, n, m) || solve(A, i + 1, n, m - A[i]);
  } else {
    return false;
  }
}

int main() {
  int n;
  cin >> n;

  vector<int> A(n);
  inputArray(&A[0], n);

  int q;
  cin >> q;

  int m;
  bool ans;
  for (int i = 0; i < q; i++) {
    cin >> m;
    ans = solve(&A[0], 0, n, m);
    cout << (ans ? "yes" : "no") << endl;
  }
  
  return 0;
}

solve の途中で m がマイナス値になったら打ち切る判定を一応入れてみました。

5.6 探索の応用(バイナリサーチ版)

ということで、バイナリサーチを使う版です。積載量の絞り込みをバイナリサーチで行っていくのですね。数が大きくなると劇的な効果が出ますね。検索回数は log2(1000000000) = 約 30 回でした。

#include <iostream>
#include <vector>

//#include <cstdio>

using namespace std;

static int inputArray(int A[], int num) {
  int max = 0;
  for (int i = 0; i < num; i++) {
    cin >> A[i];
    if (A[i] > max) {
      max = A[i];
    } 
  }
  return max;
}

static inline int f(int A[], int num, uint64_t P, int k) {
    int i = 0, v = 0;
    for (uint64_t total = 0; (v < k) && (i < num); i++) {
      total += A[i];
      if (total > P) {
        total = 0;
        i--;
        v++;
      }
    }
    return i;
}

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> w(n);
  inputArray(&w[0], n);

  uint64_t left = 0;
  uint64_t right = 100000 * 10000;
  uint64_t mid;
  int v = 0;
  while (1 < right - left) {
    mid = (left + right) / 2;
    v = f(&w[0], n, mid, k);
    if (v < n) {
      left = mid;
    } else {
      right = mid;
    }
  }

  cout << right << endl;

  return 0;
}

5.6 探索の応用(時間切れ版)

解説そのままやってみて、時間切れのパターン。ここまでは様式美です。ここからバイナリサーチをどう使うのだろう?

現時点でひとつだけ工夫したところは、入力時の最大値を覚えておいて、そこから P を増やしていく部分です。最大値が入らない積載量の探索は意味がないですからね。

#include <iostream>
#include <vector>

using namespace std;

static int inputArray(int A[], int num) {
  int max = 0;
  for (int i = 0; i < num; i++) {
    cin >> A[i];
    if (A[i] > max) {
      max = A[i];
    } 
  }
  return max;
}

static inline int f(int A[], int num, int P, int k) {
    int i = 0, v = 0;
    for (int total = 0; (v < k) && (i < num); i++) {
      total += A[i];
      if (total > P) {
        total = 0;
        i--;
        v++;
      }
    }
    return i;
}

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> w(n);
  int P = inputArray(&w[0], n);
  
  for (int v = 0; v < n; P++) {
    v = f(&w[0], n, P, k);
  }

  cout << (P - 1) << endl;

  return 0;
}

5.4 ハッシュ(解説版)

解説(というか答え)の写経版です。ハッシュ関数は、自力では思いつかないので、研究している人たちにおまかせするしかない感じです。

#include <iostream>
#include <string>
#include <vector>

using namespace std;

static inline int charToInt(char c) {
  switch(c) {
  case 'A':
    return 1;
  case 'C':
    return 2;
  case 'G':
    return 3;
  case 'T':
    return 4;
  default:
    return 0;
  }  
}

static inline uint64_t getKey(const string& str) {
  uint64_t total = 0, p = 1;
  for (int i = 0; i < str.length(); i++) {
    total += p * (charToInt(str[i]));
    p *= 5;
  }  
  return total;
}

static const int M = 1046527;
static const int NUL = -1;
static const int L = 14;

static inline int h1(int key) {
  return key % M;
}

static inline int h2(int key) {
  return 1 + (key % (M - 1));
}

static vector<string> H(M);

static inline bool find(const string& str) {
  uint64_t key, h;
  key = getKey(str);
  for (int i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (H[h] == str) {
      return true;
    } else if (H[h].size() == 0) {
      return false;
    }
  }
  return false;
}

static inline bool insert(const string& str) {
  uint64_t key, h;
  key = getKey(str);
  for (int i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (H[h] == str) {
      return true;
    } else if (H[h].size() == 0) {
      H[h] = str;
      return false;
    }
  }
  return false;
}

int main() {
  int n, h;

  cin >> n;

  string str, com;
  for (int i = 0; i < n; i++) {
    cin >> com >> str;
    if (com[0] == 'i') {
      insert(str);
    } else {
      cout << (find(str) ? "yes" : "no") << endl;
    }
  }
  return 0;
}

5.4 ハッシュ(自力版)

「ハッシュ」という言葉と、問題の図を参考に、自分なりに考えたのがこちら。「文字の種類は4つ」ということで、文字列を4進数とみなして、全部の組み合わせ分の bool 配列を持ち、insert 時に組み合わせを int 値にして、配列の index に読み替えてそこを true にする、という形式にしています。このあと解説を読みます。

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

using namespace std;

static inline int charToInt(char c) {
  switch(c) {
  case 'A':
    return 0;
  case 'C':
    return 1;
  case 'G':
    return 2;
  case 'T':
    return 3;
  default:
    return -1;
  }  
}

static inline int strToInt(const string& str) {
  int total = charToInt(str[0]);
  int cur;
  for (int i = 1; i < str.length(); i++) {
    cur = charToInt(str[i]);
    total += pow(4, i) * (1 + cur);
  }  
  return total;
}

int main() {
  int n;
  cin >> n;

  vector<bool> dictionary(static_cast<size_t>(pow(4, 12)) + 4, false);
  string command, str;

  for (int i = 0; i < n; i++) {
    cin >> command >> str;
    if (command[0] == 'i') {
      dictionary[strToInt(str)] = true;
    } else {
        cout << (dictionary[strToInt(str)] ? "yes" : "no") << endl;
    }
  }
  return 0;
}