zoukankan      html  css  js  c++  java
  • HDU-5188zhx and contest(贪心+背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5188

    题目大意:打比赛得积分,有n个题,要求达到w分,求最短时间,每个题有消耗时间t,得分v,以及一个限制l(完成该题的时间不能在l前面).

    Sample Input

    1 3
    1 4 7
    3 6
    4 1 8
    6 8 10
    1 5 2
    2 7
    10 4 1
    10 2 3

    Sample Output

    7
    8
    zhx is naive!

    emmm,这个需要贪心配合背包,我们知道的是,如果他写1道题目要2分钟,但限制为4分钟,那么这多余的2分钟就会被浪费掉,所以我们必须先选择充分利用时间的,也就是$l-t$越小的越优。

    排完序然后就是跑01背包了,自己定义一个时间时间上限作为背包容量即可跑板子了

    以下是AC代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int mac=1e5+10;
    
    long long dp[mac*30];
    struct node
    {
        int t,v,l;
        bool operator<(const node&a)const {
            return l-t<a.l-a.t;//越充分利用时间越优,浪费时间的后选
        }
    }a[50];
    
    int main()
    {
        int n,w;
        while (~scanf ("%d%d",&n,&w)){
            memset(dp,0,sizeof dp);
            int s=0,ss=0;
            for (int i=0; i<50; i++)
                a[i].t=a[i].v=a[i].l=0;
            long long pt=0;
            for (int i=1; i<=n; i++){
                int t,v,l;
                scanf ("%d%d%d",&t,&v,&l);
                a[i]=node{t,v,l};
                pt+=v;
                s+=a[i].t;ss=max(a[i].l,ss);
            }
            if (pt<w) {printf("zhx is naive!
    "); continue;}
            int tot=max(s,ss)+10;
            sort(a+1,a+1+n);
            for (int i=1; i<=n; i++){
                for (int j=tot; j>=max(a[i].t,a[i].l); j--){
                    dp[j]=max(dp[j],dp[j-a[i].t]+a[i].v);
                }
            }
            for (int i=0; i<=tot; i++)
                if (dp[i]>=w) {
                    printf("%d
    ",i);
                    break;
                }
        }
    
        return 0;
    }
    路漫漫兮
  • 相关阅读:
    python-全局变量是否使用global总结
    python-多线程创建以及查看
    python-tcp客户端
    python-udp聊天器
    python-udp接受数据
    python-udp发送数据
    python-正则表达式总结
    JAVA程序员面试必知32个知识点
    计算机专业英语基础篇
    非常经典有深度的电影英文台词
  • 原文地址:https://www.cnblogs.com/lonely-wind-/p/13263584.html
Copyright © 2011-2022 走看看