zoukankan      html  css  js  c++  java
  • hdu5188 01 背包

    这题说的是给了n道题每道题用时ti分钟,需要在Li 时间或者之后完成,得分为vi, 我们 首先必须要能过完成,尽量让L-t小的放在前面。

    然后采用背包去做

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    const int maxn =30+5;
    typedef long long LL;
    struct point{
      int t,v,L,x;
      bool operator <(const point &rhs)const{
          if(x!=rhs.x) return x<rhs.x;
          return L<rhs.L;
      }
    }P[maxn];
    LL dp[400005];
    int main()
    {
        int n,w;
        while(scanf("%d%d",&n,&w)==2){
             LL sum1=0;
             int sumt=0,ub=0;
             for(int i =0; i<n; i++){
                 scanf("%d%d%d",&P[i].t,&P[i].v,&P[i].L);
                  ub = max(P[i].L,ub);
                  sum1+=P[i].v;
                  sumt+=P[i].t;
                  P[i].x=P[i].L-P[i].t;
             }
             if(sum1<w){
                 printf("zhx is naive!
    ");continue;
             }
             sort(P,P+n);
              ub = sumt+ub;
             memset(dp,0,sizeof(dp));
             for(int i=0; i<n; ++i)
                for(int j=ub; j>=P[i].L&&j>=P[i].t; j--)
                 dp[j] = max(dp[j],dp[j-P[i].t]+P[i].v);
             int ans=ub+1;
             for(int i =0; i<=ub; i++)
             if(dp[i]>=w){
                 ans=i; break;
             }
             if(ans == ub+1){
                 printf("zhx is naive!
    ");continue;
             }else printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Django -- 模板系统
    CSRF_TOKEN
    MySQL的sql_mode模式说明及设置
    程序员必备的600单词
    前端 -- jQuery
    datatime模块
    github高效搜索
    Mac上Homebrew常用命令总结
    对比System.currentTimeMillis()、new Date().getTime()、System.nanoTime()
    MACOS安装使用kafka
  • 原文地址:https://www.cnblogs.com/Opaser/p/4351004.html
Copyright © 2011-2022 走看看