Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
// パチンコ
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int N = 26;
int D;

vector<int> c(N + 1);
vector<vector<int>> s;
vector<int> t;

ll now_score = 0;

ll greedy() {
  vector<int> last(N + 1, 0);
  ll greedy_score = 0;
  for (int d = 1; d <= D; d++) {
    ll max_score = -1e9;
    vector<int> max_last;
    for (int n = 1; n <= N; n++) {
      ll tmp_score = 0;
      vector<int> tmp_last(N + 1);
      tmp_last = last;

      tmp_score += s[d][n];
      tmp_last[n] = d;

      int penalty = 0;
      for (int i = 1; i <= N; i++) {
        penalty += c[i] * (d - tmp_last[i]);
      }

      tmp_score -= penalty;

      if (max_score < tmp_score) {
        max_score = tmp_score;
        max_last = tmp_last;
        t[d] = n;
      }
    }

    last = max_last;
    greedy_score += max_score;
  }

  return greedy_score;
}

ll scoring() {
  vector<int> last(N + 1, 0);
  ll score = 0;
  for (int d = 1; d <= D; d++) {

    score += s[d][t[d]];
    last[t[d]] = d;

    int penalty = 0;
    for (int i = 1; i <= N; i++) {
      penalty += c[i] * (d - last[i]);
    }

    score -= penalty;
  }
  return score;
}

void exchange(int d, int x) {
  swap(t[d], t[d + x]);

  ll swap_score = scoring();

  if (now_score >= swap_score) {
    swap(t[d], t[d + x]);
  } else {
    now_score = swap_score;
  }
}

void pachinko() {
  srand((unsigned)time(NULL)); /* Use Mersenne Twister */
  for (int i = 1; i <= D; i++) {
    int pre = t[i];
    t[i] = rand() % 26 + 1;

    ll pachi_score = scoring();
    if (now_score >= pachi_score) {
      t[i] = pre;
    } else {
      now_score = pachi_score;
    }
  }
}

int main() {
  clock_t start = clock();

  cin >> D;

  s.resize(D + 1);
  for (int i = 0; i <= D; i++) {
    s[i].resize(N + 1);
  }
  t.resize(D + 1);

  for (int i = 1; i <= N; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 1; i <= D; i++) {
    cin >> t[i];
  }

  now_score = greedy();

  clock_t now_time = clock();
  while ((now_time - start) / CLOCKS_PER_SEC < 1.2) {

    // 乱数パチンコ
    pachinko();
    // 解交換
    for (int x = 1; x < D; x++) {
      for (int d = 1; d <= D - x; d++) {
        exchange(d, x);
      }
    }
    now_time = clock();
  }

  for (int i = 1; i <= D; i++) {
    cout << t[i] << endl;
  }

  return 0;
}

Fixed submission:
// パチンコ
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int N = 26;
int D;

vector<int> c(N + 1);
vector<vector<int>> s;
vector<int> t;

ll now_score = 0;

ll greedy() {
  vector<int> last(N + 1, 0);
  ll greedy_score = 0;
  for (int d = 1; d <= D; d++) {
    ll max_score = -1e9;
    vector<int> max_last;
    for (int n = 1; n <= N; n++) {
      ll tmp_score = 0;
      vector<int> tmp_last(N + 1);
      tmp_last = last;

      tmp_score += s[d][n];
      tmp_last[n] = d;

      int penalty = 0;
      for (int i = 1; i <= N; i++) {
        penalty += c[i] * (d - tmp_last[i]);
      }

      tmp_score -= penalty;

      if (max_score < tmp_score) {
        max_score = tmp_score;
        max_last = tmp_last;
        t[d] = n;
      }
    }

    last = max_last;
    greedy_score += max_score;
  }

  return greedy_score;
}

ll scoring() {
  vector<int> last(N + 1, 0);
  ll score = 0;
  for (int d = 1; d <= D; d++) {

    score += s[d][t[d]];
    last[t[d]] = d;

    int penalty = 0;
    for (int i = 1; i <= N; i++) {
      penalty += c[i] * (d - last[i]);
    }

    score -= penalty;
  }
  return score;
}

void exchange(int d, int x) {
  swap(t[d], t[d + x]);

  ll swap_score = scoring();

  if (now_score >= swap_score) {
    swap(t[d], t[d + x]);
  } else {
    now_score = swap_score;
  }
}

void pachinko() {
  srand((unsigned)time(NULL)); /* Use Mersenne Twister */
  for (int i = 1; i <= D; i++) {
    int pre = t[i];
    t[i] = rand() % 26 + 1;

    ll pachi_score = scoring();
    if (now_score >= pachi_score) {
      t[i] = pre;
    } else {
      now_score = pachi_score;
    }
  }
}

int main() {
  clock_t start = clock();

  cin >> D;

  s.resize(D + 1);
  for (int i = 0; i <= D; i++) {
    s[i].resize(N + 1);
  }
  t.resize(D + 1);

  for (int i = 1; i <= N; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 1; i <= D; i++) {
    cin >> t[i];
  }

  now_score = greedy();

  clock_t now_time = clock();
  while ((now_time - start) / CLOCKS_PER_SEC < 1) {

    // 乱数パチンコ
    pachinko();
    // 解交換
    for (int x = 1; x < D; x++) {
      for (int d = 1; d <= D - x; d++) {
        exchange(d, x);
      }
    }
    now_time = clock();
  }

  for (int i = 1; i <= D; i++) {
    cout << t[i] << endl;
  }

  return 0;
}