/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 4ms 532.0 KiB
#3 Accepted 5ms 580.0 KiB
#4 Accepted 7ms 764.0 KiB
#5 Accepted 276ms 628.0 KiB
#6 Accepted 476ms 696.0 KiB
#7 Accepted 192ms 1.348 MiB
#8 Memory Exceeded ≥1602ms ≥128.016 MiB
#9 Accepted 915ms 50.129 MiB
#10 Accepted 90ms 804.0 KiB
#11 Time Exceeded ≥1597ms ≥1.887 MiB

Code

#include <bits/stdc++.h>

using namespace std;

#define el '\n'
// Warning: Disable #define int ...
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

#define int int64_t
// If values can be >= -1e9, S = 1e9 + 1
const int S = 1e12 + 1;
const int z = 1e6;

void run_case(int tc) {
  int n;
  cin >> n;
  vector<int> a(n), c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  if (n == 1) {
    cout << "0\n";
    return;
  }
  gp_hash_table<int, int, custom_hash> f;
  auto upd = [&](int p, int v) {
    for (p += 0; p < S; p += p & -p) {
      if (f.find(p) == f.end()) {
        f[p] = -S;
      }
      f[p] = max(f[p], v);
    }
  };
  auto get = [&](int p) {
    int ans = -S;
    for (p += 0; p; p ^= p & -p) {
      if (f.find(p) == f.end()) {
        f[p] = -S;
      }
      ans = max(f[p], ans);
    }
    return ans;
  };
  upd(z, 0);
  vector<int> p(1, a[0]);
  for (int i = 1; i < n - 1; i++) {
    upd(z + p.back(), get(z + p.back() + a[i] - c[i]) + 1);
    p.push_back(p.back() + a[i]);
  }
  int v = get(z + p.back() + a[n - 1] - c[n - 1]);
  if (v >= 0) {
    cout << v + 1 << el;
  } else {
    cout << "-1\n";
  }
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int _t = 1;
  cin >> _t;
  for (int i = 1; i <= _t; i++) {
    run_case(i);
  }
}

Information

Submit By
Type
Submission
Problem
P1199 F. Roy and Path Game
Language
C++17 (G++ 13.2.0)
Submit At
2026-01-06 17:34:26
Judged At
2026-01-06 17:34:26
Judged By
Score
50
Total Time
≥1602ms
Peak Memory
≥128.016 MiB