mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-08-21 18:52:07 +00:00
添加主程序实现,包含输入处理、负数求和、绝对值排序及优先队列计算
This commit is contained in:
parent
fb5c203e58
commit
401024ab86
57
src/7/21/T636328d.cpp
Normal file
57
src/7/21/T636328d.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
using ll = long long;
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
cout.tie(nullptr);
|
||||
|
||||
ll n, k;
|
||||
cin >> n >> k;
|
||||
vector<ll> a(n);
|
||||
ll C = 0;
|
||||
for (ll i = 0; i < n; i++) {
|
||||
cin >> a[i];
|
||||
if (a[i] < 0) {
|
||||
C += a[i];
|
||||
}
|
||||
}
|
||||
|
||||
vector<ll> b;
|
||||
for (ll x : a) {
|
||||
b.push_back(abs(x));
|
||||
}
|
||||
sort(b.begin(), b.end());
|
||||
|
||||
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
|
||||
pq.push({0, 0});
|
||||
|
||||
vector<ll> ans;
|
||||
while (ans.size() < k && !pq.empty()) {
|
||||
auto [sum, idx] = pq.top();
|
||||
pq.pop();
|
||||
if (idx == n) {
|
||||
ans.push_back(sum + C);
|
||||
} else {
|
||||
pq.push({sum, idx + 1});
|
||||
pq.push({sum + b[idx], idx + 1});
|
||||
}
|
||||
}
|
||||
|
||||
for (ll i = 0; i < ans.size(); i++) {
|
||||
cout << ans[i];
|
||||
if (i < ans.size() - 1) {
|
||||
cout << ' ';
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user