[置顶] 九度笔记之 1434:今年暑假不AC

时间:2023-03-08 23:51:50
[置顶] 九度笔记之 1434:今年暑假不AC

题目1434:今年暑假不AC

时间限制:1 秒

内存限制:128 兆

特殊判题:否

提交:307

解决:180

题目描述:

“今年暑假不AC?”“是的。”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...”确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

输入:

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

输出:

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

样例输入:
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

算法分析

           属于特殊的动态规划问题,maxn[t] 表示在时间 t 之前,最多可以看的电视节目数。

           begt表示节目开始时间,endt表示节目结束时间。

           首先我们对于电视节目按照结束时间进行排序,然后计算考虑第i个电视节目情况下,所能看的最多电视节目数。

          为什么要先对电视节目按照结束时间进行排序呢,因为 考虑第i个电视节目情况下,所能看的最多电视节目数,也就是maxn[ tvs[j].endt ]  等于  

        int temMaxn = std::max(maxn[tvs.at(i).endt],maxn[tvs.at(i).begt]+1);

动态更新是通过比较当前节目 结束时间endt 和 开始时间begt+1 来更新的,所以要先对电视节目按照结束时间从早到晚排列,计算早些时间所能看的最多节目数。

         除了更新maxn[tvs.at(i).endt],我们还要更新maxn[j]  ,j>=tvs.at(i).endt

         因为当前我们仅考虑的是前i个电视节目,所以在tvs.at(i).endt之后的时间里没有其它的电视节目了,所能看的最多电视节目数就和max[tvs.at(i).endt]一样。

       for(int j = tvs.at(i).endt; j<endtime+1; j++){
            maxn[j] = temMaxn;
        }

        

普通背包问题
题目1364:v字仇杀队
题目1462:两船载物问题
题目1455:珍惜现在,感恩生活
题目1209:最小邮票数
题目1420:Jobdu MM分水果

项目安排类题目
题目1499:项目安排
题目1463:招聘会
题目1434:今年暑假不AC

资源无限求最大值的题目
题目1494:Dota


源程序

//============================================================================
// Name        : judo1434.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
  
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct TVshow{
    int begt;
    int endt;
};
std::vector<TVshow> tvs;
bool lessthan(const TVshow & tsl,const TVshow &tsr){
    return tsl.endt < tsr.endt;
}
void noAC(int n){
    TVshow ts;
    tvs.clear();
    for(int i = 0;i<n;i++){
        std::cin>>ts.begt>>ts.endt;
        tvs.push_back(ts);
    }
    std::sort(tvs.begin(),tvs.end(),lessthan);
  
    int endtime = tvs.back().endt;
    int *maxn = new int[endtime+1];
  
    for(int i = 0;i<endtime+1;i++)
        maxn[i]=0;
  
    for(int i = 0;i<n;i++){
        int temMaxn = std::max(maxn[tvs.at(i).endt],maxn[tvs.at(i).begt]+1);
        for(int j = tvs.at(i).endt; j<endtime+1; j++){
            maxn[j] = temMaxn;
           // maxn[j] = std::max(maxn[j],temMaxn);
        }
    }
    std::cout<<maxn[endtime]<<std::endl;
}
int main() {
    //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    int n;
    while(std::cin>>n && n!=0){
        noAC(n);
    }
    return 0;
}
/**************************************************************
    Problem: 1463
    User: KES
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1520 kb
****************************************************************/