跳到正文

题解

题解:P13707 [NWERC 2023] Higher Arithmetic

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

文章信息

收录
2025年12月12日
更新
2026年8月2日
篇幅
约 2 分钟
题目
P13707

版本

  1. v1当前

提供一种比较简洁的做法捏。

我们发现 11 非常烦啊,大力分讨一下。首先如果 11 和 22 都有剩余,那么组成 (1+2)(1+2) 显然是优的。于是我们处理完了所有的 11 或所有的 22,如果剩余的是 11 那就尽量三个一组 (1+1+1)(1+1+1),剩余的小心分讨一下即可。

快快乐乐找 coner case,我们发现 11 恰比 22 多一个的时候显然 (1+2)×⋯×(1+2)×(1+1)×2(1+2) \times \cdots \times (1+2) \times (1+1) \times 2 显然是要比 (1+2)×⋯(1+2)(1+2) \times \cdots (1+2) 剩下一个 11 更优的,特判即可。

C++
const int N=1e5+5;
int n,a[N],ch;

int main() {
	for(int i=(scanf("%d",&n),1);i<=n;i++) scanf("%d",&a[i]);
	if(n==1) return printf("%d\n",a[1]),0;
	sort(a+1,a+1+n);
	int d1=upper_bound(a+1,a+1+n,1)-a-1,p1=d1;
	int d2=upper_bound(a+1,a+1+n,2)-a-1,p2=d2;
	auto pt=[](int& ch)->void {(ch?printf("*"):ch=1);};
	for(;p2>d1&&p1>0;p1--,p2--)
		if(p2>d1+1||p1!=2) pt(ch),printf("(2+1)");
		else pt(ch),printf("(2+1+1)"),--p1;
	for(;p2>d1;--p2) pt(ch),printf("2");
	for(;p1>4;p1-=3) pt(ch),printf("(1+1+1)");
	if(p1==4) pt(ch),printf("(1+1)*(1+1)");
	if(p1==3) pt(ch),printf("(1+1+1)");
	if(p1==2) pt(ch),printf("(1+1)");
	if(p1==1) pt(ch),printf("(1+%d)",a[++d2]);
	for(++d2;d2<=n;d2++) pt(ch),printf("%d",a[d2]);
	return printf("\n"),0;
}
原文