QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#392299 | #7132. Subset sums | I_Love_Sonechka | WA | 0ms | 3796kb | C++17 | 2.3kb | 2024-04-17 14:13:52 | 2024-04-17 14:13:52 |
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;
int zeroes = 0;
for(int i = 0; i < n; ++i) {
int x; cin >> x;
if(x == 0) {
zeroes ++;
} else 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());
int cnt = min(k, 1 << min(zeroes, 24));
for(auto x: sums) {
for(int i = 0; i < cnt && k; ++i, --k) {
cout << x << "\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: 3624kb
input:
2 3 -1 1
output:
-1 0 1
result:
ok 3 number(s): "-1 0 1"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3796kb
input:
3 7 -1 0 1
output:
-1 -1 0 0 1 1
result:
wrong answer 5th numbers differ - expected: '0', found: '1'