跳到正文

题解

题解:P11886 「Stoi2025」爱你没差

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

文章信息

发布
2025年3月16日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟
获赞
1
题目
P11886

版本

  1. v1当前

思路

根据样例提示,我们发现:贪心策略为每次合并当前最小的两个数,以确保后续的数尽可能大,从而在后续合并中更容易满足得分条件。

用小根堆维护,每次堆操作的时间复杂度为 O(log⁡n)O(\operatorname{log} n),总共有 n−1n - 1 次合并操作,总时间复杂度为 O(nlog⁡n)O(n \operatorname{log} n)。

注意:不开 __int128 见祖宗。

代码

C++
#include <bits/stdc++.h>
#define int __int128
using namespace std;

int n, m, ans;
priority_queue<int, vector<int>, greater<>> q;
int read() {
	char array[105];
	cin >> array;
	int ans = 0;
	int cnt = 1;
	if (array[0] == '-') {
		cnt = -1;
		for (int i = 1; i < strlen(array); i++) ans = ans * 10 + (array[i] - '0');
	} else {
		for (int i = 0; i < strlen(array); i++) ans = ans * 10 + (array[i] - '0');
	}
	return ans * cnt;
}
void print(int num) {
	if (num < 0) {
		putchar('-');
		print(-num);
	} else if (num > 9) {
		print(num / 10);
		putchar(num % 10 + '0');
	} else putchar(num + '0');
	return ;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	n = read();
    m = read();
	for (int i = 0, x; i < n; i++) {
		x = read();
		q.push(x);
	}
	while (q.size() > 1) {
		int x = q.top(); q.pop();
		int y = q.top(); q.pop();
		if (m * x >= y && m * y >= x) ans++;
		q.push(x + y);
	}
	print(ans);
	return 0;
}

AC 记录不放了

原文
存档评论1 条抓取于