QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#392317 | #7132. Subset sums | I_Love_Sonechka | WA | 0ms | 3848kb | C++17 | 2.1kb | 2024-04-17 14:30:05 | 2024-04-17 14:30:06 |
Judging History
answer
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
// c++ short types
#define Int long long
#define vt vector
void solver() {
int n, k; cin >> n >> k;
vt<int> positive, negative;
for(int i = 0; i < n; ++i) {
int x; cin >> x;
if(x <= 0) {
negative.push_back(x);
} else {
positive.push_back(x);
}
}
auto calc = [](vt<int> a, int k, bool flag = true) -> vt<Int>{
Int sum = accumulate(a.begin(), a.end(), 0);
if(flag) {
for(auto &x: a) {
x = -x;
}
}
sort(a.begin(), a.end());
vt<Int> sums;
multiset<pair<Int, int>> st;
st.insert({0, -1});
k ++;
while(sums.size() < k && not st.empty()) {
auto [sum, last] = *st.begin();
st.erase(st.begin());
sums.push_back(sum);
if(last + 1 < a.size()) {
st.insert({sum + a[last+1], last + 1});
if(last != -1) {
st.insert({sum + a[last+1] - (last >= 0 ? a[last] : 0), last + 1});
}
}
}
for(auto &x: sums) if(flag) {
x = sum + x;
}
auto it = find(sums.begin(), sums.end(), 0ll);
if(it != sums.end()) {
sums.erase(it);
}
return sums;
};
auto brute = [](vt<int> a, int k) {
vt<Int> sums;
for(int i = 0; i < (1<<a.size()); ++i) {
Int sum = 0;
for(int j = 0; j < a.size(); ++j) if(i >> j & 1){
sum += a[j];
}
sums.push_back(sum);
}
sort(sums.begin(), sums.end());
return sums;
};
vt<Int> spos = calc(positive, k);
vt<Int> npos = calc(negative, k, 1);
vt<int> ids(npos.size());
set<pair<Int, int>> st;
for(int i = 0; i < npos.size(); ++i) {
ids[i] = -1;
st.insert({npos[i], i});
}
vt<Int> sums = {};
while(not st.empty() && sums.size() < k) {
auto [sum, id] = *st.begin();
st.erase(st.begin());
sums.push_back(sum);
ids[id] ++ ;
if(ids[id] < spos.size()) {
st.insert({npos[id] + spos[ids[id]], id});
}
}
for(auto x: spos) {
sums.push_back(x);
}
sort(sums.begin(), sums.end());
for(int i = 0; i < k; ++i) {
cout << sums[i] << "\n";
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int tt = 1;
for(int t = 0; t < tt; ++t) {
solver();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3688kb
input:
2 3 -1 1
output:
-1 0 1
result:
ok 3 number(s): "-1 0 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
3 7 -1 0 1
output:
-1 -1 0 0 0 1 1
result:
ok 7 numbers
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3676kb
input:
10 1023 883143256 -498280460 630334270 570356589 -527809586 165589387 940711765 689070096 439819867 -683931378
output:
-6004988720 -5839399333 -5565168853 -5506708260 -5477179134 -5434632131 -5399579466 -5374654450 -5341118873 -5321057342 -5315918624 -5311589747 -5269042744 -5209065063 -5155467955 -5150329237 -5121845464 -5066888393 -5064276955 -5037359267 -4994812264 -4978898674 -4956256077 -4936351671 -4934834583 ...
result:
wrong answer 1st numbers differ - expected: '-1710021424', found: '-6004988720'