codeforces 466C. Number of Ways 解题报告

题目链接:http://codeforces.com/problemset/problem/466/C

题目意思:给出一个 n 个数的序列你,问通过将序列分成三段,使得每段的和都相等的分法有多少种。

    这个是详细的题解:

    http://codeforces.com/blog/entry/13758

    

    代码也是按这个思路来做的,可怜我的做法改了 N 次总是得不到正确的结果~~泪

    

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 typedef __int64 ll;
 8 
 9 const int maxn = 5e5 + 5;
10 ll a[maxn], sum[maxn];
11 ll cnt[maxn];
12 
13 int main()
14 {
15     int n;
16     while (scanf("%d", &n) != EOF)
17     {
18         memset(sum, 0, sizeof(sum));
19         memset(cnt, 0, sizeof(cnt));
20 
21         for (int i = 1; i <= n; i++)
22         {
23             scanf("%I64d", &a[i]);
24             sum[i] = sum[i-1] + a[i];
25         }
26         ll ans = 0;
27         if (sum[n] % 3 == 0)   
28         {
29             ll avg = sum[n] / 3;
30 
31             for (int i = n-1; i; i--)
32             {
33                 cnt[i] = cnt[i+1];    // 不断往前传递
34                 if (sum[i] == avg)
35                     ans += cnt[i];
36                 if (sum[i] == avg * 2) // 可以分成三段的标志
37                     cnt[i]++;
38             }
39         }
40         printf("%I64d
", ans);
41     }
42     return 0;
43 }
View Code
原文地址:https://www.cnblogs.com/windysai/p/3974555.html