题解:P15699 [2018 KAIST RUN Spring] Touch The Sky
题目大意
给定
题目分析
总的来说,本题可以通过邻项交换法确定处理的基准顺序,再利用反悔贪心配合优先队列在序列上进行动态决策。
先说关于决策顺序的结论:将所有气球按照
::::info[决策顺序的证明]
假设已经选定了一个最优气球集合
-
若先使用气球
A 后使用气球B : 需同时满足H \le L_A 且H + D_A \le L_B 。由后式变形得H \le L_B - D_A ,此时前置高度H 需满足的上限为:H \le \min(L_A, L_B - D_A) -
若先使用气球
B 后使用气球A : 同理,前置高度H 需满足的上限为:H \le \min(L_B, L_A - D_B)
为了使可承受的前置高度范围尽可能大,若先
关注交叉限制项,应当满足:
::::
按上述排序结果从前往后遍历。维护一个大根堆存储当前已选气球的容量
接下来我先说关于反悔贪心的结论,证明写在下面。
对于当前遍历到的气球
- 若
H \le L_i ,直接将i 加入当前的选择集合中,更新H \leftarrow H + D_i ,并将D_i 压入大根堆。 - 若
H > L_i ,由于此时无法直接使用气球i ,我们尝试用气球i 替换已选集合中容量最大的气球(即堆顶元素D_{\max} )。若D_{\max} > D_i ,则弹出堆顶并插入D_i ,同时更新H \leftarrow H - D_{\max} + D_i 。
::::info[关于反悔替换后新高度合法性的证明]
假设被替换的堆顶气球为
根据替换触发条件,满足
在尚未充入气球
将上式代入排序不等式中,可得新高度
由于
| 由此可知,执行替换后,新高度 |
|---|
参考代码
::::info[P15699.cpp]
// Problem: P15699 [2018 KAIST RUN Spring] Touch The Sky
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P15699
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
#include <bits/stdc++.h>
// #define Multiple
#define i32 int
#define u32 unsigned i32
#define c32 const i32
#define i64 long long
#define u64 unsigned i64
#define c64 const i64
#define dou double
#define vec vector
#define str string
#define pii pair< i32, i32 >
#define pdd pair< dou, dou >
#define fir first
#define sec second
#define il inline
#define pb push_back
#define SZ(s) ((int)s.size())
#define ALL(a) a.begin(), a.end()
#define MAX(a, b) (a) = max((a), (b))
#define MIN(a, b) (a) = min((a), (b))
#define MST(a, s) memset(a, s, sizeof(a))
#define U(i, a, b) for (i32 i = (a), i##E = (b); i <= i##E; ++i)
#define d(i, a, b) for (i32 i = (a), i##E = (b); i >= i##E; --i)
#define DEBUG(x) cerr << "[Debug] " << #x << " = " << (x) << '\n'
#define FRE(fn) freopen(fn ".in", "r", stdin), freopen(fn ".out", "w", stdout)
using namespace std;
il void IOS(c32 _) { ios::sync_with_stdio(0), cin.tie(0), cout << fixed << setprecision(_); }
bool MEM_ST;
namespace stO_HWQ_Orz {
c32 N = 250000 + 5;
struct balloon {
i64 l, d;
bool operator<(const balloon &o) const { return l + d < o.l + o.d; }
} a[N];
il void Main() {
i32 n; cin >> n;
U(i, 1, n) cin >> a[i].l >> a[i].d;
sort(a + 1, a + n + 1);
priority_queue< i64 > q;
i64 h = 0;
U(i, 1, n) {
if (h <= a[i].l) {
h += a[i].d;
q.push(a[i].d);
} else if (!q.empty() && q.top() > a[i].d) {
h -= q.top();
h += a[i].d;
q.pop();
q.push(a[i].d);
}
}
cout << q.size() << '\n';
}
il void Init() {}
} // namespace stO_HWQ_Orz
bool MEM_ED;
int main() {
IOS(6);
clock_t START = clock();
#ifdef Multiple
stO_HWQ_Orz::Init();
int Case_T;
cin >> Case_T;
while (Case_T--) stO_HWQ_Orz::Main();
#else
stO_HWQ_Orz::Main();
#endif
cerr << (dou)(clock() - START) / CLOCKS_PER_SEC << " s\n";
cerr << 1.0 * abs(&MEM_ED - &MEM_ST) / 1024 / 1024 << " MB\n";
return 0;
}
::::
时间复杂度为