CF 1899B 学习笔记

B. 250 Thousand Tons of TNT

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Alex is participating in the filming of another video of BrMeast, and BrMeast asked Alex to prepare 250 thousand tons of TNT, but Alex didn't hear him well, so he prepared 𝑛� boxes and arranged them in a row waiting for trucks. The 𝑖�-th box from the left weighs 𝑎𝑖�� tons.

All trucks that Alex is going to use hold the same number of boxes, denoted by 𝑘�. Loading happens the following way:

  • The first 𝑘� boxes goes to the first truck,
  • The second 𝑘� boxes goes to the second truck,
  • ⋯⋯
  • The last 𝑘� boxes goes to the 𝑛𝑘��-th truck.

Upon loading is completed, each truck must have exactly 𝑘� boxes. In other words, if at some point it is not possible to load exactly 𝑘� boxes into the truck, then the loading option with that 𝑘� is not possible.

Alex hates justice, so he wants the maximum absolute difference between the total weights of two trucks to be as great as possible. If there is only one truck, this value is 00.

Alex has quite a lot of connections, so for every 1≤𝑘≤𝑛1≤�≤�, he can find a company such that each of its trucks can hold exactly 𝑘� boxes. Print the maximum absolute difference between the total weights of any two trucks.

Input

The first line contains one integer 𝑡� (1≤𝑡≤1041≤�≤104) --- the number of test cases.

The first line of each test case contains one integer 𝑛� (1≤𝑛≤1500001≤�≤150000) --- the number of boxes.

The second line contains 𝑛� integers 𝑎1,𝑎2,...,𝑎𝑛�1,�2,...,�� (1≤𝑎𝑖≤1091≤��≤109) --- the weights of the boxes.

It is guaranteed that the sum of 𝑛� for all test cases does not exceed 150000150000.

Output

For each test case, print a single integer --- the answer to the problem.

Example

input

Copy

复制代码

5

2

1 2

6

10 2 3 6 1 3

4

1000000000 1000000000 1000000000 1000000000

15

60978 82265 78961 56708 39846 31071 4913 4769 29092 91348 64119 72421 98405 222 14294

8

19957 69913 37531 96991 57838 21008 14207 19198

output

Copy

复制代码
1
9
0
189114
112141

Note

In the first case, we should pick two trucks, so the first one will have only the first box, and the second one will have only the second box.

In the second case, we should pick six trucks, so the maximum will be 1010, the minimum will be 11, and the answer is 10−1=910−1=9.

In the third case, for any possible 𝑘�, the trucks will have the same total weight of boxes, so the answer is 00.

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

const int N=150000+10;
int a[N];
typedef long long LL;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;i++)	scanf("%d",&a[i]);
		LL ans=-1;
		for(int d=1;d<=n;d++)
		{
			LL mx=-1e18,mn=1e18;
			if(n%d==0)
			{
				for(int i=0;i<n;i+=d)
				{
					LL sum=0;
					for(int j=i;j<i+d;j++)
					{
						sum+=a[j];
					}
					mx=max(mx,sum);
					mn=min(mn,sum);
				}
			}
			ans=max(ans,mx-mn);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

赛时想到了这个做法,但是代码能力不够,实现不了自己的思路。我的思路是,枚举k,找到最大的差值,但是我们怎么求出一个数组中第一个k箱炸药,第二个k箱炸药,第三个k箱炸药表示出来。

题目的意思是这样的:有很多箱炸药,n箱炸药,每一箱的炸药的重量是ai,把n箱炸药装到若干车辆上,每一辆车上装k箱炸药,要求是找到使得某两辆车装的炸药总质量差值最大的k ,输出这个最大的差值

首先读入炸药的箱数n和每一箱的重量ai,然后枚举k(代码里面用d表示的),只要n%d==0(表示的是每一辆车刚好装载k箱炸药),就可以进行一次判断

把a数组分成n/k个区间(因为n可以整除k),表示出每一个区间的起点

cpp 复制代码
for(int i=0;i<n;i+=d)

从这个起点开始数k箱炸药求和,求出每一辆车上的炸药重量,然后更新最大值和最小值,最大值和最小值的差值表示答案,更新答案,找到最大的答案即可

cpp 复制代码
            LL mx=-1e18,mn=1e18;
			if(n%d==0)
			{
				for(int i=0;i<n;i+=d)
				{
					LL sum=0;
					for(int j=i;j<i+d;j++)
					{
						sum+=a[j];
					}
					mx=max(mx,sum);
					mn=min(mn,sum);
				}
			}
			ans=max(ans,mx-mn);

注意一下初始化,每换一次区间,sum都要初始化为0,最大值和最小值都要维护,每一次换k值,答案都要维护

相关推荐
森林古猿17 分钟前
再论斜率优化
c++·学习·算法
心中有国也有家29 分钟前
鸿蒙Flutter开发环境从零搭建教程(Windows/macOS双平台·避坑版)
学习·flutter·华为·harmonyos
十月的皮皮2 小时前
C语言学习笔记20260706-栈的压入、弹出序列验证
c语言·笔记·学习
心中有国也有家3 小时前
Flutter 鸿蒙适配第一步:从 hive 迁移到 hive\_ce
hive·学习·flutter·华为·harmonyos
渣渣灰飞3 小时前
MySQL 系统学习 第二阶段 第三章 DQL(Data Query Language)第二节:WHERE 条件查询
sql·学习·mysql
维他奶糖613 小时前
基于 DrissionPage 实现小红书搜索笔记批量采集
笔记
心中有国也有家4 小时前
AtomGit Flutter 鸿蒙客户端:E-Brufen 架构设计
学习·flutter·华为·harmonyos
hssfscv4 小时前
QT的学习笔记4——QMainWindow、资源文件以及主要控件的介绍
笔记·qt·学习
克里斯蒂亚诺更新4 小时前
电脑关闭密码的操作
笔记
小小的代码里面挖呀挖呀挖4 小时前
杰理蓝牙耳机开发--LE Audio与游戏耳机应用
笔记·单片机·物联网·学习