【PA2010】【BZOJ3493】Termites

Description

有两个玩家,会去挑选一些数字,每次只能选至少与一个0相邻的数字(不能选0),拿完一个数字后这个数字 会变成0。
两个玩家都采取最优策略,使自己拿到的数字的和最大,问最后两位玩家的得分。
数字个数< 1000000,保证初始时至少有一个0。

Input

Output

Sample Input

8

1 2 0 3 7 4 0 9

Sample Output

17 9
HINT

Explanation of the example: The fence consisted of 8 planks, out of which 2 have already been eaten. The first termite in his first turn can choose between planks of heights 2, 3, 4 and 9. During the optimal game, in the consecutive turns, termites will eat planks of heights 9, 2, 1, 4, 7 and 3.

Source

我是什么时候拿zky的号做过了这个题..自己都忘了..
而且当时竟然没写题解…
考虑从整个数列的两端取啊取,相邻的数肯定分别属于两个人的.
记录这个差值.
在中间的数,则考虑把某个数取出来之后,把他和两边数对总差值的贡献累积一下.
比较像集训队2011的种树.

链表模拟删除.

过去了这么久对这个题只记得这么多了.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1000100
#define LL long long
#define GET (ch>='0'&&ch<='9')
using namespace std;
int n,cnt,h,t,top;
LL sum,delta;
LL sta[MAXN],a[MAXN];
int b[MAXN],nxt[MAXN],pre[MAXN];
inline void in(LL &x)
{
    char ch=getchar();x=0;
    while (!GET)    ch=getchar();
    while (GET) x=x*10+ch-'0',ch=getchar();
}
inline void del(int x)  {   b[x]=1;pre[nxt[x]]=pre[x];nxt[pre[x]]=nxt[x];   }
inline bool cmp(LL a,LL b)  {   return a>b; }
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)  in(a[i]),b[i]=!a[i],sum+=a[i],nxt[i]=i+1,pre[i]=i-1,cnt+=!b[i];
    nxt[0]=1;pre[n+1]=n;h=1;t=n;bool flag=1;
    while (flag&&h<t)
    {
        flag=0;
        if (!b[h]&&!b[nxt[h]]&&a[h]>=a[nxt[h]])
        {
            delta+=(cnt&1)?a[h]-a[nxt[h]]:a[nxt[h]]-a[h];
            int tmp=nxt[nxt[h]];
            del(nxt[h]);del(h);h=tmp;flag=1;
        }
        if (!b[t]&&!b[pre[t]]&&a[t]>=a[pre[t]])
        {
            delta+=(cnt&1)?a[t]-a[pre[t]]:a[pre[t]]-a[t];
            int tmp=pre[pre[t]];
            del(pre[t]);del(t);t=tmp;flag=1;
        }
        for (int i=h;i<=t;++i)
            if (!b[i]&&pre[i]>0&&nxt[i]<=n&&!b[pre[i]]&&!b[nxt[i]]&&a[i]>=a[pre[i]]&&a[i]>=a[nxt[i]])
            {
                flag=1;a[i]=a[pre[i]]+a[nxt[i]]-a[i];
                if (pre[i]==h)  h=i;
                if (nxt[i]==t)  t=i;
                del(pre[i]);del(nxt[i]);
            }
    }
    for (int i=1;i<=n;i++)  if (!b[i])  sta[++top]=a[i];
    sort(sta+1,sta+top+1,cmp);
    for (int i=1;i<=top;i++)    delta+=(i&1)?sta[i]:-sta[i];
    cout<< (sum+delta)/2 <<' '<< (sum-delta)/2 <<endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值