Codeforce 466C. Number of Ways (前缀+思维)

题目链接

题意:

给出你一个序列让你三等分,求一共多少种分法.

思路:

首先判断和是否能整除3,然后维护一个前缀和,之后三等分,两个断点就是1倍的sum/3,和2倍的sum/3,找出这两个断点.

#include <set>
#include <map>
#include <queue>
#include <string>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<ll, ll> pii;
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
const int maxn = 5e5 + 1010;
#define inf 0x3f3f3f3f
#define sf scanf
#define pf printf
const int mod = 998244353;
const int MOD = 10007;

inline int read()
{
    
    
    int x = 0;
    bool t = false;
    char ch = getchar();
    while((ch < '0' || ch > '9') && ch != '-')ch = getchar();
    if(ch == '-')t = true, ch = getchar();
    while(ch <= '9' && ch >= '0')x = x * 10 + ch - 48, ch = getchar();
    return t ? -x : x;
}

/*
vector<ll> m1;
vector<ll> m2;
priority_queue<ll , vector<ll> , greater<ll> > mn;//上  小根堆 		小到大
priority_queue<ll , vector<ll> , less<ll> > mx;//下   	大根堆  	大到小
*/
map<ll, ll>mp;
map<ll, ll>mpp;
multiset<ll> s, t;
vector<ll> ans;
ll n, m, u, d, r, l;
ll a[maxn], b[maxn];


#define read read()
int main()
{
    
    
    cin >> n;
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++)
    {
    
    
        b[i] = a[i] + b[i - 1];
    }

    if(b[n] % 3) puts("0");
    else
    {
    
    
        ll ans = b[n] / 3;
        ll num = 0, num1 = 0, num2 = 0;
        for(int i = 1; i < n; i++)
        {
    
    
        	
            if(ans * 2 == b[i])
            {
    
    
                num1 = num1 + num;
            }

            if(ans == b[i])
            {
    
    
                num++;
            }

        }
        cout << num1 << endl;
    }
    return 0;

}

猜你喜欢

转载自blog.csdn.net/weixin_45911397/article/details/114883571
今日推荐