[CF1468D] Firecrackers - 贪心
Description
有一条长度为 n 的长廊,位置依次编号为 1,2,...,n,初态下 A,B 分别站在 a,b 位置,A 手上有 m 个鞭炮,第 i 个的爆炸延迟为 si。每秒内 A 有 3 种选择:向右走一格,向左走一格,选择一个鞭炮并点燃。B 每秒会向 A 靠近一格。当 B 与 A 重合后,游戏结束。求鞭炮爆炸数量的最大值。
Solution
分为两步:把该点的鞭炮都点了,赶紧跑路拖延时间
时间分为:点鞭炮时间(等于 A,B 距离 - 1)和跑路时间(等于 A 沿着 B 的反方向到墙的距离)
点哪些鞭炮?显然是从小到大点一批,点到什么时候点不动了为止
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n, m, a, b;
cin >> n >> m >> a >> b;
vector<int> s(m + 2);
for (int i = 1; i <= m; i++)
cin >> s[i];
sort(&s[1], &s[m + 1]);
int time_escape = 0, time_fire = 0;
time_fire = abs(a - b) - 1;
if (a < b)
time_escape = a;
else
time_escape = n - a + 1;
int time_total = time_escape + time_fire;
int ans = 0;
for (int i = time_escape + 1; ans < m && i <= time_total; i++)
if (i > s[ans + 1])
++ans;
else
i = max(i, s[ans + 1] - 1);
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
solve();
}