题解:P16034 [CSPro 33] 十滴水
FJ_EYoungOneC · · 题解
问题描述
这是一个一维的十滴水游戏,我们发现当一个水滴爆炸的时候需要完成以下操作:
- 删掉当前点。
- 将左侧最近的非零点加一滴水。
- 将右侧最近的非零点加一滴水。
- 如果左侧的水滴爆炸,跳到步骤 1。
- 如果右侧的水滴爆炸,跳到步骤 1。
那么需要快速的删掉序列中的一个点,我们可以选用 map(时间复杂度
从步骤 4、5 跳回步骤 1,我们可以使用递归、迭代。
AC_Code #1(map)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 3e5 + 10, M = 5 * N;
int n, m, q;
map<int, int> h;
void dfs(int x)
{
auto p = h.find(x);
auto l = p, r = p;
l -- , r ++ ;
int lv = (*l).first, rv = (*r).first;
h[lv] ++ , h[rv] ++ ;
h.erase(p);
if (h[lv] >= 5)
dfs(lv);
if (h.count(rv) && h[rv] >= 5)
dfs(rv);
}
void insert(int x, int v)
{
if (!h.count(x))
h[x] = v;
else
{
h[x] += v;
if (h[x] >= 5)
dfs(x);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
h[0] = h[n + 1] = -1e9;
while (m -- )
{
int a, b;
cin >> a >> b;
insert(a, b);
}
while (q -- )
{
int x;
cin >> x;
insert(x, 1);
cout << h.size() - 2 << '\n';
}
return 0;
}
AC_Code #2(链表)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
int n, m, q;
int l[N], r[N], w[N];
unordered_map<int, int> h;
PII a[N];
int main()
{
cin >> n >> m >> q;
for (int i = 1; i <= m; ++ i )
cin >> a[i].x >> a[i].y;
sort(a + 1, a + m + 1);
for (int i = 1; i <= m; ++ i )
{
l[i] = i - 1, r[i] = i + 1;
w[i] = a[i].y;
h[a[i].x] = i;
}
int res = m;
while (q -- )
{
int x;
cin >> x;
int p = h[x];
w[p] ++ ;
while (w[p] >= 5)
{
res -- ;
r[l[p]] = r[p], l[r[p]] = l[p];
w[l[p]] ++ , w[r[p]] ++ ;
w[p] = 0;
int np = p;
if (r[p] <= m && w[r[p]] >= 5)
np = r[p];
if (l[p] && w[l[p]] >= 5)
np = l[p];
p = np;
}
cout << res << '\n';
}
return 0;
}