跳到正文

题解

数字游戏

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

文章信息

发布
2025年6月15日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 2 分钟

版本

  1. v1当前

#include <iostream> #include <cstdio> using namespace std; long long a[1000005], n, t, sum, ans, lsum, rsum; int main() { scanf(“%lld”, &n); for(int i = 1; i <= n; i++) { scanf(“%lld”, &t); if(a[t] == 0) sum++; // sum记录数字种数 a[t]++; // a[t]存储数字t的个数 }

C++
int l = 0, r = 1000000;
while(a[l] == 0) l++; // 找出最小值的位置 
while(a[r] == 0) r--; // 找出最大值的位置 

while(sum > 2)
{
	//只要sum>2直接消掉最大/小值中数量较少的一位
    if(a[l]<a[r])//消除所有l 
    {
    	t=a[l];
    	a[l]=0;
    	a[r]-=t;
    	ans+=2*t;
    	sum--;
    	while(a[l]==0)l++;
	}
	else if(a[l]>a[r])//消除所有r
	{
		t=a[r];
		a[r]=0;
		a[l]-=t;
		ans+=2*t;
		sum--;
		while(a[r]==0)r--;
	}
	else//数量相同一起消除 
	{
		t=a[l];
		a[l]=a[r]=0;
		ans+=2*t;
		while(a[l]==0)l++;
		sum--;
		if(sum==2)//如果左边只剩下2,不能消右边 
		{
			ans-=1;
			break;
		}
		while(a[r]==0)r--;
		sum--;
	}
}
printf("%lld %lld %lld", ans, l, r);
return 0;

}

原文