QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873151#7739. KnapsackmakeranWA 1ms5748kbC++20954b2025-01-26 10:01:052025-01-26 10:01:05

Judging History

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

  • [2025-01-26 10:01:05]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5748kb
  • [2025-01-26 10:01:05]
  • 提交

answer

#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
struct node {
	int w,v;
} obj[5010];
int dp[5010][10010];
int f[5010];
bool cmp(node a,node b) {
	if(a.w==b.w) return a.v>b.v;
	return a.w<b.w;
}
bool cmp2(int a,int b){
	return a>b;
}
signed main() {
	int n,w,k;
//	freopen("knapsack8.in","r",stdin);
	cin>>n>>w>>k;
	for(int i=1; i<=n; i++) {
		cin>>obj[i].w>>obj[i].v;
	}
	sort(obj+1,obj+1+n,cmp);
	for(int i=1; i<=n; i++) {
		for(int j=0; j<=w; j++) {
			if(j-obj[i].w>=0)
			dp[i][j]=max(dp[i-1][j],dp[i-1][j-obj[i].w]+obj[i].v);
		}
	}
	int res=0;
	int temp=0;
	int idx=0;
	for(int i=n;i>=1;i--){
		temp=dp[i][w];
		//cout<<dp[i][w]<<"\n";
		idx++;
		f[idx]=obj[i+1].v;
		for(int j=idx;j>1;j--){
			if(f[j]>f[j-1]) swap(f[j],f[j-1]);
			else break;
		}
		for(int j=1;j<=min(idx,k);j++){
			temp+=f[j];
		} 
		res=max(res,temp);
	}
	cout<<res;
	return 0;
}
//	f[1]=0;

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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: -100
Wrong Answer
time: 1ms
memory: 5748kb

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:

1577075983

result:

wrong answer 1st numbers differ - expected: '2009456281', found: '1577075983'