QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873157#7739. KnapsacktmbcanWA 0ms3968kbC++141.8kb2025-01-26 10:03:402025-01-26 10:03:40

Judging History

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

  • [2025-01-26 10:03:40]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3968kb
  • [2025-01-26 10:03:40]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<climits>
#include<cstdlib>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T>
inline void read(T&x){
	int w = 0;x = 0;
	char ch = getchar();
	while(ch<'0' || ch>'9'){
		if(ch=='-') w = 1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9'){
		x = (x<<1)+(x<<3)+(ch^48);
		ch = getchar();
	}
	if(w) x = ~x+1;
}
template <typename T,typename...Args>
inline void read(T&t,Args&...args){
	read(t);read(args...);
}
template <typename T>
inline T Min(T x,T y){ return (x < y ? x : y); }
template <typename T>
inline T Max(T x,T y){ return (x > y ? x : y); }
template <typename T>
inline T Abs(T x){ return (x < 0 ? ~x+1 : x); }
const int N = 5e3+10,M = 1e4+10;
int n,W,K;
struct node{
	int w; ll v;
	inline bool operator < (const node&G) const{
		return v > G.v;
	}
}g[N];
int vis[N],cnt;
ll dp[M],sumv;
int main(){
//	freopen("knapsack3.in","r",stdin);
	
	read(n,W,K);
	for(int i=1;i<=n;++i){
		read(g[i].w,g[i].v); 
		sumv += g[i].v;
	}
	sort(g+1,g+1+n);
//	for(int i=1;i<=n;++i) cout << g[i].w << " " << g[i].v << endl; cout << endl;
	for(int i=1;i<=n;++i){
		for(int j=W;j>=g[i].w;--j){
			dp[j] = Max(dp[j],dp[j-g[i].w]+g[i].v);
		}
	}
//	cout << dp[W] << endl;
	for(int i=1,tW=W;i<=n && tW>0;++i){
		for(int j=tW;j>=g[i].w;--j){
			if(dp[j]==dp[j-g[i].w]+g[i].v){
//				cout << j << " " << g[i].w << endl;
//				cout << i << endl << endl;	
				vis[i] = 1;
				++cnt;
				tW -= g[i].w;
				break;
			}
		}
	}
//	cout << cnt << endl;
	for(int i=1,tk=0;i<=n && tk<K;++i){
		if(vis[i]) continue;
		dp[W] += g[i].v;
		++tk;
	}

	printf("%lld",dp[W]);
	
	
	return 0;
}
/*
#1
4 10 1
9 10
10 1
3 5
5 20

#2
5 13 2
5 16
5 28
7 44
8 15
8 41
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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

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: 0
Accepted
time: 0ms
memory: 3840kb

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:

5201000365

result:

ok 1 number(s): "5201000365"

Test #6:

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

input:

10 3000 10
1325 563890842
2007 190665722
1393 874490922
548 279594682
1380 155046921
2666 894516819
770 740325614
2735 643777488
2451 754155860
1068 138544189

output:

5235009059

result:

ok 1 number(s): "5235009059"

Test #7:

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

input:

10 10000 5
108 735534045
6250 87364128
3071 66920092
9343 555321302
9070 759896065
9843 146885261
3083 364637443
7088 370871572
7802 754417134
3125 697204945

output:

4451687859

result:

ok 1 number(s): "4451687859"

Test #8:

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

input:

100 50 61
24 517916473
33 497071404
40 343150837
13 559776223
2 941245278
27 987936903
7 403293890
26 68412861
28 683505315
6 173482637
31 220799032
29 815472376
42 426462445
25 470177395
43 818534622
26 137556071
15 308105056
27 745044655
28 309413241
11 61130780
36 963194467
19 701095156
5 9347020...

output:

45198386194

result:

wrong answer 1st numbers differ - expected: '44747553879', found: '45198386194'