列表

详情


NC222482. [USACODec2020B]DaisyChains

描述

Every day, as part of her walk around the farm, Bessie the cow visits her favorite pasture, which has N flowers (all colorful daisies) labeled 1…N lined up in a row (1≤N≤100). Flower i has pi petals (1≤pi≤1000).
As a budding photographer, Bessie decides to take several photos of these flowers. In particular, for every pair of flowers (i,j) satisfying 1≤i≤j≤N, Bessie takes a photo of all flowers from flower i to flower j (including i and j).

Bessie later looks at these photos and notices that some of these photos have an "average flower" -- a flower that has P petals, where P is the exact average number of petals among all flowers in the photo.

How many of Bessie's photos have an average flower?

输入描述

The first line of input contains N. The second line contains N space-separated integers p1…pN.

输出描述

Please print out the number of photos that have an average flower.

示例1

输入:

4
1 1 2 3

输出:

6

说明:

Every picture containing just a single flower contributes to the count (there are four of these in the example). Also, the (i,j) ranges (1,2) and (2,4) in this example correspond to pictures that have an average flower.

Problem credits: Nick Wu

原站题解

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

C++ 解法, 执行用时: 3ms, 内存消耗: 524K, 提交时间: 2021-12-04 10:56:38

#include <bits/stdc++.h>
using namespace std;
const int maxn = 110,maxp = 1e3 + 10;
int n,sum,ans;
int a[maxn],num[maxp];
void solve(){
	for(int i = 1;i <= n;i++){
		sum = 0;
		memset(num,0,sizeof(num));
		for(int j = i;j <= n;j++){
			sum += a[j];
			num[a[j]]++;
			if(sum % (j - i + 1) == 0 && num[sum / (j - i + 1)]) ans++;
		}
	}
}
int main(){
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
	solve();
	printf("%d",ans);
	return 0;
}

上一题