QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#776776#7739. KnapsackguangxuautumnWA 0ms3612kbC++141.5kb2024-11-23 20:56:282024-11-23 20:56:30

Judging History

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

  • [2024-11-23 20:56:30]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3612kb
  • [2024-11-23 20:56:28]
  • 提交

answer

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const int N = 1e4 + 10;

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

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

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

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

	ll n, W, k;
	cin >> n >> W >> k;

	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;

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

	ll index = n - k;

	max_k[index --] = sum;

	for(int i = n - k - 1; i >= 0; i --) {
		// cout << index << ' ';
		ll 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;
	}

	int old = 1, now = 0;
	ll 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: 0ms
memory: 3588kb

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

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: 0ms
memory: 3544kb

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: 0ms
memory: 3612kb

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

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'