跳到正文

题解

P8587题解

akakal约 2 分钟
原文

这是 2025年12月1日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

这是本菜鸡的首篇题解,望通过^_^


思路

数据范围为 1≤n≤1061\leq n\leq 10^6,1≤hi≤3×1031\leq h_i \leq 3\times 10^3。

数据很小啊,我们可以用桶排序来完成。

其实,让整个题难度变成黄题就是因为它的数据范围。一说到桶排序,整个题就简单起来了呢。

上次用桶排序的时候还是上次,太久远了

我们使用两个桶,先用一个桶记矿石高度,循环把所有合成的柱子高度放在另一个桶中就搞定了。

AC代码

C++
#include <bits/stdc++.h>
using namespace std;
//数组大小 常量前用 k 符合规范
const int karr = 10010;
int b[karr],c[karr];
//max 为系统函数,所以用 _max
int n,sub,ans,ans2,_max;
int main(){
    //开启 cin cout 优化
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> sub;
		b[sub]++,_max = max(_max,sub);
	} 
	for(int i = 1; i <= _max; i++)
		for(int j = 1; j <= i; j++)
			if(b[i] && b[j]){
				if(i == j) c[i+j] += b[i] / 2;
				else c[i+j] += min(b[i],b[j]);
			}
    int _2max = _max * 2;
	for(int i = 1; i <= _2max; i++) ans = max(ans,c[i]);
	for(int i = 1; i <= _2max; i++) if(c[i] == ans) ans2++;
	cout << ans << " " << ans2 << endl;
	return 0;
}

第一次写题解,不知道怎么写,望大佬提醒!

审核大大辛苦了~