数组划分
数组划分
https://ac.nowcoder.com/acm/problem/246912
注意两点 1 小心数组不能被3整除 2.即使cnt没有更新 也要将其赋进lcnt数组
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef long long LL;
LL a[N];
LL s[N];
LL n;
LL ans;
LL lcnt[N];
LL cnt;
int main(){
cin >> n;
if(n <= 2){
cout << "0";
return 0;
}
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
LL l = s[n] / 3;
LL r = s[n] / 3 * 2;
for(int i = 1 ; i <= n - 2 ; i++){
if(s[i] == l){
cnt++;
}
lcnt[i] = cnt;
}
for(int i = 2 ; i <= n - 1 ; i++){
if(s[i] == r){
ans += lcnt[i - 1];
}
}
if(s[n] % 3 != 0)cout << "0";
else cout << ans;
return 0;
}