QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#225153#7615. Sequence FoldingLainTL 3510ms125836kbC++202.7kb2023-10-24 01:28:002023-10-24 01:28:02

Judging History

你现在查看的是最新测评结果

  • [2023-10-24 01:28:02]
  • 评测
  • 测评结果:TL
  • 用时:3510ms
  • 内存:125836kb
  • [2023-10-24 01:28:00]
  • 提交

answer

#include <bits/extc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;


// Source: ecnerwala
// Templates for Policy Based Data Structures
struct splitmix64_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 =
        std::chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

using namespace __gnu_pbds;
template <typename K, typename V, typename Hash = splitmix64_hash>
using hash_map = gp_hash_table<K, V, Hash>;

template <typename K, typename Hash = splitmix64_hash>
using hash_set = hash_map<K, null_type, Hash>;

template <class T, class Compare = less<>>
using ordered_set =
    tree<T, null_type, Compare, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

  auto msb = [&](ll x)->int {
    if (x == 0) return 0;
    return __lg(x) + 1;
  };

  ll n;
  int m;
  cin >> n >> m;
  vector<ll> p(m);
  vector<vector<ll>> levels(64);
  for (auto& x : p) {
    cin >> x, x--;
    levels[msb(x)].push_back(x);
  }
  map<ll, array<int, 2>> dp;
  for (auto& x : p) {
    dp[x] = {1, 0};
  }
  for (auto x: p) {
    for (int j = msb(x);j < 62; j++) {
      // Iterate over all the parent sets...
      ll up = ((1LL<<j) - x - 1) + (1LL<<j);
      if (up >= n) break;
      if (up < n && dp.find(up) == dp.end()) {
        levels[msb(up)].push_back(up);
        dp[up] = {0, 1};
      }
    }
  }

  for (int j = sz(levels) - 1; j > 0; j--) {
    if (levels[j].empty()) continue;
    auto& level = levels[j];
    for (auto& x : level) {
      int bit = j - 1;
      ll to = (1LL<<bit) - 1 - (x - (1LL<<bit));
      if (dp.find(to) == dp.end()) {
        levels[msb(to)].push_back(to);
        for (int k = msb(to); k < j; k++)  {
          ll up = ((1LL<<k) - to - 1) + (1LL<<k);
          if (dp.find(up) == dp.end()) {
            levels[msb(up)].push_back(up);
            dp[up] = {0, 1};
          }
        }
        dp[to] = {0, 1};
      }

      array<int, 2> ndp;
      ndp[0] = min(dp[to][0] + dp[x][0], dp[to][1] + dp[x][1] + 1);
      ndp[1] = min(dp[to][1] + dp[x][1], dp[to][0] + dp[x][0] + 1);
      dp[to] = ndp;
    }
  }

  cout << min(dp[0][0], dp[0][1]) << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3420kb

input:

8 3
1 5 8

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3512kb

input:

1 1
1

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 3510ms
memory: 125836kb

input:

17179869184 100000
138476 774165 993977 1152277 1236393 1244970 1534017 1693701 1763926 1778781 1842066 1874644 1885666 2120429 2485344 2977941 3158255 3340476 3504862 4000117 4066652 4197639 4338723 4389163 4576965 4761601 5097091 5175412 5295902 5810551 5855982 6001770 6111262 6163309 6351547 6582...

output:

99999

result:

ok single line: '99999'

Test #4:

score: -100
Time Limit Exceeded

input:

549755813888 100000
16886305 20807233 27844305 30727441 30898344 35755457 38085835 43336454 47877882 50347884 53237225 53718183 60030541 66954859 80773500 82511603 84025040 86398615 93070876 94502940 98906398 100677488 103720017 105522694 116741042 122492007 135222584 155167916 160926866 166110647 1...

output:


result: