题解:P11886 「Stoi2025」爱你没差
思路
根据样例提示,我们发现:贪心策略为每次合并当前最小的两个数,以确保后续的数尽可能大,从而在后续合并中更容易满足得分条件。
用小根堆维护,每次堆操作的时间复杂度为
注意:不开 __int128 见祖宗。
代码
#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 记录不放了