题目描述
[NOIP2006 普及组] 开心的金明
https://www.luogu.com.cn/problem/P1060
题目描述
金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间。更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NNN元钱就行”。今天一早金明就开始做预算,但是他想买的东西太多了,肯定会超过妈妈限定的NNN元。于是,他把每件物品规定了一个重要度,分为555等:用整数1−51-51−5表示,第555等最重要。他还从因特网上查到了每件物品的价格(都是整数元)。他希望在不超过NNN元(可以等于NNN元)的前提下,使每件物品的价格与重要度的乘积的总和最大。
设第jjj件物品的价格为v[j]v[j]v[j],重要度为w[j]w[j]w[j],共选中了kkk件物品,编号依次为j1,j2,…,jkj_1,j_2,…,j_kj1,j2,…,jk,则所求的总和为:
v[j1]×w[j1]+v[j2]×w[j2]+…+v[jk]×w[jk]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]。
请你帮助金明设计一个满足要求的购物单。
输入格式
第一行,为222个正整数,用一个空格隔开:n,mn,mn,m(其中N(<30000)N(<30000)N(<30000)表示总钱数,m(<25)m(<25)m(<25)为希望购买物品的个数。)
从第222行到第m+1m+1m+1行,第jjj行给出了编号为j−1j-1j−1的物品的基本数据,每行有222个非负整数$ v p(其中(其中(其中v表示该物品的价格表示该物品的价格表示该物品的价格(v \le 10000),,,p$表示该物品的重要度(1−51-51−5)
输出格式
111个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值(<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