QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#776766#7739. KnapsackguangxuautumnWA 1ms3628kbC++141.6kb2024-11-23 20:53:212024-11-23 20:53:26

Judging History

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

  • [2024-11-23 20:53:26]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3628kb
  • [2024-11-23 20:53:21]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e4 + 10;

int dp[2][N];
int max_k[N];
priority_queue<int, vector<int>, greater<int>> big;//小根堆

struct gem{
	int v; //value
	int w; //weight
};

bool cmp(gem x, gem y)
{
	if(x.w == y.w) return x.v < y.v;
	else return x.w < y.w;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n, W, k;
	cin >> n >> W >> k;

	vector<int> w(n), v(n);
	vector<gem> g(n);

	for(int i = 0; i < n; i ++) {
		// int tw, tv;
		cin >> g[i].w >> g[i].v;
	}

	sort(g.begin(), g.end(), cmp);

	// for(int i = 0; i < g.size(); i ++) {
	// 	cout << g[i].w << ' ' << g[i].v << endl;
	// }cout << endl;

	int sum = 0;
	for(int i = n - 1; i >= n - k; i --) {
		sum += g[i].v;
		big.push(g[i].v);
	}
	// cout << big.top() << endl;

	int index = n - k;

	max_k[index --] = sum;

	for(int i = n - k - 1; i >= 0; i --) {
		// cout << index << ' ';
		int t = big.top();
		big.pop();
		if(g[i].v > t) {
			big.push(g[i].v);
			sum += g[i].v - t;
		} else big.push(t);

		max_k[index --] = sum;
	}
	// cout << index << endl;
	// for(int i = 0; i < n; i ++) cout << max_k[i] << ' ';
	// 	cout << endl;
	int old = 1, now = 0;
	int ans = 0;
	for(int i = 0; i < n - k; i ++) {
		swap(old, now);
		// cout << old << now << endl;
		for(int j = 0; j <= W; j ++) {
			if(g[i].w > j) dp[now][j] = dp[old][j];
			else dp[now][j] = max(dp[old][j], dp[old][j - g[i].w] + g[i].v);
		}
		// cout << dp[now][W] << endl;
		ans = max(ans, dp[now][W] + max_k[i + 1]);
	}

	cout << ans << endl;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3496kb

input:

4 10 1
9 10
10 1
3 5
5 20

output:

35

result:

ok 1 number(s): "35"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

5 13 2
5 16
5 28
7 44
8 15
8 41

output:

129

result:

ok 1 number(s): "129"

Test #3:

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

input:

10 50 1
44 182173741
38 163268500
36 114173760
30 521894533
25 89514235
12 516184197
42 971377551
35 28242326
31 480227821
31 388523197

output:

2009456281

result:

ok 1 number(s): "2009456281"

Test #4:

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

input:

10 100 3
23 51015869
9 981426050
76 243762017
64 128189636
4 718411601
48 250140255
17 340478117
68 262055220
40 370503079
4 547232664

output:

3765024872

result:

ok 1 number(s): "3765024872"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 3616kb

input:

10 500 10
430 981427684
100 458631577
32 453298334
393 716958962
82 120486064
393 561149128
182 518807793
293 950335710
332 159193263
331 280711850

output:

0

result:

wrong answer 1st numbers differ - expected: '5201000365', found: '0'