Number of Ways

gRPC之gRPC开发流程与入门案例 阅读详情


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.

Sample test(s)
input
5
1 2 3 0 3
output
2
input
4
0 1 -1 0
output
1
input
2
4 1
output
0

把一个数列分成3个和相等的部分,有多少个分法。首先判断是否能被3整除,如果不被3整除就没得玩了。 

思路1:

每部分的和是pre,先找等于pre的位置,然后是等于2pre的位置(位置<n),然后对第一个位置遍历,找第二个中位置大于第一种位置的个数,累加得答案,结果超时……

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
long long a[500005];
int dp1[500005], dp2[500005];
int main()
{
	int i, j, m, n, temp1, temp2;
	long long ans, pre, temp;
	cin >> n; ans = 0;
	for (i = 1; i <= n; i++)
	{
		cin >> a[i];
		ans += a[i];
	}
	if (ans % 3 != 0)
		cout << "0" << endl;
	else
	{
		temp1 = temp2 = 1;
		pre = ans / 3;
		ans = 0;
		for (i = 1; i <= n; i++)
		{
			ans = ans + a[i];
			if (ans == pre&&i <= n - 2)
				dp1[temp1++] = i;
			if (ans == 2 * pre&&i <= n - 1)
				dp2[temp2++] = i;
		}
		temp1--; temp2--;
		temp = 0;
		for (i = 1; i <= temp1; i++)
		{
			for (j = 1; j <= temp2; j++)
			{
				if (dp2[j] > dp1[i])
				{
					temp = temp + temp2 - j + 1;
					break;
				}
			}
		}
		cout << temp << endl;
	}
	return 0;
}

思路2:

 如果位置i符合和为pre,那么就是从i+2之后符合pre的第三组值的个数,只要第三组满足pre,第一组也满足pre,那么第二组一定是确定的而且和为pre。类似于动规了。从后向前找,这个从这个位置开始,符合和为pre的第三组的个数。

if(a[i]==pre)dp[i]=dp[i+1]+1;

else dp[i]=dp[i+1];

然后再从前向后找和为pre的第一组数,ans+=dp[i+2];至少留一个数给第二组。AC了。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
long long a[500005];
int dp[500005];
int main()
{
	int i, j, m, n, temp1, temp2;
	long long ans, pre, temp;
	cin >> n; ans = 0;
	for (i = 1; i <= n; i++)
	{
		scanf("%I64d", &a[i]);
		ans += a[i];
	}
	if (ans % 3 != 0)
		cout << "0" << endl;
	else
	{
		temp1 = temp2 = 1;
		pre = ans / 3;
		ans = a[n]; 
		if (a[n] == pre)dp[n] = 1;
		else dp[n] = 0;
		for (i = n - 1; i > 2; i--)
		{
			ans += a[i];
			if (ans == pre)
				dp[i] = dp[i + 1] + 1;
			else
				dp[i] = dp[i + 1];
		}
		ans = 0; temp = 0;
		for (i = 1; i <= n - 2; i++)
		{
			ans = ans + a[i];
			if (ans == pre)
				temp = temp + dp[i + 2];
		}
		cout << temp << "\n";
	}
	return 0;
}



思路3:

在提交状态里,看见一个居然只有47ms的。

#include<bits/stdc++.h>
#define pf printf
#define sf scanf
#define ll  long long
using namespace std;

template<class T>
inline bool fs(T &x)
{
    int c=getchar();
    int sgn=1;
    while(~c&&c<'0'||c>'9')
    {
        if(c=='-')sgn=-1;
        c=getchar();
    }
    for(x=0; ~c&&'0'<=c&&c<='9'; c=getchar())
        x=x*10+c-'0';
    x*=sgn;
    return ~c;
}
int ara[500009],n;
ll x,y,a,b,c;
int main()
{
    int i,j;
    fs(n);
    a=b=0;
    x=y=0;
    for(i=0; i<n; i++)
    {
        fs(ara[i]);
        x+=ara[i];
    }
    for(i=0; i<n-1; i++)
    {
        y+=ara[i];
        if(2*x==3*y)
            b+=a;
        if(3*y==x)
            a++;
    }
    pf("%lld\n",b);
    return 0;
}


其代码神奇之处在于一次遍历搞定两次遍历的活。如果找到了第一组满足pre,那么a++,如果找到了第二组也符合的,那么结果加上a。最后输出结果。这是另一种遍历方式。

然而他没有对3整除进行剪枝,加入这个判断后可以达到31ms,简直神奇。


泛微ecology9二次开发-附件上传接口实战:从表单创建到数据库存储解析 本文详细解析了泛微ecology9二次开发中的附件上传接口实战,从测试环境搭建、数据库表结构解析到接口代码实现,提供了全面的开发指南。重点介绍了文件上传的核心逻辑、常见问题排查及高级应用场景,帮助开发者高效实现附件上传功能并优化系统性能。 阅读详情

相关推荐

基于Simulink的二相混合式步进电机驱动器快速建模与仿真实践

本文详细介绍了如何利用Simulink对二相混合式步进电机驱动器进行快速建模与仿真。通过从零搭建电机数学模型、设计H桥控制逻辑,并集成步进时序,手把手指导读者创建可运行的仿真模型文件。该方法能有效验证控制策略,避免硬件调试风险,是学习与实践电机驱动开发的理想途径。

weixin_29171087的博客 147

B - Number of Ways CodeForces - 466C

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 i...

云乐 356

最优化问题——一维搜索(一)

最优化问题——一维搜索 1. 一维搜索的引入 1.1 从非线性规划问题到一维搜索 在前面的文章中,我们提到了基于迭代的非线性规划问题的求解思路。在求解过程中,我们提到了在每一步的迭代中都需要对计算步长因子,其计算公式为: λk=minλf(xk+λp)λ_k=min_{λ}f(x_k+λp)λk​=minλ​f(xk​+λp) 我们将f(xk+λp)f(x_k+λp)f(xk​+λp)视为λ的一元函数,我们的目标就是求这个一元函数的极小值。对于这个问题,我通常的求解方式为一维搜索。 1.2 一维搜索的基本形

hei653779919的博客 7358

Codeforces Round #266 (Div. 2)C(算是简单计数吧)

C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You've got array a[1], a[2], ..., a[n], c

cq_phqg 938

【WILEY】wiley期刊投稿详解:从revision到Accepted全流程实例讲解

注意:包括没标注修改位置的word文件和标注了修改位置的word文件。

Everyday try to become better -Kobe Bryant 1万+

Yolov5s可视化网络结构图

Yolov5共有四种网络结构,每种网络深度和宽度上都不相同。 (1) Yolov5s可视化网络结构图:点击查看 (2) Yolov5m可视化网络结构图:点击查看 (3) Yolov5l可视化网络结构图:点击查看 (4) Yolov5x可视化网络结构图:点击查看 ...

江南研习社 15万+

B - Number of Ways

B - Number of Ways

Yuric的专栏 507

MTK6769平台Type-C充电全解析:从硬件管脚到PD协议实战(Android15环境)

本文深入解析了MTK6769平台在Android 15环境下Type-C充电的完整链路。从硬件管脚(特别是核心的CC引脚)功能与连接,到USB-PD协议报文抓取与实战分析,并结合MTK平台PD芯片驱动与充电IC驱动的软件框架,提供了从硬件设计到软件调试的系统性解决方案和常见问题排查案例。

ol78901234的博客 309

CodeForces - 438D The Child and Sequence (线段树)

The Child and Sequence   time limit per test:4 seconds memory limit per test:256 megabytes input:standard input output:standard output At the children's day, the child came to Picks's house, and...

GYH0730的博客 250

用PyCharm+Scrapy打造你的第一个爬虫:从安装到实战(附完整代码)

本文详细介绍了如何使用PyCharm和Scrapy框架从零开始构建高效网络爬虫。通过环境配置、项目创建、核心爬虫逻辑编写到数据处理与存储的完整流程,帮助Python开发者快速掌握爬虫开发技巧,并附有实战代码示例。特别适合想要学习Scrapy框架和提升爬虫技能的开发者。

weixin_29038345的博客 867

Spacedesk保姆级教程:用旧平板给Windows电脑当副屏,从安装到避坑一步到位

本文提供Spacedesk保姆级教程,教你如何将旧平板变成Windows电脑的副屏,从软件安装到网络配置、显示优化及声音交互,一步步避开常见陷阱。通过WiFi实现扩展屏功能,支持4K分辨率和60Hz刷新率,让闲置设备焕发新生。

weixin_42524945的博客 1万+

CF 279C Ladder(区间问题)

You've got an array, consisting of n integers a1, a2, ..., an. Also, you've got m queries, the i-th query is described by two integers li, ri. Numbers li, ri define a subsegment of the original ar

blaze_jack 469

【linux】解决报错:Network error: Connection refused

【linux】解决报错:Network error: Connection refused

weixin_44502754的博客 4936

Leetcode 1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers

文章作者:Tyan 博客:noahsnail.com  |  优快云  |  简书 1. Description 2. Solution 解析: Version 1,分别计算两个数组的平方和以及所有组合乘积并统计对应值的个数,遍历每个数组平方和的个数,找到另一个数组对应的积的个数,二者相乘,加到三元组总个数中。Version 2进行进一步优化。 Version 1 class Solution: def numTriplets(self, nums1:

SnailTyan 264

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

CF466C Number of Ways (前缀和)

这道题不难发现,可以利用前缀和更快捷的划分区间求和,将数组划分为3份,说明需要找到两个断点,在第一个断点前面的数字的和为所有数字加起来的sum的三分之一,而从索引1到第二个断点之间的数字之和则是sum的三分之二,而当sum无法被3整除时候一定无解,所以在前面可以对sum先进行一个特判,同时要对两个断点设定好满足条件,上题解。

2403_87530627的博客 481

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

C. Number of Ways

链接:https://codeforces.com/contest/466/problem/C You've got arraya[1], a[2], ..., a[n], consisting ofnintegers. Count the number of ways to split all the elements of the array into three contiguous...

Luoriliming的博客 259

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Number Of Ways To Make Change

Prompt Given an array of positive integers representing coin denominations and a single non-negative integer n representing a target amount of money, write a function that returns the number of ways to make change for that target amount using the given coi

tianye00011的博客 352
上一篇: Two Buttons
下一篇: ZgukistringZ
Techay
博客等级 码龄11年 11粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值