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

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

typedef long long ll;

ll d;
ll c[29];
ll a[369][29];
ll t[369];
ll b[29];
ll b2[29];
ll now[369];
ll ans;
bool vv[369];
ll ans3[369];
double ch1 = 1.1;
double ch2 = 0.05 / 26;

bool g() {
  ll re = 0;
  for (ll i = 1; i <= 26; i++)
    b[i] = 0;
  for (ll i = 1; i <= d; i++) {
    b[now[i]] = i;
    for (ll j = 1; j <= 26; j++) {
      re -= (i - b[j]) * c[j];
    }
    re += a[i][now[i]];
    if (re * (ch1 - ch2 * i) < ans3[i])
      return false;
  }
  if (ans < re) {
    ans = re;
    return true;
  } else
    return false;
}

bool f() {
  ll x = rand() % 26 + 1;
  ll y = rand() % d + 1;
  if (vv[y]) {
    return g();
  }
  ll ans2 = 0;
  ll t2 = now[y];
  now[y] = x;
  vv[y] = true;
  bool v = false;
  if (rand() % 100 == 0)
    v = f();
  else
    v = g();
  if (!v)
    now[y] = t2;
  vv[y] = false;
  return v;
}

int main() {
  //    freopen("input.txt", "r", stdin);
  srand(time(NULL));
  scanf("%lld", &d);
  for (ll i = 1; i <= 26; i++) {
    scanf("%lld", &c[i]);
  }
  for (ll i = 1; i <= d; i++) {
    for (ll j = 1; j <= 26; j++) {
      scanf("%lld", &a[i][j]);
    }
  }
  ans = 0;
  for (ll i = 1; i <= d; i++) {
    ll mx = -4e18, mi = 0;
    for (ll ii = 1; ii <= 26; ii++) {
      t[i] = ii;
      ll ans2 = 0;
      ll tt = b[t[i]];
      b[t[i]] = i;
      for (ll j = 1; j <= 26; j++) {
        ans2 -= (i - b[j]) * c[j];
      }
      ans2 += a[i][t[i]];
      if (mx < ans2)
        mx = ans2, mi = ii;
      b[t[i]] = tt;
    }
    t[i] = mi;
    b[t[i]] = i;
    for (ll j = 1; j <= 26; j++) {
      ans -= (i - b[j]) * c[j];
    }
    ans += a[i][t[i]];
    now[i] = mi;
    ans3[i] = ans;
  }
  ll k = 0;
  k = 150000;
  while (k--) {
    f();
  }
  for (ll i = 1; i <= d; i++)
    printf("%lld\\n", now[i]);
  //    printf("\\n\\n%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

typedef long long ll;

ll d;
ll c[29];
ll a[369][29];
ll t[369];
ll b[29];
ll b2[29];
ll now[369];
ll ans;
bool vv[369];

bool g() {
  ll re = 0;
  for (ll i = 1; i <= 26; i++)
    b[i] = 0;
  for (ll i = 1; i <= d; i++) {
    b[now[i]] = i;
    for (ll j = 1; j <= 26; j++) {
      re -= (i - b[j]) * c[j];
    }
    re += a[i][now[i]];
  }
  if (ans < re) {
    ans = re;
    return true;
  } else
    return false;
}

bool f() {
  ll x = rand() % 26 + 1;
  ll y = rand() % d + 1;
  if (vv[y]) {
    return g();
  }
  ll ans2 = 0;
  ll t2 = now[y];
  now[y] = x;
  vv[y] = true;
  bool v = false;
  if (rand() % 100 == 0)
    v = f();
  else
    v = g();
  if (!v)
    now[y] = t2;
  vv[y] = false;
  return v;
}

int main() {
  srand(time(NULL));
  scanf("%lld", &d);
  for (ll i = 1; i <= 26; i++) {
    scanf("%lld", &c[i]);
  }
  for (ll i = 1; i <= d; i++) {
    for (ll j = 1; j <= 26; j++) {
      scanf("%lld", &a[i][j]);
    }
  }
  ans = 0;
  for (ll i = 1; i <= d; i++) {
    ll mx = -4e18, mi = 0;
    for (ll ii = 1; ii <= 26; ii++) {
      t[i] = ii;
      ll ans2 = 0;
      ll tt = b[t[i]];
      b[t[i]] = i;
      for (ll j = 1; j <= 26; j++) {
        ans2 -= (i - b[j]) * c[j];
      }
      ans2 += a[i][t[i]];
      if (mx < ans2)
        mx = ans2, mi = ii;
      b[t[i]] = tt;
    }
    t[i] = mi;
    b[t[i]] = i;
    for (ll j = 1; j <= 26; j++) {
      ans -= (i - b[j]) * c[j];
    }
    ans += a[i][t[i]];
    now[i] = mi;
  }
  ll k = 0;
  k = 1000000;
  while (k--) {
    f();
  }
  for (ll i = 1; i <= d; i++)
    printf("%lld\\n", now[i]);
  printf("\\n\\n%lld\\n", ans);
  return 0;
}