列表

详情


NC25029. [USACO 2007 Dec S]Charm Bracelet

描述

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

输入描述

* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di

输出描述

* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints

示例1

输入:

4 6
1 4
2 6
3 12
2 7

输出:

23

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Pascal(fpc 3.0.2) 解法, 执行用时: 64ms, 内存消耗: 256K, 提交时间: 2021-10-02 15:42:50

var
  f:array[0..100001] of longint;
  n,m,i,j,x,y:longint;
begin
  readln(n,m);
  for i:=1 to n do
   begin
     readln(x,y);
     for j:=m downto x do
      if f[j-x]+y>f[j] then f[j]:=f[j-x]+y;
   end;
  writeln(f[m]);
end.

C++14(g++5.4) 解法, 执行用时: 97ms, 内存消耗: 492K, 提交时间: 2020-03-12 17:14:18

#include<bits/stdc++.h>
using namespace std;
int n,m;
int v,w;
int f[100005];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>v>>w;
		for(int i=m;i>=v;i--)
			f[i]=max(f[i],f[i-v]+w);
	}
	cout<<f[m]<<endl;
}

C++(clang++ 11.0.1) 解法, 执行用时: 50ms, 内存消耗: 448K, 提交时间: 2022-10-05 19:26:57

#include<bits/stdc++.h>
using namespace std;
int n,m,w,d,f[13000];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&w,&d);
		for(int j=m;j>=w;j--)f[j]=max(f[j],f[j-w]+d);
	}
	printf("%d\n",f[m]);
 } 

pypy3 解法, 执行用时: 351ms, 内存消耗: 33984K, 提交时间: 2022-12-01 22:16:34

N,M=map(int,input().split())
f=[0]*(M+1)
for _ in range(N):
    w,d=map(int,input().split())
    for m in range(M,-1,-1):
        if m<w:break
        f[m]=max(f[m],f[m-w]+d)
print(max(f))
        

上一题