NC51450. Removing Stones
描述
输入描述
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 ofover 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(); }