Day 36 C. Number of Ways

这篇文章探讨了如何计算给定数组如何分割成三个部分,使得每个部分的元素之和相等。通过前缀和和双指针技巧,博主展示了如何找出符合条件的分割方式数量。输入是一个整数数组,输出是满足条件的划分方案数。

Problem
You’ve got array a[1], a[2], …, a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same.

More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that .

Input
The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1], a[2], …, a[n] (|a[i]| ≤  109) — the elements of array a.

Output
Print a single integer — the number of ways to split the array into three parts with the same sum.

Examples
input
5
1 2 3 0 3
output
2

input
4
0 1 -1 0
output
1

input
2
4 1
output
0

题目大致意思为:给你一串数组 在不改变数组顺序情况下 将数组的总和分成平等的三份

#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<stdio.h>
#include<limits.h>
#include<cmath>
#include<set>
#include<map>
#define ll long long
using namespace std;
ll s[500005];
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        s[i + 1] = s[i] + a;//用前缀和记录每组总和 
    }
    ll ans = 0;
    if (s[n] % 3 == 0)//如果数组不能够被三整除 直接输出0组  
    {
        ll u = s[n] / 3;//一份数组和
        ll v = 2 * s[n] / 3;
        ll cnt = 0;
        for (int i = 1; i < n; i++) 
        {
            if (s[i] == v)
            {
                ans += cnt;
            }
            if (s[i] == u) 
            {
                cnt++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
Write a computer program that could be used to track users' activities. Lab Number Computer Station Numbers 1 1-3 2 1-4 3 1-5 4 1-6  You run four computer labs. Each lab contains computer stations that are numbered as the above table.  There are two types of users: student and staff. Each user has a unique ID number. The student ID starts with three characters (for example, SWE or DMT), and is followed by three digits (like, 001). The staff ID only contains digits (for example: 2023007).  Whenever a user logs in, the user’s ID, lab number, the computer station number and login date are transmitted to your system. For example, if user SWE001 logs into station 2 in lab 3 in 01 Dec, 2022, then your system receives (+ SWE001 2 3 1/12/2022) as input data. Similarly, when a user SWE001 logs off in 01 Jan, 2023, then your system receives receives (- SWE001 1/1/ 2023). Please use = for end of input.  When a user logs in or logs off successfully, then display the status of stations in labs. When a user logs off a station successfully, display student id of the user, and the number of days he/she logged into the station.  When a user logs off, we calculate the price for PC use. For student, we charge 0 RMB if the number of days is not greater than 14, and 1 RMB per day for the part over 14 days. For staff, we charge 2 RMB per day if the number of days is not greater than 30, and 4 RMB per day for the part over 30 days.  If a user who is already logged into a computer attempts to log into a second computer, display "invalid login". If a user attempts to log into a computer which is already occupied, display "invalid login". If a user who is not included in the database attempts to log off, display "invalid logoff".
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值