結果
| 問題 |
No.3322 引っ張りだこ
|
| コンテスト | |
| ユーザー |
👑 potato167
|
| 提出日時 | 2025-11-01 03:09:54 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 138 ms / 2,000 ms |
| コード長 | 2,623 bytes |
| コンパイル時間 | 2,291 ms |
| コンパイル使用メモリ | 208,892 KB |
| 実行使用メモリ | 22,972 KB |
| 最終ジャッジ日時 | 2025-11-01 03:10:06 |
| 合計ジャッジ時間 | 9,044 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 43 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
vector<ll> candy(vector<ll> v, int K){
int N = v.size();
vector<int> L(N), R(N);
rep(i, 0, N) L[i] = i - 1, R[i] = i + 1;
auto del = [&](int ind) -> void {
if (ind == -1 || ind == N) return;
if (L[ind] == -1){
if (R[ind] != N) L[R[ind]] = -1;
}
else{
if (R[ind] == N) R[L[ind]] = N;
else{
int l = L[ind];
int r = R[ind];
R[l] = r;
L[r] = l;
}
}
R[ind] = -1;
};
vector<ll> ans = {0};
priority_queue<pair<ll, int>> pq;
rep(i, 0, N) pq.push({v[i], i});
rep(rp, 0, (N + 1) / 2){
if ((int)ans.size() > K) break;
while (true){
auto [val, ind] = pq.top();
pq.pop();
if (R[ind] == -1 || v[ind] != val){
continue;
}
ans.push_back(ans.back() + val);
int l = L[ind];
int r = R[ind];
del(ind);
if (l == -1 || r == N){
del(l), del(r);
}
else{
del(r);
v[l] = v[l] - v[ind] + v[r];
pq.push({v[l], l});
}
break;
}
}
return ans;
}
void solve();
// POP'N ROLL MUSIC / TOMOO
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
rep(i, 0, t) solve();
}
void solve(){
int N, K;
cin >> N >> K;
vector<ll> A(N + 1), B(N + 1);
rep(i, 0, N) cin >> A[i + 1];
rep(i, 0, N) cin >> B[i + 1];
const ll inf = (1ll << 50);
B[0] = -inf;
vector<pair<int, ll>> p;
N++;
if (K % 2 == 0){
A.push_back(0);
B.push_back(-inf);
N++;
K++;
}
B.push_back(0);
A.push_back(-inf);
N++;
ll ans = 0;
// 圧縮する
rep(i, 0, N){
if (A[i] >= B[i]) p.push_back({0, A[i] - B[i]});
else p.push_back({1, B[i] - A[i]});
ans += max(A[i], B[i]);
}
vector<ll> C;
for (int l = 0, r = 0; l < N; l = r){
while (r != N && p[l].first == p[r].first) r++;
ll tmp = 0;
rep(i, l, r) tmp += p[i].second;
C.push_back(-tmp);
}
// S[i] != T[i] の index の数を K に置き換える
K = max(0, (int)(C.size()) - 1 - K);
K /= 2;
// C に対して、JOI 飴を解き、ans に加算する
ans += candy(C, K)[K];
// 答えの出力
cout << ans << "\n";
}
potato167