Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
// #include <random>
#include <map>
// #include <unordered_map>
#include <cstdlib>
#include <functional>
#include <time.h>
// #include <chrono>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
// #include <type_traits>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
// #define int ll
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ndx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ndy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
double pi = 3.141592653589793;
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

void solve1();
int score1();
int change(int, int, int);
vector<int> changeT(vector<int>);

int ti;
int D;
int sumC;
int k = 26;
int score;
vector<int> c(k), t, tz;
vector<vector<int>> s;

int main() {
  srand((unsigned)time(NULL));
  cin >> D;
  s.resize(D);

  rep(i, k) {
    cin >> c[i];
    sumC += c[i];
  }
  rep(i, D) {
    s[i].resize(k);
    rep(j, k) { cin >> s[i][j]; }
  }
  t.resize(D);

  int maxS = -1 * INF;
  vector<int> maxTi(D);
  ti = clock();
  while (1) {

    double time = (double)(clock() - ti) / CLOCKS_PER_SEC;
    if (time > 0.2) {
      break;
    }

    rep(i, D) { t[i] = rand() % 26; }
    score = score1();
    if (score > maxS) {
      maxTi = t;
    }
  }
  t = maxTi;
  score = score1();

  solve1();

  // cout << score << endl;
  // score = score1();
  // cout << score << endl;

  rep(i, D) { cout << t[i] + 1 << endl; }
}

void solve1() {

  int count = 0;

  while (1) {
    count++;
    double time = (double)(clock() - ti) / CLOCKS_PER_SEC;
    if (time > 1.7) {
      break;
    }

    int no = rand() % 15;
    int sz = score;
    tz = t;
    // vector<P> temp;
    rep(i, no) {
      int d = rand() % D;
      int q = rand() % k;
      sz = change(sz, d, q);
      // temp.push_back(mp(d, q));
    }

    if (sz > score) {
      score = sz;
      t = tz;
    }
  }

  // cout << count << endl;
}

vector<int> changeT(vector<int> t) {
  int no = rand() % 15;
  rep(i, no) {
    int d = rand() % D;
    int q = rand() % k;
    t[d] = q;
  }
  return t;
}

int score1() {
  // 最後にコンテストが行われた日
  vector<int> last(k, -1);
  int ans = 0;

  rep(i, D) {
    ans += s[i][t[i]];
    last[t[i]] = i;
    rep(j, k) { ans -= c[j] * (i - last[j]); }
    // cout << ans << endl;
  }
  return ans;
}

int change(int score, int d, int q) {
  score += s[d][q] - s[d][tz[d]];
  int x = d + 1;
  while (x != D && tz[x] != q) {
    x++;
  }
  int y = d - 1;
  while (y >= 0 && tz[y] != q) {
    y--;
  }
  score += c[q] * (d - y) * (x - d);

  x = d + 1;
  while (x != D && tz[x] != tz[d]) {
    x++;
  }
  y = d - 1;
  while (y >= 0 && tz[y] != tz[d]) {
    y--;
  }
  score -= c[tz[d]] * (d - y) * (x - d);

  tz[d] = q;
  return score;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
// #include <random>
#include <map>
// #include <unordered_map>
#include <cstdlib>
#include <functional>
#include <time.h>
// #include <chrono>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
// #include <type_traits>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
// #define int ll
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ndx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ndy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
double pi = 3.141592653589793;
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

void solve1();
int score1();
int change(int, int, int);
vector<int> changeT(vector<int>);

int ti;
int D;
int sumC;
int k = 26;
int score;
vector<int> c(k), t, tz;
vector<vector<int>> s;

int main() {
  srand((unsigned)time(NULL));
  cin >> D;
  s.resize(D);

  rep(i, k) {
    cin >> c[i];
    sumC += c[i];
  }
  rep(i, D) {
    s[i].resize(k);
    rep(j, k) { cin >> s[i][j]; }
  }
  t.resize(D);

  int maxS = -1 * INF;
  vector<int> maxTi(D);
  while (1) {

    ti = clock();

    double time = (double)(clock() - ti) / CLOCKS_PER_SEC;
    if (time > 0.2) {
      break;
    }

    rep(i, D) { t[i] = rand() % 26; }
    score = score1();
    if (score > maxS) {
      maxTi = t;
    }
  }
  t = maxTi;
  score = score1();

  solve1();

  // cout << score << endl;
  // score = score1();
  // cout << score << endl;

  rep(i, D) { cout << t[i] + 1 << endl; }
}

void solve1() {

  ti = clock();
  int count = 0;

  while (1) {
    count++;
    double time = (double)(clock() - ti) / CLOCKS_PER_SEC;
    if (time > 1.7) {
      break;
    }

    int no = rand() % 15;
    int sz = score;
    tz = t;
    // vector<P> temp;
    rep(i, no) {
      int d = rand() % D;
      int q = rand() % k;
      sz = change(sz, d, q);
      // temp.push_back(mp(d, q));
    }

    if (sz > score) {
      score = sz;
      t = tz;
    }
  }

  // cout << count << endl;
}

vector<int> changeT(vector<int> t) {
  int no = rand() % 15;
  rep(i, no) {
    int d = rand() % D;
    int q = rand() % k;
    t[d] = q;
  }
  return t;
}

int score1() {
  // 最後にコンテストが行われた日
  vector<int> last(k, -1);
  int ans = 0;

  rep(i, D) {
    ans += s[i][t[i]];
    last[t[i]] = i;
    rep(j, k) { ans -= c[j] * (i - last[j]); }
    // cout << ans << endl;
  }
  return ans;
}

int change(int score, int d, int q) {
  score += s[d][q] - s[d][tz[d]];
  int x = d + 1;
  while (x != D && tz[x] != q) {
    x++;
  }
  int y = d - 1;
  while (y >= 0 && tz[y] != q) {
    y--;
  }
  score += c[q] * (d - y) * (x - d);

  x = d + 1;
  while (x != D && tz[x] != tz[d]) {
    x++;
  }
  y = d - 1;
  while (y >= 0 && tz[y] != tz[d]) {
    y--;
  }
  score -= c[tz[d]] * (d - y) * (x - d);

  tz[d] = q;
  return score;
}