跳到正文

题解

AT_dwango2015_finals_4 コインの取り合い Solution

MatrixGroup约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v1当前

Analysis

Consider the case where Q=0Q=0 first. Since after the ii-th turn, the states of the coins 1,2,⋯ ,i1,2,\cdots,i are determined, and the states of the coins i+1,i+2,⋯ ,Ni+1,i+2,\cdots,N are unchanged, one can easily come up with the following dynamic programming solution: let fi/gif_i/g_i be the maximum possible (signed) difference of the score of the current player and that of the other player, only considering the coins i,i+1,⋯ ,Ni,i+1,\cdots,N, where the state of the coin ii is head/tail. The initial value of the dp is fN=SN,gN=−SNf_N=S_N,g_N=-S_N, and the transition formula is fi=Si−gi+1,gi=max⁡(Si−fi+1,−Si−gi+1)f_i=S_i-g_{i+1},g_i=\max(S_i-f_{i+1},-S_i-g_{i+1}). The answer is therfore the average value of f1f_1 and the sum of SS, which yields a solution with time complexity O(NQ)O(NQ).

To solve the problem, one must find f1f_1 in a faster way. Consider hifi−gih_if_i-g_i. Expanding the recurrence, one can get hi=fi−gi=Si−gi+1−max⁡(Si−fi+1,−Si−gi+1)=min⁡(fi+1−gi+1,2Si)h_i=f_i-g_i=S_i-g_{i+1}-\max(S_i-f_{i+1},-S_i-g_{i+1})=\min(f_{i+1}-g_{i+1},2S_i) and hN=fN−gN=SN−(−SN)=2SNh_N=f_N-g_N=S_N-(-S_N)=2S_N. It turns out that hih_i is exactly the doubled suffix minimum of SiS_i! Therefore one can write the explicit formula of f1f_1, since f1=S1−g2=S1−(f2−h2)=S1+h2−S2−h3+⋯=∑i=1N(−1)i−1hi+∑i=2N(−1)ihif_1=S_1-g_2=S_1-(f_{2}-h_2)=S_1+h_2-S_2-h_3+\cdots=\sum\limits_{i=1}^{N}(-1)^{i-1}h_i+\sum\limits_{i=2}^{N}(-1)^i h_i. Therefore, the final answer is ∑i≡1(mod2)Si+∑i=2N(−1)iTi\sum\limits_{i\equiv1\pmod 2}S_i+\sum\limits_{i=2}^{N}(-1)^iT_i, where TiT_i is the suffix minimum of SiS_i.

One may use std::set to maintain the indices where the suffix minimum of SiS_i changes. Since a modification only decreases the value of SiS_i, it’s sufficient to insert a new element and delete all the previous element whose former suffix minimum is less than or equal to the value of SiS_i after decreasing. Each time the set is changed, one can recalculate the answer in O(1)O(1) time. Since each element can be deleted at most once, the time complexity is O((N+Q)log⁡N)O((N+Q)\log N).

Code

C++
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
using namespace std;
set<pair<int,ll> > S;
int n,q,p;ll s;
ll a[200005];
ll b[200005];
ll ans;
inline int query(int L,int R)
{
	return (R%2==0)-(L%2==0);
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		cin>>a[i];
		if(i&1) ans+=a[i];
	}
	for(int i=n;i>=2;--i)
	{
		if(i==n) b[i]=a[i];
		else b[i]=min(b[i+1],a[i]);
		if(i==n||b[i]!=b[i+1])
		{
			S.insert(mp(i,b[i]));
		}
		if(i&1) ans-=b[i];
		else ans+=b[i];
	}
	S.insert(mp(1,0));
	cout<<ans<<'\n';cin>>q;
	while(q--)
	{
		cin>>p>>s;
		a[p]-=s;
		if(p&1) ans-=s;
		if(p>1)
		{
			set<pair<int,ll> >::iterator itr=S.lower_bound(mp(p,0));
			if(a[p]<(itr->se))
			{
				ans-=query(prev(itr)->fi,p)*((itr->se)-a[p]);
				if((itr->fi)==p)
				{
					S.erase(itr);
				}
				S.insert(mp(p,a[p]));
				int cur=p;
				while(1)
				{
					itr=S.lower_bound(mp(cur,0));
					--itr;
					if((itr->se)<=a[p]) break;
					ans-=query(prev(itr)->fi,itr->fi)*((itr->se)-a[p]);
					S.erase(itr);
					cur=itr->fi;
				}
			}
		}
		cout<<ans<<'\n';
	}
	return 0;
}
原文