璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:another_array_problem
problems
名称Another Array Problem
题目编号840C
题目链接codeforces.com/…
来源CodeForces
算法分类思维, 构造, 贪心
难易程度看懂题解

Another Array Problem

想法

突破点在于如果重复做操作会将整个区间清零,那如果0和一个最大值放在一起那这个区间就全为最大值。所以从这入手,接下来就是分类讨论,n为2,n为3的时候需要考虑最大值的位置,然后进行一个类比。

代码实现

#include<cstdio>
#include<algorithm>
 
const int N = 2*100010;
int n;
long long int line[N];
 
int main()
{
	int T;
	scanf("%d", &T);
	for (int t = 0; t < T; ++t) {
		scanf("%d", &n);
		for(int i=1; i<=n; ++i)
			scanf("%lld", &line[i]);
 
		if(n == 2)
			printf("%lld\n", std::max(line[1]+line[2], abs(line[1]-line[2])*2));
		else if(n == 3)
			printf("%lld\n", std::max( { line[1] * 3, line[3] * 3, (line[2] - line[1]) * 3, (line[2] - line[3]) * 3, line[1] + line[2] + line[3]}));
		else
		{
			long long int mx = 0;
			for (int i = 1; i <= n; ++i) {
				mx = std::max(line[i], mx);
			}
			printf("%lld\n", mx * n);
		}
	}
 
	return 0;
}
/app/www/public/data/pages/icpc/problems/another_array_problem.txt · 最后更改: 2024/01/20 06:30 由 温婕莺