HDU 5188

这题我已不想吐槽自己了。第一反应就是个01背包,也就是加了些限制。开始时按 l 排序,按01的来写,呵呵,输出数组时才发现不对。后来改为按 l-t即最早开始时间排,OK。但自己手贱竟然写了个线段树要快速返回前面大的值。2B啊,既然按最早开始时间加入,那分值必定是按顺序递增的啊,根本就没必啊。。。所以,只需排序后按裸01来做就可以了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL __int64

using namespace std;
const int N=200010;
LL ret;
LL tree[N*4];
LL temp[N];
struct Node{
    int v,l,t,es;
    bool operator<(const Node &a) const{
        if(es<a.es)return true;
        return false;
    }
}node[35];

void search(int root,int L,int R,int l,int r){
    if(L<=l&&r<=R){
        ret=max(ret,tree[root]);
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m) search(root*2,L,R,l,m);
    if(m<R) search(root*2+1,L,R,m+1,r);
}

void update(int root,int L,int R,int l,int r,LL maxv){
    if(L<=l&&r<=R){
        tree[root]=maxv;
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m) update(root*2,L,R,l,m,maxv);
    if(m<R) update(root*2+1,L,R,m+1,r,maxv);
//    tree[root]=max(tree[root*2],tree[root*2+1]);
}

int main(){
    LL w; int n;
    int v,t,l; LL sum; int mt,ll;
    while(scanf("%d%I64d",&n,&w)!=EOF){
        mt=ll=-1; sum=0;
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&t,&v,&l);
            node[i].l=l; node[i].es=l-t;
            node[i].v=v;    node[i].t=t;
            mt=max(mt,t); ll=max(ll,l);
            sum+=v;
        }
        if(sum<w){
            printf("zhx is naive!\n");
            continue;
        }
        sort(node,node+n);
        memset(tree,0,sizeof(tree));
        memset(temp,0,sizeof(temp));
        for(int i=0;i<n;i++){
            for(int j=ll+mt;;j--){
                if(j<node[i].l) break;
                if(j-node[i].t<0) break;
        //        ret=-1;
    //            search(1,0,j-node[i].t,0,ll+mt);
    //            cout<<ret<<endl;
                if(temp[j-node[i].t    ]+node[i].v>temp[j]){
                    temp[j]=temp[j-node[i].t]+node[i].v;
            //        update(1,j,j,0,ll+mt,temp[j]);
                }
            }
    /*        for(int k=0;k<=ll+mt;k++)
            cout<<temp[k]<<" ";
            cout<<endl;*/
        }
        int i;
        for(i=0;i<=ll+mt;i++){
            if(temp[i]>=w){
                printf("%d\n",i);
                break;
            }
        }
        if(i>ll+mt)
        printf("zhx is naive!\n");
    }
    return 0;
}
时间: 2024-08-13 22:40:59

HDU 5188的相关文章

hdu 5188 zhx and contest [ 排序 + 背包 ]

传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 145    Accepted Submission(s): 49 Problem Description As one of the most powerful brushes in the world, zhx usually takes part

hdu 5188 dfs+二分

get了很多新技能 当时想到了用dfs,但是排序用的是限制时间排序,一直没搞出来. 正解: 二分用时,dfs判断,为了顺利进行做题,需要按照做题开始时间排序 还可以用dp 题意: 作为史上最强的刷子之一,zhx常常参与各种比赛. 有一天,zhx去虐一场比赛.他觉得题太简单了. 这场比赛有n道题.他一眼就已经计算出他做第i道题要花ti的时间,做完后可以得到vi分. 因为他太强了,所以他被管理员盯上了.如果他在第li个单位时间前做完了第i道题,那么管理员就会认为他在作弊,然后把他的号封了. zhx不

HDU 5188 背包

有N道题,要求得到最少W分 给出N道题的:每道题用时T,分数V,应在且必须在L时刻提交才能得分 问得到W分所用的最少的时间 以L-T排序,然后做01背包即可 #include "stdio.h" #include "algorithm" #include "string.h" using namespace std; struct Mark { int t,v,l,x; }mark[40]; int dp[300010]; bool cmp(Ma

hdu 3401 单调队列优化+dp

http://acm.hdu.edu.cn/showproblem.php?pid=3401 Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5188    Accepted Submission(s): 1776 Problem Description Recently, lxhgww is addicted to stoc

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};

HDU 3555 Bomb (数位DP)

数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下来. 简介: 顾名思义,所谓的数位DP就是按照数字的个,十,百,千--位数进行的DP.数位DP的题目有着非常明显的性质: 询问[l,r]的区间内,有多少的数字满足某个性质 做法根据前缀和的思想,求出[0,l-1]和[0,r]中满足性质的数的个数,然后相减即可. 算法核心: 关于数位DP,貌似写法还是