今年暑假不AC(解题报告)

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=3&problemid=3

乍看之下,感觉要用动态规划。仔细分析题目你会发现,当我们将结束时间排序后,可以使用逐步的贪婪算法求出最优解。

至于为什么要用结束时间排序,因为可以出现开始时间为1结束时间很长的节目。

如 节目1(1-8) 节目2(2-4) 节目3(4-7) 这时的最优解是选择节目2,3。如果使用开始时间排序的话会得到节目1,

而非最优解

#include<iostream>
#include<string.h>
int main()
{
    int n,i=0;
    while(std::cin>>n, n != 0)
    {
         
        int* begin = new int[n];
        int* end = new int[n];
        int* res = new int[n];
        memset(res,0,n*sizeof(int));
        int index = 0;
        for(int i = 0; i < n; i++)
        {
            std::cin>>begin[i]>>end[i];
        }
        for(int i = 0; i < n; i++)
            for(int j = n - 1 ; j > i; j--)
            {
                if(end[j] < end[j - 1])
                {
                    int e = end[j];
                    end[j] = end[j-1];
                    end[j-1] = e;
                    int b = begin[j];
                    begin[j] = begin[j-1];
                    begin[j-1] = b;
                }
                else if(end[j] == end[j-1])
                {
                    if(begin[j] < begin[j-1])
                    {
                        int temp = begin[j];
                        begin[j] = begin[j-1];
                        begin[j-1] = begin[j];
                    }
                }
            }
        for(int i = 0; i < n; i++)
        {
            if(res[index] <= begin[i])
                res[++index] = end[i];
        }
        std::cout<<index<<std::endl;
        delete begin;
        delete end;
        delete res;
    }
    return 0;
}

posted on   Bourbon  阅读(751)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
< 2011年7月 >
26 27 28 29 30 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

导航

统计

点击右上角即可分享
微信分享提示