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

#include <algorithm>
#include <iostream>
#include <map>
#include <random>
#include <time.h>
#include <vector>
using namespace std;

const int MAX_D = 365;
const int C_SIZE = 26;

typedef vector<int> State;
typedef pair<int, State> CostState;

map<int, int> BIT_TABLE;

string createBits(int num) {
  string ret;
  for (int i = 0; i < C_SIZE; i++) {
    if ((num >> i) & 1) {
      ret += '1';
    } else {
      ret += '0';
    }
  }
  return ret;
}

void displayState(State state) {
  for (int i = 0; i < state.size(); i++) {
    cout << createBits(state[i]) << endl;
  }
}

int decreaseSatisfaction(const vector<int> &vec_c, vector<int> &last, int d) {
  int res = 0;
  for (int i = 0; i < C_SIZE; i++) {
    res += vec_c[i] * (d - last[i]);
  }
  return res;
}

int calcCost(const int D, State state, const vector<int> &vec_c,
             const vector<vector<int>> vec_s) {
  int res = 0;
  vector<int> last;
  for (int i = 0; i < C_SIZE; i++) {
    last.push_back(0);
  }

  for (int i = 0; i < D; i++) {
    res += vec_s[i][BIT_TABLE[state[i]]];
    last[BIT_TABLE[state[i]]] = (i + 1);
    res -= decreaseSatisfaction(vec_c, last, (i + 1));
  }
  return res;
}

CostState createCostState(const int D, State state, const vector<int> &vec_c,
                          const vector<vector<int>> vec_s) {
  // displayState(state);
  // cout << calcCost(D, state, vec_c, vec_s) << endl;
  return CostState(calcCost(D, state, vec_c, vec_s), state);
}

void input(int &D, vector<int> &vec_c, vector<vector<int>> &vec_s) {
  cin >> D;
  for (int i = 0; i < C_SIZE; i++) {
    int c;
    cin >> c;
    vec_c.push_back(c);
  }
  for (int i = 0; i < D; i++) {
    vector<int> vec;
    for (int j = 0; j < C_SIZE; j++) {
      int s;
      cin >> s;
      vec.push_back(s);
    }
    vec_s.push_back(vec);
  }
}

void init(const int D, State &state) {

  srand(0);

  for (int i = 0; i < D; i++) {
    int bit = 0;
    for (int j = 0; j < C_SIZE; j++) {
      if (i % C_SIZE == j) {
        bit = (1 << j);
      }
    }
    state.push_back(bit);
  }
  for (int i = 0; i < C_SIZE; i++) {
    BIT_TABLE[(1 << i)] = i;
  }
}

void output(int D, const State &res) {
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < C_SIZE; j++) {
      if ((res[i] >> j) & 1) {
        cout << (j + 1) << endl;
      }
    }
  }
}

void StateShuffle(const int D, State &state) {
  int bit = 1 << rand() % C_SIZE;
  int day = rand() % D;
  state[day] = bit;
}

/*
void GenenateState(const int D, State& state, const vector<int>& vec_c, const
vector<vector<int> >vec_s){ int max_cost = -1e8; int max_day = -1; State
max_state; for(int i = 0 ; i < D ; i++){ for(int j = 0 ; j < 2 ; j++){ State s =
state; if(j){ s[i] = min(1 << (C_SIZE-1) , s[i] << 1);
      }
      else {
        s[i] = max(1, s[i] >> 1);
      }
      int cost = calcCost(D, s, vec_c, vec_s);
      if(max_cost < cost){
        max_cost = cost;
        max_state = s;
        max_day = i;
      }
    }
  }
  cerr << "max_cost = " << max_cost << endl;
  state = max_state;
}
*/

State solve(const int D, const vector<int> &vec_c,
            const vector<vector<int>> vec_s, clock_t start) {
  vector<vector<int>> res;
  State state;
  init(D, state);

  const int GENERATE_COUNT = 1;
  const int LIMIT = 1;

  vector<CostState> states;
  states.push_back(createCostState(D, state, vec_c, vec_s));

  for (int i = 0;; i++) {
    if (clock() - start > 1.9 * CLOCKS_PER_SEC)
      break;
    vector<CostState> candidates;
    for (int j = 0; j < states.size() && j < LIMIT; j++) {
      CostState cstate = states[j];
      candidates.push_back(cstate);
      State state = cstate.second;
      for (int k = 0; k < GENERATE_COUNT; k++) {
        StateShuffle(D, state);
        // GenenateState(D, state, vec_c, vec_s);
        candidates.push_back(createCostState(D, state, vec_c, vec_s));
      }
    }
    sort(candidates.begin(), candidates.end());
    reverse(candidates.begin(), candidates.end());
    states = candidates;
  }
  cerr << states[0].first << endl;
  return states[0].second;
}

int main() {
  clock_t start = clock();
  int D;
  vector<int> vec_c;
  vector<vector<int>> vec_s;
  input(D, vec_c, vec_s);
  output(D, solve(D, vec_c, vec_s, start));
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <random>
#include <time.h>
#include <vector>
using namespace std;

const int MAX_D = 365;
const int C_SIZE = 26;

typedef vector<int> State;
typedef pair<int, State> CostState;

map<int, int> BIT_TABLE;

string createBits(int num) {
  string ret;
  for (int i = 0; i < C_SIZE; i++) {
    if ((num >> i) & 1) {
      ret += '1';
    } else {
      ret += '0';
    }
  }
  return ret;
}

void displayState(State state) {
  for (int i = 0; i < state.size(); i++) {
    cout << createBits(state[i]) << endl;
  }
}

int decreaseSatisfaction(const vector<int> &vec_c, vector<int> &last, int d) {
  int res = 0;
  for (int i = 0; i < C_SIZE; i++) {
    res += vec_c[i] * (d - last[i]);
  }
  return res;
}

int calcCost(const int D, State state, const vector<int> &vec_c,
             const vector<vector<int>> vec_s) {
  int res = 0;
  vector<int> last;
  for (int i = 0; i < C_SIZE; i++) {
    last.push_back(0);
  }

  for (int i = 0; i < D; i++) {
    res += vec_s[i][BIT_TABLE[state[i]]];
    last[BIT_TABLE[state[i]]] = (i + 1);
    res -= decreaseSatisfaction(vec_c, last, (i + 1));
  }
  return res;
}

CostState createCostState(const int D, State state, const vector<int> &vec_c,
                          const vector<vector<int>> vec_s) {
  // displayState(state);
  // cout << calcCost(D, state, vec_c, vec_s) << endl;
  return CostState(calcCost(D, state, vec_c, vec_s), state);
}

void input(int &D, vector<int> &vec_c, vector<vector<int>> &vec_s) {
  cin >> D;
  for (int i = 0; i < C_SIZE; i++) {
    int c;
    cin >> c;
    vec_c.push_back(c);
  }
  for (int i = 0; i < D; i++) {
    vector<int> vec;
    for (int j = 0; j < C_SIZE; j++) {
      int s;
      cin >> s;
      vec.push_back(s);
    }
    vec_s.push_back(vec);
  }
}

void init(const int D, State &state) {

  srand(0);

  for (int i = 0; i < D; i++) {
    int bit = 0;
    for (int j = 0; j < C_SIZE; j++) {
      if (i % C_SIZE == j) {
        bit = (1 << j);
      }
    }
    state.push_back(bit);
  }
  for (int i = 0; i < C_SIZE; i++) {
    BIT_TABLE[(1 << i)] = i;
  }
}

void output(int D, const State &res) {
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < C_SIZE; j++) {
      if ((res[i] >> j) & 1) {
        cout << (j + 1) << endl;
      }
    }
  }
}

void StateShuffle(const int D, State &state) {
  int bit = 1 << rand() % C_SIZE;
  int day = rand() % D;
  state[day] = bit;
}

/*
void GenenateState(const int D, State& state, const vector<int>& vec_c, const
vector<vector<int> >vec_s){ int max_cost = -1e8; int max_day = -1; State
max_state; for(int i = 0 ; i < D ; i++){ for(int j = 0 ; j < 2 ; j++){ State s =
state; if(j){ s[i] = min(1 << (C_SIZE-1) , s[i] << 1);
      }
      else {
        s[i] = max(1, s[i] >> 1);
      }
      int cost = calcCost(D, s, vec_c, vec_s);
      if(max_cost < cost){
        max_cost = cost;
        max_state = s;
        max_day = i;
      }
    }
  }
  cerr << "max_cost = " << max_cost << endl;
  state = max_state;
}
*/

State solve(const int D, const vector<int> &vec_c,
            const vector<vector<int>> vec_s, clock_t start) {
  vector<vector<int>> res;
  State state;
  init(D, state);

  const int GENERATE_COUNT = 1;
  const int LIMIT = 1;

  vector<CostState> states;
  states.push_back(createCostState(D, state, vec_c, vec_s));

  for (int i = 0;; i++) {
    // if(clock() - start > 1.9 * CLOCKS_PER_SEC)break;
    vector<CostState> candidates;
    for (int j = 0; j < states.size() && j < LIMIT; j++) {
      CostState cstate = states[j];
      candidates.push_back(cstate);
      State state = cstate.second;
      for (int k = 0; k < GENERATE_COUNT; k++) {
        StateShuffle(D, state);
        // GenenateState(D, state, vec_c, vec_s);
        candidates.push_back(createCostState(D, state, vec_c, vec_s));
      }
    }
    sort(candidates.begin(), candidates.end());
    reverse(candidates.begin(), candidates.end());
    states = candidates;
  }
  cerr << states[0].first << endl;
  return states[0].second;
}

int main() {
  clock_t start = clock();
  int D;
  vector<int> vec_c;
  vector<vector<int>> vec_s;
  input(D, vec_c, vec_s);
  output(D, solve(D, vec_c, vec_s, start));
  return 0;
}