• POJ 3624 Charm Bracelet (01背包)


    题目链接:http://poj.org/problem?id=3624

    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.

    Input

    * 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 andDi

    Output

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

    Sample Input

    4 6
    1 4
    2 6
    3 12
    2 7

    Sample Output

    23

    题解:还是01背包模板题
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=100005;
    33 const int mod=1e9+7;
    34 int dp[N];
    35 int main()
    36 {
    37     std::ios::sync_with_stdio(false);
    38     int n,m,x,y;
    39     mem(dp);
    40     cin>>n>>m;
    41     for(int i=1;i<=n;i++){
    42         cin>>x>>y;
    43         for(int j=m;j>=x;j--){
    44             dp[j]=max(dp[j],dp[j-x]+y);
    45         }
    46     }
    47     cout<<dp[m]<<endl;
    48     return 0;
    49 }
    View Code
  • 相关阅读:
    Java:day11
    Java:day10
    Java:day9
    Java:day8
    纯虚函数和抽象类
    C++的虚拟继承
    派生类构造函数、析构函数的定义和调用次序
    c++的继承方式——公有、保护、私有
    操作系统中系统调用的执行过程
    C++的类
  • 原文地址:https://www.cnblogs.com/shixinzei/p/7299760.html
Copyright © 2020-2023  润新知