QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#392303#7132. Subset sumsI_Love_SonechkaWA 1ms3816kbC++172.5kb2024-04-17 14:19:302024-04-17 14:19:31

Judging History

你现在查看的是最新测评结果

  • [2024-04-17 14:19:31]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3816kb
  • [2024-04-17 14:19:30]
  • 提交

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));
	bool flag = false;
	for(auto x: sums) {
		if(x >= 0 && flag == 0) {
			for(int i = 0; i < cnt-1 && k; ++i, --k) {
				cout << x << "\n";
			}
			flag = 1;
		}
		for(int i = 0; i < cnt && k; ++i, --k) {
			cout << x << "\n";
		}
	}
	if(flag == false) {
		for(int i = 0; i < cnt && k; ++i, --k) {
			cout << 0 << "\n";
		}
	}
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(nullptr);
	int tt = 1;
	for(int t = 0; t < tt; ++t) {
    solver();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3620kb

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: 3564kb

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: 1ms
memory: 3816kb

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'