zoukankan      html  css  js  c++  java
  • HDU2652Warching TV (二分 dp)

     

    Warching TV
    Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 223    Accepted Submission(s): 80
    Problem Description
    Lemon likes warching TV very much. When winter holiday comes, it is a good time, isn’t it. There are lots of TV program listed on the paper. Every TV program has its start time ,end time, and the happiness value that will add Lemon’s happiness and it depends on Lemon’s taste.
     
    Input
    There are many test cases. Please process to end of file. Each test case starts with one integer N (1 <= N <= 100000) which indicates the size of the list of the TV program. Then N lines follow, each line contains three integers s, e and v. s, e indicate that the TV program is during [s, e](1 <= s <= e <= 1000000). v indicates that after warching the TV program will add Lemon v happyiness(1 <= v <= 1000). Once Lemon choose a TV program, he must finish warching the TV program.
     
    Output
    Print the maximum happiness value that Lemon will get.
     
    Sample Input

    2 1 2 1 3 4 2 2 1 2 1 2 4 2

     
    Sample Output

    3 2


     
    Author
    lemon
     
    Source
    决战龙虎门
     
    Recommend
    yifenfei   |   We have carefully selected several similar problems for you:  2658 2655 2656 2670 2650 
     题意:给了n个节目的开始时间,结束时间,和看完节目可收获的开心值,一个节目结束的时间必须大于下一个节目开始的时间,计算可以可以收获的最大开心值和。

    思路:dp,二分

    注意:把结构体定义在外面,不然会超时

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    const int N = 100010;
    struct node
    {
        int s,e,v;
    } nodes[N];    //定义到外面,可以节省运行时间,不然会超时,,
    bool comp(node a,node b)
    {
        if(a.e!=b.e)
        return a.e<b.e;
        return a.s<b.s;
    }
    int value[N];
    int dp[N];
    int main()
    {
        int n;
      
        while(~scanf("%d",&n))
        {
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&nodes[i].s,&nodes[i].e,&nodes[i].v);
    
        }
        sort(nodes,nodes+n,comp);
        int left,mid,right;
        dp[0]=nodes[0].v;
        for(int i=1;i<n;i++)
        {
            dp[i]=nodes[i].v;
            left=0;
            right=i-1;
            int ans=-1;
            mid=(left+right)/2;
            while(left<=right)   //二分找到最省时的节目播放方式
            {
                mid=(left+right)/2;
            if(nodes[mid].e<nodes[i].s)
            {
                ans=mid;
                left=mid+1;
            }
            else
            right=mid-1;
            }
            if (ans != -1)   //如果ans的值为-1,则说明第i个节目结束后没有节目可看了。
       {
        dp[i] = max(dp[i], dp[ans] + nodes[i].v);
       }
       dp[i] = max(dp[i], dp[i - 1]);
            //dp[i]=max(dp[i-1],max(dp[i],dp[mid]+nodes[i].v));   这样写没有考虑到看完当前节目后没有可开始的节目的情况,输出结果就会是开心值的简单累加
        }
        printf("%d
    ",dp[n-1]);
        }
        return 0;
    }
  • 相关阅读:
    python 3.6.2 使用VScode 安装requests包
    JExcel读取Excel,完成随机数对应学号,随机选取学生姓名
    HashMap和Map的道道
    腾讯云主机windows 2012 r2搭建ftp服务器
    关于网站域名访问出现welcome to nginx 的问题
    腾讯云主机windows 2012 r2标准版&&搭建java环境(jdk+tomcat)
    Anaconda 安装+使用+换源+更新
    已知三点求平面方程、平面法向量和点到平面的距离
    Open Cascade:计算体积
    VS2015安装QT插件
  • 原文地址:https://www.cnblogs.com/dshn/p/4750825.html
Copyright © 2011-2022 走看看