답안 #624758

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624758 2022-08-08T17:04:15 Z MilosMilutinovic San (COCI17_san) C++14
120 / 120
180 ms 41400 KB
/**
 *    author:  wxhtzdy
 *    created: 08.08.2022 18:38:51
**/
#include <bits/stdc++.h>

using namespace std;

template <typename T>
class fenwick {
  public:
  vector<T> fenw;
  int n;
  fenwick(int _n) : n(_n) {
    fenw.resize(n);
  }
  void modify(int x, T v) {
    while (x < n) {
      fenw[x] += v;
      x |= (x + 1);
    }
  }
  T get(int x) {
    T v{};
    while (x >= 0) {
      v += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n;
  long long k;
  cin >> n >> k;
  vector<int> h(n);
  vector<long long> g(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i] >> g[i];
  }
  auto xs = h;
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (int i = 0; i < n; i++) {
    h[i] = lower_bound(xs.begin(), xs.end(), h[i]) - xs.begin();
  }
  int x = min(n, 20);
  int y = n - x;
  vector<pair<long long, int>> v;
  for (int mask = 0; mask < (1 << x); mask++) {
    int mx = 0;
    bool ok = true;
    long long sum = 0;
    for (int i = 0; i < x; i++) {
      if (mask >> i & 1) {
        if (mx > h[i]) {
          ok = false;
          break;
        }
        sum += g[i];
        mx = h[i];
      }
    }
    if (!ok) {
      continue;
    }
    v.emplace_back(sum, mx);
  }
  sort(v.begin(), v.end());
  int m = (int) v.size();
  vector<vector<int>> qs(m + 1);
  for (int mask = 0; mask < (1 << y); mask++) {
    int mx = -1, mn = n - 1;
    bool ok = true;
    long long sum = 0;
    for (int i = 0; i < y; i++) {
      if (mask >> i & 1) {
        if (mx > h[x + i]) {
          ok = false;
          break;
        }
        sum += g[x + i];
        mx = h[x + i];
        mn = min(mn, h[x + i]);
      }
    }
    if (!ok) {
      continue;
    }
    int low = 0, high = m - 1, idx = m;
    while (low <= high) {
      int mid = low + high >> 1;
      if (v[mid].first >= k - sum) {
        idx = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    qs[idx].push_back(mn);
  }                  
  long long ans = 0;
  fenwick<int> fenw(n);
  for (int i = m - 1; i >= 0; i--) {
    fenw.modify(v[i].second, 1);
    for (auto& p : qs[i]) {
      ans += fenw.get(p);
    }
  }
  cout << ans << '\n';
  return 0;
}

Compilation message

san.cpp: In function 'int main()':
san.cpp:95:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   95 |       int mid = low + high >> 1;
      |                 ~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 10712 KB Output is correct
2 Correct 17 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 41400 KB Output is correct
2 Correct 13 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 6604 KB Output is correct
2 Correct 33 ms 1500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 23096 KB Output is correct
2 Correct 84 ms 4592 KB Output is correct