結果
| 問題 |
No.2501 Maximum Inversion Number
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2023-07-13 14:01:37 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,738 bytes |
| コンパイル時間 | 1,375 ms |
| コンパイル使用メモリ | 127,264 KB |
| 実行使用メモリ | 7,424 KB |
| 最終ジャッジ日時 | 2024-10-11 19:23:50 |
| 合計ジャッジ時間 | 5,144 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 WA * 4 |
ソースコード
#include <cassert>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <utility>
#include <vector>
std::int64_t NChoose2(const int n) { return std::int64_t{n} * (n - 1) / 2; }
// <WA> #890252 ベース
// \sum_{i = 1}^n L_i = m のときに -1 を出力する。
std::int64_t Solve(
int m, const std::vector<int>& l, const std::vector<int>& r) {
const int n = l.size();
assert(std::ssize(r) == n);
std::int64_t ans = NChoose2(m);
using PriorityQueue =
std::priority_queue<int, std::vector<int>, std::greater<int>>;
std::map<int, PriorityQueue> c;
for (int i = 0; i < n; ++i) {
m -= l[i];
if (m < 0) return -1;
ans -= NChoose2(l[i]);
if (l[i] < r[i]) c[l[i]].emplace(r[i]);
}
while (m > 0 && !c.empty()) {
PriorityQueue que;
que.swap(c.begin()->second);
const int current_c = c.begin()->first;
c.erase(c.begin());
ans += NChoose2(current_c) * std::ssize(que);
const int next_v = (c.empty() || que.top() < c.begin()->first ?
que.top() : c.begin()->first);
const std::int64_t next_len = std::ssize(que) * (next_v - current_c);
if (next_len >= m) {
const int t0 =
current_c + (m + std::ssize(que) - 1) / std::ssize(que) - 1;
assert(current_c <= t0 && t0 < next_v);
const int num_of_t0 = (t0 + 1 - current_c) * std::ssize(que) - m;
m -= (t0 - current_c) * num_of_t0;
m -= (t0 + 1 - current_c) * (std::ssize(que) - num_of_t0);
assert(m == 0);
ans -= NChoose2(t0) * num_of_t0;
ans -= NChoose2(t0 + 1) * (std::ssize(que) - num_of_t0);
return ans;
}
m -= next_len;
ans -= NChoose2(next_v) * std::ssize(que);
while (!que.empty() && que.top() == next_v) que.pop();
if (!que.empty()) {
if (c.empty() || next_v < c.begin()->first) {
assert(c.emplace(next_v, que).second);
} else {
if (que.size() > c.begin()->second.size()) {
std::swap(que, c.begin()->second);
}
for (; !que.empty(); que.pop()) {
c.begin()->second.emplace(que.top());
}
}
}
}
return -1;
}
int main() {
constexpr int kMaxT = 200000, kMaxN = 200000, kMaxM = 1000000000;
int t;
std::cin >> t;
assert(1 <= t && t <= kMaxT);
while (t--) {
int n, m;
std::cin >> n >> m;
assert(1 <= n && n <= kMaxN && 1 <= m && m <= kMaxM);
std::vector<int> l(n);
for (int i = 0; i < n; ++i) {
std::cin >> l[i];
}
std::vector<int> r(n);
for (int i = 0; i < n; ++i) {
std::cin >> r[i];
assert(0 <= l[i] && l[i] <= r[i] && r[i] <= kMaxM);
}
std::cout << Solve(m, l, r) << '\n';
}
return 0;
}
emthrm