题目描述
[NOIP2006 普及组] 开心的金明
https://www.luogu.com.cn/problem/P1060
题目描述
金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间。更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过 N N N元钱就行”。今天一早金明就开始做预算,但是他想买的东西太多了,肯定会超过妈妈限定的 N N N元。于是,他把每件物品规定了一个重要度,分为 5 5 5等:用整数 1 − 5 1-5 1−5表示,第 5 5 5等最重要。他还从因特网上查到了每件物品的价格(都是整数元)。他希望在不超过 N N N元(可以等于 N N N元)的前提下,使每件物品的价格与重要度的乘积的总和最大。
设第 j j j件物品的价格为 v [ j ] v[j] v[j],重要度为 w [ j ] w[j] w[j],共选中了 k k k件物品,编号依次为 j 1 , j 2 , … , j k j_1,j_2,…,j_k j1,j2,…,jk,则所求的总和为:
v [ j 1 ] × w [ j 1 ] + v [ j 2 ] × w [ j 2 ] + … + v [ j k ] × w [ j k ] v[j_1] \times w[j_1]+v[j_2] \times w[j_2]+ …+v[j_k] \times w[j_k] v[j1]×w[j1]+v[j2]×w[j2]+…+v[jk]×w[jk]。
请你帮助金明设计一个满足要求的购物单。
输入格式
第一行,为 2 2 2个正整数,用一个空格隔开: n , m n,m n,m(其中 N ( < 30000 ) N(<30000) N(<30000)表示总钱数, m ( < 25 ) m(<25) m(<25)为希望购买物品的个数。)
从第 2 2 2行到第 m + 1 m+1 m+1行,第 j j j行给出了编号为 j − 1 j-1 j−1的物品的基本数据,每行有 2 2 2个非负整数$ v p (其中 (其中 (其中v 表示该物品的价格 表示该物品的价格 表示该物品的价格(v \le 10000) , , ,p$表示该物品的重要度( 1 − 5 1-5 1−5)
输出格式
1 1 1个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值 ( < 100000000 ) (<100000000) (<100000000)。
样例 #1
样例输入 #1
1000 5
800 2
400 5
300 5
400 3
200 2
样例输出 #1
3900
提示
NOIP 2006 普及组 第二题
meaning
easy 01 bag problem,notice that the value is v[i] * w[i]
idea
I want to write it without template ,but after i write it down , i can’t pass the example ,
first I think maybe i just write the wrong template —— total bag
after looking the problem solution , i just wrong on the for circle
Notice
for(int i = 1;i <= m;i++){
for(int j = n;j >= v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]] + w[i]);
}
the second for must from n to 0 ,can’t 0 to n
in this problem when the 1500 renew , if you from 0 to n,401,402,403…will be renew a wrong answer
A new find ,if you 0 to n, it’s total bag …
miaoa
AC code
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
int v[50],w[50];
int dp[30005];
int main(){
std::ios::sync_with_stdio(false);
int n,m;
cin >> n >> m;
for(int i = 1;i <= m;i++){
cin >> v[i] >> w[i];
w[i]*=v[i];
}
for(int i = 1;i <= m;i++){
for(int j = n;j >= v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]] + w[i]);
}
cout << dp[n] << endl;
return 0;
}
colorful egg
meet the schoolmate
the First problem solution is
dalao shichui
https://www.luogu.org/space/show?uid=21354