@toc
问题描述
给定一个长度为 n
的数列 a1,a2,…,an
,每次可以选择一个区间 [l,r]
,使下标在这个区间内的数都加一或者都减一。
求至少需要多少次操作才能使数列中的所有数都一样,并求出在保证最少次数的前提下,最终得到的数列可能有多少种。
格式输入
第一行包含整数 n
。
第二行包含 n
个整数 a1,a2,…,an
。
格式输出
输出一个整数,表示截断方法数量。
样例输入
5
1 2 3 4 5
样例输出
0
评测用例规模与约定
前六个测试点满足 1≤n≤10
。
所有测试点满足 1≤n≤105
,−10000≤ai≤10000
。
参考程序
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long o;
const int N = 100010;
int n, m;
int s[N];
int main()
{
scanf("%d",&m);
while (m--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
s[i] = s[i - 1] + x;
}
if (s[n] % 3) puts("0");
else
{
o res = 0, cnt = 0;
for (int j = 2; j < n; j++)
{
if (s[j - 1] == s[n] / 3) cnt++;
if (s[j] == s[n] / 3 * 2) res += cnt;
}
printf("%lld", res);
}
}
return 0;
}
以个人刷题整理为目的,如若侵权,请联系删除~