AT_dwango2015_finals_4 コインの取り合い Solution

· · 题解

Analysis

Consider the case where Q=0 first. Since after the i-th turn, the states of the coins 1,2,\cdots,i are determined, and the states of the coins i+1,i+2,\cdots,N are unchanged, one can easily come up with the following dynamic programming solution: let f_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,\cdots,N, where the state of the coin i is head/tail. The initial value of the dp is f_N=S_N,g_N=-S_N, and the transition formula is 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 f_1 and the sum of S, which yields a solution with time complexity O(NQ).

To solve the problem, one must find f_1 in a faster way. Consider h_if_i-g_i. Expanding the recurrence, one can get 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 h_N=f_N-g_N=S_N-(-S_N)=2S_N. It turns out that h_i is exactly the doubled suffix minimum of S_i! Therefore one can write the explicit formula of f_1, since f_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 \sum\limits_{i\equiv1\pmod 2}S_i+\sum\limits_{i=2}^{N}(-1)^iT_i, where T_i is the suffix minimum of S_i.

One may use std::set to maintain the indices where the suffix minimum of S_i changes. Since a modification only decreases the value of S_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 S_i after decreasing. Each time the set is changed, one can recalculate the answer in O(1) time. Since each element can be deleted at most once, the time complexity is O((N+Q)\log N).

Code

#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;
}