跳到正文

个人记录

原来是这样解决的吗,太厉害啦

Another_Tomorrow约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

CF1763C

瞪眼,发现执行两次 (l,r)(l,r) 后,al...ra_{l...r} 都变成 00。于是当 aa 中最大值的下标不为 22 和 n−1n-1 时,设最大值下标为 vv,则依次执行 (1,v−1),(1,v−1),(v+1,n),(v+1,n),(1,v),(v,n)(1,v-1),(1,v-1),(v+1,n),(v+1,n),(1,v),(v,n),不满足值域的就跳过。最终答案为 n×avn\times a_v,显然不会更大。

当 n≥4n\ge 4 时,即使 aa 中最大值的下标不为 22 和 n−1n-1,我们也可以通过一系列操作转成上述情形:当 v=2v=2 时,执行 (3,n),(3,n),(2,n)(3,n),(3,n),(2,n) 即可转为 v=nv=n;当 v=n−1v=n-1 时,执行 (1,n−2),(1,n−2),(1,n−1)(1,n-2),(1,n-2),(1,n-1) 即可转为 v=1v=1。

剩下 n≤3n\le 3 的情况显然可以特判,不再赘述。

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200005;
ll t,n,a[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--)
	{
		ll d=0;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			d=max(d,a[i]);
		}
		if(n==1)
		{
			cout<<d<<"\n";
		}
		else if(n==2)
		{
			cout<<max(a[1]+a[2],2*abs(a[1]-a[2]))<<"\n";
		}
		else if(n==3)
		{
			if(d==a[1]||d==a[3]) cout<<3*d<<"\n";
			else cout<<max(max(a[1]+a[2]+a[3],3*(d-min(a[1],a[3]))),3*max(a[1],a[3]))<<"\n";
		}
		else cout<<n*d<<"\n";
	}
	return 0;
}
原文