結果

問題 No.777 再帰的ケーキ
ユーザー kakira9618kakira9618
提出日時 2018-09-21 13:01:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 4,553 bytes
コンパイル時間 1,885 ms
コンパイル使用メモリ 187,388 KB
実行使用メモリ 20,288 KB
最終ジャッジ日時 2024-04-29 21:21:41
合計ジャッジ時間 5,304 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 8 ms
5,376 KB
testcase_07 AC 14 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 8 ms
5,376 KB
testcase_19 AC 13 ms
5,376 KB
testcase_20 AC 15 ms
5,376 KB
testcase_21 AC 15 ms
5,376 KB
testcase_22 AC 16 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 AC 9 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 308 ms
20,260 KB
testcase_29 AC 311 ms
20,156 KB
testcase_30 AC 316 ms
20,288 KB
testcase_31 AC 327 ms
20,280 KB
testcase_32 AC 194 ms
20,284 KB
testcase_33 AC 37 ms
5,376 KB
testcase_34 AC 54 ms
5,376 KB
testcase_35 AC 199 ms
11,856 KB
testcase_36 AC 192 ms
20,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
constexpr int MOD = 1000000007;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os; }
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p){os << "(" << p.first << ", " << p.second << ")"; return os;}

struct Phone {
  int h, w, t;
  bool operator<(const Phone &rhs) const {
    if (h == rhs.h) {
      return w > rhs.w;
    }
    return h < rhs.h;
  }
};

struct BIT {
  vector<ll> data;
  BIT(int n): data(n + 1) {}
  ll getmax(int a) {
    ll ret = 0;
    for(int i = a; i > 0; i -= i & -i) {
      ret = max(ret, data[i]);
    }
    return ret;
  }
  void update(int a, ll w) {
    for(int i = a; i < data.size(); i += i & -i) {
      data[i] = max(data[i], w);
    }
  }
};


ll LIS(vector<ll> &A) {
  vector<ll> dp; 
  for(int i = 0; i < A.size(); i++) {
    int k = lower_bound(all(dp), A[i]) - dp.begin();
    if (k == dp.size()) dp.push_back(A[i]);
    else dp[k] = A[i];
  }
  return dp.size();
}

ll LIS_dp(vector<ll> &A) {
  vector<ll> dp(A.size());
  for (int i = 0; i < A.size(); i++) {
    ll ma = 0;
    for (int j = 0; j < i; j++) {
      if (A[j] >= A[i]) continue;
      ma = max(ma, dp[j]);
    }
    dp[i] = ma + 1;
  }
  return *max_element(all(dp));
}

// BIT解(想定解)
ll solve1(int N, vector<Phone> &P) {
  vector<int> W(N);
  for (int i = 0; i < N; i++) {
    W[i] = P[i].w;    
  }
  sort(all(W));
  W.erase(unique(W.begin(), W.end()), W.end());

  BIT bit(N + 2);
  for(int i = 0; i < N; i++) {
    int k = lower_bound(all(W), P[i].w) - W.begin() - 1;
    k += 2; // for BIT
    ll mi = bit.getmax(k);
    bit.update(k + 1, mi + P[i].t);
  }
  return bit.getmax(N + 2);
}

// Naive DP解(O(N^2), TLE)
ll solve2(int N, vector<Phone> &P) {
  vector<ll> dp(N, 0);
  for(int i = 0; i < N; i++) {
    ll ma = 0;
    for(int j = 0; j < i; j++) {
      if (P[j].w >= P[i].w) continue;
      ma = max(ma, dp[j]);
    }
    dp[i] = ma + P[i].t;
  }
  return *max_element(all(dp));
}

// 二分探索 LIS解(O(N log N), T_i = 1)
ll solve3(int N, vector<Phone> &P) {
  vector<ll> W(N);
  for (int i = 0; i < N; i++) {
    W[i] = P[i].w;    
  }
  return LIS(W);
}

// LIS DP解(O(N^2), T_i = 1)
ll solve4(int N, vector<Phone> &P) {
  vector<ll> W(N);
  for (int i = 0; i < N; i++) {
    W[i] = P[i].w;    
  }
  return LIS_dp(W);
}

// 全探索解
ll solve5(int N, vector<Phone> &P) {
  ll ans = 0;
  for(int i = 0; i < 1 << N; i++) {
    vector<ll> W;
    ll sum = 0;
    for(int j = 0; j < N; j++) {
      if (i >> j & 1) {
        W.push_back(P[j].w);
        sum += P[j].t;
      }
    }
    bool flag = true;
    for(int j = 0; j + 1 < W.size(); j++) {
      if (W[j] >= W[j + 1]) {
        flag = false;
        break;
      }
    }
    if (!flag) continue;
    ans = max(ans, sum);
  }
  return ans;
}

void solve() {
  int N;
  cin >> N;
  if (N < 1 || N > 200000) exit(1);
  map<pii, int> M;
  for(int i = 0; i < N; i++) {
    int h, w, t;
    cin >> h >> w >> t;
    if (h < 1 || h > 1000000000) exit(1);
    if (w < 1 || w > 1000000000) exit(1);
    if (t < 1 || t > 1000000000) exit(1);
    M[{h, w}] = max(M[{h, w}], t);
  }
  vector<Phone> P;
  for(auto &&x : M) {
    P.push_back({x.first.first, x.first.second, x.second});
  }
  sort(all(P));
  N = P.size();

  bool solve1_f = true;
  bool solve2_f = N <= 2000;
  bool solve3_f = true;
  for (int i = 0; i < N; i++) {
    if (P[i].t != 1) solve3_f = false;
  }
  bool solve4_f = solve3_f && (N <= 2000);
  bool solve5_f = N <= 16;

  vector<ll> ans;
  if (solve1_f) ans.push_back(solve1(N, P));
  if (solve2_f) ans.push_back(solve2(N, P));
  if (solve3_f) ans.push_back(solve3(N, P));
  if (solve4_f) ans.push_back(solve4(N, P));
  if (solve5_f) ans.push_back(solve5(N, P));
  
  set<ll> S;
  for (int i = 0; i < ans.size(); i++) {
    S.insert(ans[i]);
  }

  if (S.size() > 1) {
    cout << "Error!" << endl;
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << ans[0] << endl;
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}
0