列表

详情


NC51450. 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 piles of stones numbered from to, and there are a_i stones in the-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 such that it is possible for Mark to win the game if the game is played using only the piles numbered from to.

输入描述

The input contains multiple cases. The first line of the input contains a single positive integer, the number of cases.
The first line of each case contains a single integer , the number of piles. The following line contains space-separated integers, where the-th integer denotes , the number of stones in the-th pile.
It is guaranteed that the sum of over all cases does not exceed.

输出描述

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

示例1

输入:

2
3
1 1 1
4
1 2 3 4

输出:

3
3

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 197ms, 内存消耗: 2816K, 提交时间: 2019-08-17 19:38:35

#include<bits/stdc++.h>
using namespace std;
const int M=3e5+5;
typedef long long ll;
ll a[M];
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%lld",&a[i]);
		ll ans=1ll*n*(n+1)/2ll;
		for(int i=1;i<=n;i++){
			int l=i,r=i;
			ll sum=0;
			while(l>0&&sum<a[i])
				sum+=a[--l];
			while(l<i){
				sum-=a[l++];
				while(r<=n&&sum<a[i])
					sum+=a[++r];
				ans-=r-i;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 185ms, 内存消耗: 1644K, 提交时间: 2019-07-25 23:42:24

#include<bits/stdc++.h>
#define rep(i,x,y) for(auto i=(x);i<=(y);++i)
using namespace std;
typedef long long ll;
int a[300010];
void sol(){int n;ll ans=0;
	scanf("%d",&n);
	rep(i,1,n)scanf("%d",&a[i]);
	rep(i,1,n){
		int l=i,r=i;ll nw=0;
		while(l&&nw<a[i])nw+=a[--l];
		while(l<i){nw-=a[l++];
			while(r<=n&&nw<a[i])nw+=a[++r];
			ans+=r-i;
		}
	}
	printf("%lld\n",1ll*n*(n+1)/2-ans);
}
int main(){int t;
	scanf("%d",&t);
	rep(i,1,t)sol();
}

上一题