我回来了,纵使日薄西山
zhouhuanyi · · 题解
CF436E Cardboard Box
考虑三张从下标
先只看
现在再把
上面合并两张表的运算称为
设有
固定下标和为
由此可以估计究竟要改多少个位置。把差值逐个排出来,部分和从
回到刚才的合并。即使这些改动全落在左组,左边分走的下标和也至多改变
回到本题
把一张表的下标理解成在一关中拿到的星数,表里填的数理解成用时,第
代入
设手中已有一个
于是补一颗星只有两种改法要比。一种是在某一关加一星,花费为
还差前半步。怎样跳过之前的卷积系数,直接找到一个离
如果
这样标价以后,最便宜的
想象把标记按单价从小到大排好,同价时按关卡编号排列。一个折扣关的两枚标记一定相邻。取前
仍看开头的两张表
去掉那枚不能单独兑现的标记,当前只在第一关拿了一星,花了 02。这里的加星、退星都是修改计划,每关最后只选一种玩法。
最后,排好整列标记只是为了描述那个切口。实现时只需要知道第
每轮扫描
上述分治和交换论证来自周桓毅《On the Structure of
参考代码
#include <algorithm>
#include <array>
#include <iostream>
#include <limits>
#include <utility>
#include <vector>
using namespace std;
// 返回第 k 小的价格,以及在这个价格上还要取多少枚标记。
pair<int, int> price_limit(const vector<int>& price, int k) {
int prefix = 0;
for (int shift = 24; shift >= 0; shift -= 8) {
array<int, 256> count{};
for (int p : price) {
int high = p >> shift;
if ((high >> 8) == prefix) ++count[high & 255];
}
int digit = 0;
while (k > count[digit]) {
k -= count[digit];
++digit;
}
prefix = prefix * 256 + digit;
}
return {prefix, k};
}
void add_one(const vector<array<int, 3>>& cost, vector<int>& stars) {
using Move = pair<int, int>; // 用时变化、关卡编号
const Move none = {numeric_limits<int>::max(), -1};
Move up = none, two = none, down = none;
for (int j = 0; j < static_cast<int>(stars.size()); ++j) {
int s = stars[j];
if (s < 2) up = min(up, Move{cost[j][s + 1] - cost[j][s], j});
if (s == 0) two = min(two, Move{cost[j][2], j});
if (s > 0) down = min(down, Move{cost[j][s - 1] - cost[j][s], j});
}
if (two.second >= 0 && down.second >= 0 && two.first + down.first < up.first) {
stars[two.second] = 2;
--stars[down.second];
} else {
++stars[up.second];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, w;
cin >> n >> w;
vector<array<int, 3>> cost(n);
vector<int> price(2 * n), stars(n);
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
cost[i] = {0, a, b};
price[2 * i] = min(2 * a, b);
price[2 * i + 1] = 2 * b - price[2 * i];
}
auto [limit, equal_needed] = price_limit(price, w);
for (int j = 0; j < 2 * n; ++j) {
if (price[j] < limit) {
++stars[j / 2];
} else if (price[j] == limit && equal_needed > 0) {
++stars[j / 2];
--equal_needed;
}
}
for (int i = 0; i < n; ++i) {
if (stars[i] != 1 || 2 * cost[i][1] <= cost[i][2]) continue;
stars[i] = 0; // 放回被拆开的一枚,此时已有 w - 1 星的最优方案。
add_one(cost, stars);
break;
}
long long answer = 0;
for (int i = 0; i < n; ++i) answer += cost[i][stars[i]];
cout << answer << '\n';
for (int s : stars) cout << s;
cout << '\n';
}