2019牛客暑期多校训练营(第三场)G题 Removing Stones

题目描述:

Summer vacation is coming and Mark has returned home from his university having successfully survived the exam week. Today, he is very bored. So his friend Alice challenges him to play a game with stones which is invented by her. Alice gives Mark N piles of stones numbered from 1 to N, and there are ai stones in the i-th pile. The rules of the game are simple: Mark will try to remove all stones. In each move, Mark chooses two different non-empty piles and removes one stone from each of those two piles. Mark can perform any number of moves. If all the piles are empty after some number of moves, Mark wins the game. If he can’t make a valid move but not all piles are empty, he loses the game. Obviously, if the total number of stones is odd, then Mark is not able to win the game. So there is an additional rule: if initially, the total number of stones is odd, then Mark removes a single stone from the pile with the fewest stones before starting the game. If there are multiple piles with the smallest number of stones, Mark chooses one among them to remove a stone.
Mark found the optimal strategy for Alice’s game very quickly and gets bored again. Also, he noticed that for some configuration of stones there is no way to win. So he challenges you to solve this problem: count the number of integer pairs (l, r) (1≤l<r≤N) such that it is possible for Mark to win the game if the game is played using only the piles numbered from l to r.

输入描述:

The input contains multiple cases. The first line of the input contains a single positive integer T, the number of cases. The first line of each case contains a single integer N ( 2 ≤ N ≤ 300000 ) , the number of piles. The following line contains N space-separated integers, where the i-th integer denotes ai (1≤ai≤109), the number of stones in the i i-th pile. It is guaranteed that the sum of N over all cases does not exceed 1000000 1000000.

输出描述:

For each case, print a single integer on a single line, the number of pairs satisfying the required property.

解:

这是一道博弈题,题目可以转化为,求一个长度大于等于2的连续子数列,其中最大的元素不大于其它元素和的1/2。具体操作解释在代码里了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;

typedef long long ll;

const double eps = 1e-8;
const int N = 3e5+5;

ll a[N], sum[N];

int main ()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        ll n;
        scanf("%lld", &n);
        ll ans = n*(n+1)/2;
        for(int i = 1; i <= n; ++i)
        {
            scanf("%lld", &a[i]);
            sum[i] = sum[i-1]+a[i];//1、预处理和
        }
        for(int i = 1; i <= n; ++i)
        {
            ll l = i, r = i;
            while(sum[r]-sum[l-1] < 2*a[i] && l >= 1)//2、找到以i为区间右端点的不符合要求的左端点l;
                l--;
            l++;
            for(int j = l; j <= i; ++j)
            {
                while(sum[r]-sum[j-1] < 2*a[i] && r <= n)//3、找到以j-1为左端点的不符合要求的右端点。
                    r++;
                r--;
                ans -= r-i+1;//4、ans减去区间长度,相当于减去了长度个区间。
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值