题解:P11859 [CCC 2025 Senior] 画画 / Pretty Pens
前置知识
multiset 的性质与操作。
题意
你有
思路
我们先不考虑我们可以更换画笔颜色的操作,我们发现我们每次肯定会选取每种颜色美丽值最高的画笔。我们可以对每一种颜色开一个 multiset 去维护每种颜色的画笔的最大美丽值以及修改操作。
接下来考虑我们可以在每次绘画前修改某一支画笔的颜色。我们考虑什么时候这个操作会让我们的答案变优,我们会选取某一种颜色的画笔的次大值去替换掉某一种颜色的画笔的最大值。为什么呢,首先不难证明我们要么拿最大值去换要么拿次大值去换。而我们拿最大值去换后次大值一定会被取,所以我们拿次大值去换一定不劣。于是我们每次只需要找到所有颜色的画笔的次大值中的最大值去和所有颜色的最大值的最小值去比较取哪个更优。这个也很好用 multiset 维护。
代码
#include<bits/stdc++.h>
#define int long long
#define N 200005
using namespace std;
multiset<int>s[N], ma, mma;
int p[N], c[N], n, m, q, ans;
signed main()
{
ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> q;
for(int i = 1; i <= m; i ++)
{
s[i].insert(0);
s[i].insert(0);
}
for(int i = 1; i <= n; i ++)
{
cin >> c[i] >> p[i];
s[c[i]].insert(p[i]);
}
for(int i = 1; i <= m; i ++)
{
auto it = s[i].end();
it --;
ans += *it;
mma.insert(*it);
it --;
ma.insert(*it);
}
auto it = ma.end();
it --;
cout << max(ans + *it - *mma.begin(), ans) << '\n';
while(q --)
{
int op, x, y;
cin >> op >> x >> y;
if((op == 1 && c[x] == y) || (op == 2 && p[x] == y))//判掉避免出问题
{
it = ma.end();
it --;
cout << max(ans + *it - *mma.begin(), ans) << '\n';
continue;
}
if(op == 1)
{
auto i = s[c[x]].end(), j = s[y].end();
i --, j --;
ans -= *i;
ans -= *j;
mma.erase(mma.lower_bound(*i));//首先更新最大值
mma.erase(mma.lower_bound(*j));
i --, j --;
ma.erase(ma.lower_bound(*i));//更新次大值
ma.erase(ma.lower_bound(*j));
s[y].insert(p[x]);
s[c[x]].erase(s[c[x]].lower_bound(p[x]));//更新这种颜色
i = s[c[x]].end(), j = s[y].end();
i --, j --;
mma.insert(*i);
mma.insert(*j);
ans += *i;//我们只需要给答案加上被修改掉的部分
ans += *j;
i --, j --;
ma.insert(*i);
ma.insert(*j);
it = ma.end();
it --;
cout << max(ans + *it - *mma.begin(), ans) << '\n';
c[x] = y;//最后别忘了修改数组
}
else
{//本质与上一个操作相同
auto i = s[c[x]].end();
i --;
mma.erase(mma.lower_bound(*i));
ans -= *i;
i --;
ma.erase(ma.lower_bound(*i));
s[c[x]].erase(s[c[x]].lower_bound(p[x]));
s[c[x]].insert(y);
i = s[c[x]].end();
i --;
mma.insert(*i);
ans += *i;
i --;
ma.insert(*i);
it = ma.end();
it --;
cout << max(ans + *it - *mma.begin(), ans) << '\n';//换了颜色不一定最优
p[x] = y;
}
}
return 0;
}
AC 记录。