- 249's solution
-
P249's Solution
- @ 2026-4-7 20:32:51
题目中的等式等价于:
$$2 \cdot \sum _{i = 1} ^x a_i = \sum _{i = 1} ^n a_i$$等式右边是定值,左边可以用一次前缀和 算出所有 值对应的计算结果,逐个判断即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 12;
long long t[N];
int main()
{
long long n, x, s = 0, a = 0;
while (cin >> n)
{
s = 0;
for (int i = 1; i <= n; i++)
{
cin >> x;
s += x;
t[i] = s;
}
a = -1;
for (int i = 0; i <= n; i++)
if (2 * t[i] == s)
{
a = i;
break;
}
cout << a << endl;
}
return 0;
}