zoukankan      html  css  js  c++  java
  • [洛谷] P2677 超级书架2

    贪心 DP 背包

    //#pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <ctime>
    #include <vector>
    #include <fstream>
    #include <list>
    #include <iomanip>
    #include <numeric>
    using namespace std;
    typedef long long ll;
    
    const int MAXN = 1e8 + 10;
    
    int volume[MAXN];
    
    int dp[MAXN];
    
    int main()
    {
        //ios::sync_with_stdio(false);
    
        //cin.tie(0);     cout.tie(0);
    
        int n, len, sum = 0, ans;
    
        cin>>n>>len;
    
        for(int i = 1; i <= n; i++)
        {
            cin>>volume[i];
        }
    
        sort(volume + 1, volume + n + 1);
    
        for(int i = 1; i <= n; i++)
        {
            sum += volume[i];
            if(sum > len)
                break;
        }
    
        ans = sum;
    
        for(int i = 1; i <= n; i++)
        {
            for(int j = sum; j >= volume[i]; j--)
            {
                dp[j] = max(dp[j], dp[j - volume[i]] + volume[i]);
                if(dp[j] >= len && dp[j] <= ans)
                    ans = dp[j];
            }
     
        }
    
        cout<<ans - len<<endl;
    
        return 0;
    }
  • 相关阅读:
    流程控制语句
    lminus
    TCL create list from file
    DFT 问答 III
    DFT 问答 II
    DFT 问答 I
    猜字符小游戏
    用户模式构造
    基元线程同步构造
    七大原则,24种设计模式
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270441.html
Copyright © 2011-2022 走看看