QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#392582#7132. Subset sumsI_Love_SonechkaWA 1ms3868kbC++172.2kb2024-04-17 17:28:392024-04-17 17:28:40

Judging History

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

  • [2024-04-17 17:28:40]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3868kb
  • [2024-04-17 17:28:39]
  • 提交

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, a;
	for(int i = 0; i < n; ++i) {
		int x; cin >> x;
		a.push_back(x);
		if(x <= 0) {
			negative.push_back(x);
		} else {
			positive.push_back(x);
		}
	}
	auto calc = [](vt<int> a, int k, bool flag = false) -> 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 += 10;
		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] << " ";
	}
}

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

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

input:

3 7
-1 0 1

output:

-1 -1 0 0 0 1 1 

result:

ok 7 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 3588kb

input:

10 1023
883143256 -498280460 630334270 570356589 -527809586 165589387 940711765 689070096 439819867 -683931378

output:

-1710021424 -1544432037 -1270201557 -1211740964 -1182211838 -1139664835 -1104612170 -1079687154 -1046151577 -1026090046 -1020951328 -1016622451 -974075448 -914097767 -860500659 -855361941 -826878168 -771921097 -769309659 -742391971 -699844968 -683931378 -661288781 -641384375 -639867287 -611855249 -6...

result:

ok 1023 numbers

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3828kb

input:

10 1023
943610328 -129243071 -591182147 -499909211 -734908696 246575063 -166609893 -533254143 407939687 553971004

output:

246575063 407939687 553971004 654514750 800546067 943610328 961910691 1190185391 1208485754 1351550015 1497581332 1598125078 1639860135 1744156395 1769103206 1806470028 1886435198 1905521019 1935713099 2015678269 2047799822 2053045091 2139769346 2152096082 2173114278 2177042893 2182288162 2193831139...

result:

wrong answer 1st numbers differ - expected: '-2655107161', found: '246575063'