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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <stdio.h>
// #include <stack>
// #include <queue>
// #include <cstdio>
#include <cmath>
#include <fstream>
#include <iterator>
#include <map>
// #include <list>
#include <iomanip>
// #include <stdlib.h>
// #include <cctype>

using namespace std;

#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define prec std::cout << std::fixed << std::setprecision(15);
#define endl "\\n"
#define MOD 1000000007
#define Int int64_t
#define PI 3.14159265358979
// #define rep(i, n) for (int i = 0; i < int(n); i++)
#define ssort(z) sort(z.begin(), z.end())
#define rsort(z) sort(z.rbegin(), z.rend())
#define eerase(z) z.erase(unique(z.begin(), z.end()), z.end())
#define ccnt(z, w) count(z.begin(), z.end(), w)
#define rep(i, a, n) for (Int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (Int(i) = (a); (i) <= (n); (i)++)
const int MAX_N = 1000000;
const Int MAX_N_Int = 1000000000000;

template <typename T>
void printV(const std::vector<T> &v, const char *const separator = " ") {
  if (!v.empty()) {
    std::copy(v.begin(), --v.end(),
              std::ostream_iterator<T>(std::cout, separator));
    std::cout << v.back() << "\\n";
  }
}

int D;
string str, s, u;
vector<int> c(26);
vector<int> t(366), t0(366);
vector<vector<int>> ss(366, vector<int>(26));
// vector<vector<int>> last(365, vector<int>(26));
vector<int> last(26), last0(26);
int satisfaction = 0;
int init_score;

int init() {
  vector<int> c_copy(26);
  c_copy = c;
  rsort(c_copy);
  int now = 1;
  srand(time(NULL));
  for (int i = 0; i < D; ++i) {
    t[i] = now;
    // now=min(now+1,26);
    now = rand() % 26 + 1;
    if (now > 26)
      now = 1;
  }

  return 0;
}

int score() {
  satisfaction = 0;
  last = last0;
  for (int d = 0; d < D; ++d) {
    int i = t[d] - 1;
    satisfaction += ss[d][i];

    last[i] = d + 1;
    for (int cc = 0; cc < 26; ++cc) {
      satisfaction -= c[cc] * ((d + 1) - last[cc]);
    }
    // cout<<satisfaction<<endl;
  }

  return satisfaction;
}
int hajime = clock();
int loop() {
  int old_score = init_score;
  int new_score = 0;
  srand(time(NULL));
  int cnt = 0;
  while (cnt <= 20000000) {
    if ((clock() - hajime) > 1.95 * CLOCKS_PER_SEC)
      break;
    int d = rand() % 365 + 1;

    int now = t[d];
    // t[d]++;
    // t[d]=min(t[d]+1,26);
    t[d] = min(rand() % 26 + 1, 26);
    new_score = score();
    if (new_score <= old_score) {
      t[d] = now;

    } else {
      old_score = new_score;
      // cout<<cnt<<" "<<d<<" "<<new_score<<endl;
    }

    cnt++;
  }

  return 0;
}

int main() {
  IOS;
  prec;

  Int x, k, d;
  cin >> x >> k >> d;
  if (x == 0) {
    if (d % 2 == 0)
      cout << 0;
    else
      cout << d;
    return 0;
  }
  if (x < 0)
    x *= -1;
  Int pos = x, tmp = k;

  Int cnt = x / d + 1;
  k -= cnt;
  if (k < 0) {
    cout << x - d * tmp;
    return 0;
  }
  pos = x - d * cnt;
  // cout<<k;
  if (pos <= 0) {
    if (abs(pos) < abs(pos + 1 * d)) {

    } else {
      pos += 1 * d;
      k++;
    }
    if (k % 2 == 0) {
      cout << abs(pos) << endl;
      return 0;
    } else {
      cout << min(abs(pos + d), abs(pos - d)) << endl;
      return 0;
    }
  }

  // cout<<pos;

  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// #include <stdio.h>
// #include <stack>
// #include <queue>
// #include <cstdio>
#include <cmath>
#include <fstream>
#include <iterator>
#include <map>
// #include <list>
#include <iomanip>
// #include <stdlib.h>
// #include <cctype>

using namespace std;

#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define prec std::cout << std::fixed << std::setprecision(15);
#define endl "\\n"
#define MOD 1000000007
#define Int int64_t
#define PI 3.14159265358979
// #define rep(i, n) for (int i = 0; i < int(n); i++)
#define ssort(z) sort(z.begin(), z.end())
#define rsort(z) sort(z.rbegin(), z.rend())
#define eerase(z) z.erase(unique(z.begin(), z.end()), z.end())
#define ccnt(z, w) count(z.begin(), z.end(), w)
#define rep(i, a, n) for (Int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (Int(i) = (a); (i) <= (n); (i)++)
const int MAX_N = 1000000;
const Int MAX_N_Int = 1000000000000;

template <typename T>
void printV(const std::vector<T> &v, const char *const separator = " ") {
  if (!v.empty()) {
    std::copy(v.begin(), --v.end(),
              std::ostream_iterator<T>(std::cout, separator));
    std::cout << v.back() << "\\n";
  }
}

int D;
string str, s, u;
vector<int> c(26);
vector<int> t(366), t0(366);
vector<vector<int>> ss(366, vector<int>(26));
// vector<vector<int>> last(365, vector<int>(26));
vector<int> last(26), last0(26);
int satisfaction = 0;
int init_score;

int init() {
  vector<int> c_copy(26);
  c_copy = c;
  rsort(c_copy);
  int now = 1;
  srand(time(NULL));
  for (int i = 0; i < D; ++i) {
    t[i] = now;
    // now=min(now+1,26);
    now = rand() % 26 + 1;
    if (now > 26)
      now = 1;
  }

  return 0;
}

int score() {
  satisfaction = 0;
  last = last0;
  for (int d = 0; d < D; ++d) {
    int i = t[d] - 1;
    satisfaction += ss[d][i];

    last[i] = d + 1;
    for (int cc = 0; cc < 26; ++cc) {
      satisfaction -= c[cc] * ((d + 1) - last[cc]);
    }
    // cout<<satisfaction<<endl;
  }

  return satisfaction;
}
int hajime = clock();
int loop() {
  int old_score = init_score;
  int new_score = 0;
  srand(time(NULL));
  int cnt = 0;
  while (cnt <= 20000000) {
    if ((clock() - hajime) > 1.95 * CLOCKS_PER_SEC)
      break;
    int d = rand() % 365 + 1;

    int now = t[d];
    // t[d]++;
    // t[d]=min(t[d]+1,26);
    t[d] = min(rand() % 26 + 1, 26);
    new_score = score();
    if (new_score <= old_score) {
      t[d] = now;

    } else {
      old_score = new_score;
      // cout<<cnt<<" "<<d<<" "<<new_score<<endl;
    }

    cnt++;
  }

  return 0;
}

int main() {
  IOS;
  prec;

  Int x, k, d;
  cin >> x >> k >> d;
  if (x == 0) {
    if (d % 2 == 0)
      cout << 0;
    else
      cout << d;
    return 0;
  }
  if (x < 0)
    x *= -1;
  Int pos = x, tmp;
  while (k > 0) {
    pos -= d;
    k--;
    if (pos <= 0) {
      if (abs(pos) < abs(pos + 2 * d)) {

      } else {
        pos += 2 * d;
        k++;
      }
      if (k % 2 == 0) {
        cout << abs(pos) << endl;
        return 0;
      } else {
        cout << min(abs(pos + d), abs(pos - d)) << endl;
        return 0;
      }
    }
  }
  cout << pos;

  cout << endl;
  return 0;
}