当前位置:网站首页>[code source] daily one question three paragraph
[code source] daily one question three paragraph
2022-07-25 09:37:00 【self_ disc】
2022.05.08
Topic link : Three paragraph - subject - Daimayuan Online Judge
Title Description
There is a length of n Sequence , Now we want to cut it into three sections ( Every paragraph is continuous ), Make the sum of the elements of each paragraph the same , How many different cutting methods are there
Input description
The first line gives a number nn,(1≤n≤10^5)
The second line gives the sequence a1,a2,a3,...,an,(|ai|≤10^5)
Output description
Output a number to indicate how many different cutting methods there are
The sample input
4
1 2 3 3Sample output
1Sample explanation
It can be divided into the first group 1,2, The second group 3, The third group 3
analysis :
The title requires that an array be divided into three consecutive segments and the same as , It is to find two different positions in the array to make the values of the three parts equal ,O(n^2) The method of direct violence enumeration is very simple , Obviously it's going to be overtime , So how to optimize ? use num1 Indicates the sum of the first paragraph ,num2 Indicates the sum of the second paragraph .num1 and num2 The size of is certain , Every num2 It can be compared with all the previous num1 Can meet the conditions , So you only need to record how many are in front of the current position num1 The position of .
See the code for details. :
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
#define int long long
int n, a[100009];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
a[i] = a[i - 1] + x; // Preprocessing prefixes and
}
int cnt = 0, res = 0;
if (a[n] % 3 || n <= 2) // Sum is not 3 The multiple or array size of is less than 3 It is impossible to meet the conditions , Output 0
{
cout << 0;
return;
}
int num1 = a[n] / 3, num2 = num1 * 2;
for (int i = 1; i < n; i++)
{
if (a[i] == num2)
{
res += cnt; // Add current num1 The number of , namely num2 The front is num1 The number of
}
if (a[i] == num1)
{
cnt++;
}
}
cout << res;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
}边栏推荐
- How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
- @4-1 CCF 2020-06-1 线性分类器
- 【代码源】每日一题 算的我头都大啦
- ~5 ccf 2021-12-2 序列查询新解
- OC--继承和多态and指针
- OC--类别 扩展 协议与委托
- ¥1-1 SWUST oj 941: 有序顺序表的合并操作的实现
- Database operation language (DML)
- 关于C和OC
- SurfaceView 闪屏(黑一下问题)
猜你喜欢
随机推荐
如何将Jar包部署到服务器,注:启动命令有无nohup有很大关系
关于C和OC
【代码源】每日一题 添加括号
2022年的个人技术选型梳理
Data preprocessing
¥1-3 SWUST oj 942: 逆置顺序表
@1-1 CCF 2021-04-1 灰度直方图
Redis string 结构命令
@3-2 CCF 2020-12-2 期末预测之最佳阈值
The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do
MongoDB数据库文件的读与写
OC -- Foundation -- array
~1 ccf 2022-06-2 寻宝!大冒险!
Swagger2 shows that there is a problem with the get interface, which can be solved with annotations
Redis set 结构命令
laravel 调用第三方 发送邮件 (php)
类(2) 和 协议
~4.1 剑指 Offer 05. 替换空格
【cf】Round 128 C. Binary String
OC--Foundation--字典









