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

#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

ll dp[3010][3010][4];

ll items[3010][3010];

void show_table(ll I, ll J, ll k) {
  for (ll i = 0; i <= I; i++) {
    for (ll j = 0; j <= J; j++) {
      cout << dp[i][j][k] << " ";
    }
    cout << "" << endl;
  }
  cout << "" << endl;
}

int main() {
  ll R, C, K;
  scanf("%lld %lld %lld", &R, &C, &K);

  vector<ll> list;
  for (ll i = 0; i < K; i++) {
    ll r, c, v;
    scanf("%lld %lld %lld", &r, &c, &v);

    r--;
    c--;
    items[r][c] = v;
  }

  for (ll r = 0; r < R; r++) {
    for (ll c = 0; c < C; c++) {
      ll v = items[r][c];

      dp[r][c + 1][0] = max(dp[r][c + 1][0], dp[r][c][0]);
      dp[r][c + 1][1] = max(dp[r][c + 1][1], dp[r][c][0] + v);
      dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][0] + v);

      dp[r][c + 1][1] = max(dp[r][c + 1][1], dp[r][c][1]);
      dp[r][c + 1][2] = max(dp[r][c + 1][2], dp[r][c][1] + v);
      dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][1] + v);

      dp[r][c + 1][2] = max(dp[r][c + 1][2], dp[r][c][2]);
      dp[r][c + 1][3] = max(dp[r][c + 1][3], dp[r][c][2] + v);
      dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][2] + v);

      dp[r][c + 1][3] = max(dp[r][c + 1][3], dp[r][c][3]);
      // dp[r][c + 1][4] = max(dp[r][c + 1][2], dp[r][c][3] + 0);
      dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][3] + 0);
    }
  }

  cout << (dp[R][C - 1][0]) << endl;
  // cout << (dp[R][C - 1][1]) << endl;
  // cout << (dp[R][C - 1][2]) << endl;

  // show_table(3, 5, 0);
  // show_table(3, 5, 1);
  // show_table(3, 5, 2);
  // show_table(3, 5, 3);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

ll grid[3010][3010];

vll positions[3010];
vll values[3010];

void show_table(ll I, ll J) {
  for (ll i = 0; i <= I; i++) {
    for (ll j = 0; j <= J; j++) {
      cout << grid[i][j] << " ";
    }
    cout << "" << endl;
  }
  cout << "" << endl;
}

// ms.insert(1);
// ms.insert(3);
// ms.insert(5);
// ms.insert(7);
// ms.insert(3);
// ms.insert(5);
// cout << (ms.find(1) != ms.end()) << endl;  // 1
// cout << (ms.find(2) != ms.end()) << endl;  // 0
// cout << *ms.begin() << endl;               // 1
// cout << *ms.rbegin() << endl;              // 7
// cout << ms.size() << endl;                 // 6
// ms.erase(3);
// cout << ms.size() << endl;  // 4!
// ms.erase(ms.find(5));
// cout << ms.size() << endl;  // 3!
// ms.clear();
// cout << ms.size() << endl;  // 0

class Max3 {
  multiset<ll> _ms;
  ll _num;

public:
  Max3(ll num) {
    _num = num; //
  }

  void insert(ll value) {
    _ms.insert(-value);
    // ll min_index = 0;
    // for (ll i = 0; i < 3; i++) {
    //   if (_values[i] < _values[min_index]) {
    //     min_index = i;
    //   }
    // }

    // if (_values[min_index] < value) {
    //   _values[min_index] = value;
    // }
  }

  ll get() {
    ll sum = 0;

    ll i = 0;
    for (auto ite = _ms.begin(); ite != end(_ms); ite++) {
      if (i >= _num) {
        break;
      }
      ll v = (*ite);
      sum += -v;
      i++;
    }
    return sum;
  }
};

int main() {
  ll R, C, K;
  scanf("%lld %lld %lld", &R, &C, &K);

  fill(grid[0], grid[3005], -1);
  grid[0][0] = 0;

  vector<tuple<ll, ll, ll>> tlist;
  for (ll i = 0; i < K; i++) {
    ll a, b, c;
    scanf("%lld %lld %lld", &a, &b, &c);
    tlist.emplace_back(make_tuple(a, b, c));
    // cout << get<0>(tlist[i]) << endl;
  }
  sort(tlist.begin(), tlist.end());

  for (ll i = 0; i < K; i++) {
    // cout << get<0>(tlist[i]) << " " << get<1>(tlist[i]) << " "
    //      << get<2>(tlist[i]) << endl;
    ll r = get<0>(tlist[i]);
    ll c = get<1>(tlist[i]);
    ll v = get<2>(tlist[i]);
    r--;
    c--;
    positions[r].emplace_back(c);
    values[r].emplace_back(v);
  }

  // for (ll i = 0; i < K; i++) {
  //   ll r, c, v;
  //   scanf("%lld %lld %lld", &r, &c, &v);
  //   r--;
  //   c--;

  //   positions[r].emplace_back(c);
  //   values[r].emplace_back(v);
  // }

  for (ll r = 0; r < R; r++) {
    for (ll c = 0; c < C; c++) {
      if (grid[r][c] == -1)
        continue;

      ll count = 0;

      auto ite = lower_bound(positions[r].begin(), positions[r].end(), c);
      ll start_index = ite - positions[r].begin(); // 先頭までの距離

      if (grid[r + 1][c] < grid[r][c]) {
        grid[r + 1][c] = grid[r][c];
      }

      auto max3 = Max3(3);

      for (ll index = start_index; index < positions[r].size(); index++) {
        ll item_position = positions[r][index];

        ll value = values[r][index];
        max3.insert(value);
        ll plus_value = max3.get();

        if (grid[r + 1][item_position] < grid[r][c] + plus_value) {
          grid[r + 1][item_position] = grid[r][c] + plus_value;
        }
      }
      // cout << *ite << endl; // 値
      // ll index = ite - items.begin();  // 先頭までの距離
      // items.end() - ite                // 末尾までの距離
    }
  }

  ll ans = 0;
  for (ll c = 0; c < C; c++) {
    ans = max(ans, grid[R][c]);
  }

  // show_table(5, 6);

  cout << (ans) << endl;
}