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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<double, ll>;
using um = unordered_map<ll, ll>;
#define fl cout << flush;
#define endl '\\n'
template <typename T> inline void prt(T v) { cout << v << '\\n'; }
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ld pi = 3.14159265358979323846;
const ld eps = 1e-10;

#define debug(v) cout << #v << ": ", prt(v);
template <typename A, typename B> inline void prt(pair<A, B> p) {
  cout << "(" << p.first << ", " << p.second << ")\\n";
}
template <typename A, typename B, typename C>
inline void prt(tuple<A, B, C> p) {
  cout << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")\\n";
}
inline void prt(bool p) {
  if (p)
    cout << "True" << '\\n';
  else
    cout << "False" << '\\n';
}
template <typename T> inline void prt(vector<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(deque<T> v) {
  cout << '{';
  for (ll i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i < v.size() - 1)
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename A, typename B> inline void prt(unordered_map<A, B> v) {
  cout << '{';
  ll c = 0;
  for (auto &p : v) {
    cout << p.first << ":" << p.second;
    c++;
    if (c != v.size())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(set<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}
template <typename T> inline void prt(multiset<T> v) {
  cout << '{';
  for (auto i = v.begin(); i != v.end(); i++) {
    cout << *i;
    if (i != --v.end())
      cout << ", ";
  }
  cout << '}' << '\\n';
}

signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll D;
  cin >> D;
  vector<double> c(27, 0);
  for (ll i = 0; i < 26; ++i)
    cin >> c[i + 1];
  vector<vector<double>> S(D + 1, vector<double>(27, 0));
  for (ll i = 0; i < D; ++i)
    for (ll j = 0; j < 26; ++j)
      cin >> S[i + 1][j + 1];
  ll Maxscore = -INF;
  vl ans;
  for (double k = -1.0; k <= 100; k += 0.03) {
    vector<ll> last(27, 0);
    ll score = 0;
    vector<ll> t(D + 1, 0);
    for (ll i = 1; i <= D; ++i) {
      ll mark;
      double Max = -2000000000;
      for (ll j = 1; j <= 26; ++j) {
        if (chmax(Max, S[i][j] + c[j] * (min(k, (double)D - i))))
          mark = j;
      }
      t[i] = mark;
      last[t[i]] = i;
      score += S[i][t[i]];
      for (ll j = 1; j <= 26; ++j)
        score -= c[j] * (i - last[j]);
    }
    if (chmax(Maxscore, score))
      ans = t;
  }
  for (double k = -1.0; k <= 200; k += 0.03) {
    vector<ll> last(27, 0);
    ll score = 0;
    vector<ll> t(D + 1, 0);
    for (ll i = 1; i <= D; ++i) {
      ll mark;
      double Max = -2000000000;
      for (ll j = 1; j <= 26; ++j) {
        if (chmax(Max, S[i][j] + c[j] * k))
          mark = j;
      }
      t[i] = mark;
      last[t[i]] = i;
      score += S[i][t[i]];
      for (ll j = 1; j <= 26; ++j)
        score -= c[j] * (i - last[j]);
    }
    if (chmax(Maxscore, score))
      ans = t;
  }
  vector<double> Q(25, 0);
  for (ll i = 0; i < 25; ++i)
    Q[i] = 0.5 + i * 0.1;
  vector<vector<double>> PP(27, vector<double>(25, 0));
  for (ll i = 1; i <= 26; ++i)
    for (ll j = 0; j < 25; ++j) {
      PP[i][j] = pow(c[i], Q[j]);
    }
  function<double(ll, ll)> powm = [&](ll j, ll d) { return PP[j][d]; };
  for (ll d = 0; d < 25; d++) {
    for (double k = -1.0; k <= 80; k += 0.5) {
      vector<ll> last(27, 0);
      ll score = 0;
      vector<ll> t(D + 1, 0);
      for (ll i = 1; i <= D; ++i) {
        ll mark;
        double Max = -2000000000;
        for (ll j = 1; j <= 26; ++j) {
          if (chmax(Max, S[i][j] + powm(j, d) * (i - last[j]) * k))
            mark = j;
        }
        t[i] = mark;
        last[t[i]] = i;
        score += S[i][t[i]];
        for (ll j = 1; j <= 26; ++j)
          score -= c[j] * (i - last[j]);
      }
      if (chmax(Maxscore, score))
        ans = t;
    }
  }
  for (ll i = 1; i <= D; ++i)
    prt(ans[i]);

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<double, ll>;
using um = unordered_map<ll, ll>;
#define fl cout << flush;
#define endl '\\n'
template <typename T> inline void prt(T v) { cout << v << '\\n'; }
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ld pi = 3.14159265358979323846;
const ld eps = 1e-10;

signed main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll D;
  cin >> D;
  vector<double> c(27, 0);
  for (ll i = 0; i < 26; ++i)
    cin >> c[i + 1];
  vector<vector<double>> S(D + 1, vector<double>(27, 0));
  for (ll i = 0; i < D; ++i)
    for (ll j = 0; j < 26; ++j)
      cin >> S[i + 1][j + 1];
  ll Maxscore = -INF;
  vl ans;
  for (double k = -1.0; k <= 100; k += 0.03) {
    vector<ll> last(27, 0);
    ll score = 0;
    vector<ll> t(D + 1, 0);
    for (ll i = 1; i <= D; ++i) {
      ll mark;
      double Max = -2000000000;
      for (ll j = 1; j <= 26; ++j) {
        if (chmax(Max, S[i][j] + c[j] * (min(k, (double)D - i))))
          mark = j;
      }
      t[i] = mark;
      last[t[i]] = i;
      score += S[i][t[i]];
      for (ll j = 1; j <= 26; ++j)
        score -= c[j] * (i - last[j]);
    }
    if (chmax(Maxscore, score))
      ans = t;
  }
  for (double k = -1.0; k <= 200; k += 0.03) {
    vector<ll> last(27, 0);
    ll score = 0;
    vector<ll> t(D + 1, 0);
    for (ll i = 1; i <= D; ++i) {
      ll mark;
      double Max = -2000000000;
      for (ll j = 1; j <= 26; ++j) {
        if (chmax(Max, S[i][j] + c[j] * k))
          mark = j;
      }
      t[i] = mark;
      last[t[i]] = i;
      score += S[i][t[i]];
      for (ll j = 1; j <= 26; ++j)
        score -= c[j] * (i - last[j]);
    }
    if (chmax(Maxscore, score))
      ans = t;
  }
  for (double d = 0.5; d <= 3.0; d += 0.2) {
    for (double k = -1.0; k <= 100; k += 0.5) {
      vector<ll> last(27, 0);
      ll score = 0;
      vector<ll> t(D + 1, 0);
      for (ll i = 1; i <= D; ++i) {
        ll mark;
        double Max = -2000000000;
        for (ll j = 1; j <= 26; ++j) {
          if (chmax(Max, S[i][j] + pow(c[j], d) * (i - last[j]) * k))
            mark = j;
        }
        t[i] = mark;
        last[t[i]] = i;
        score += S[i][t[i]];
        for (ll j = 1; j <= 26; ++j)
          score -= c[j] * (i - last[j]);
      }
      if (chmax(Maxscore, score))
        ans = t;
    }
  }
  for (ll i = 1; i <= D; ++i)
    prt(ans[i]);

  return 0;
}