HDOJ 2037 今年暑假不AC 【贪心】
生活随笔
收集整理的這篇文章主要介紹了
HDOJ 2037 今年暑假不AC 【贪心】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
HDOJ 2037 今年暑假不AC 【貪心】
題目鏈接 http://acm.hdu.edu.cn/showproblem.php?pid=2037
給出n個電視節目的開始時間和結束時間,
要求合理安排如果看這些節目可以完整的看完更多節目
策略是觀看的節目的長度越短越好,也即節目越早結束我越想看它。
將節目的結束時間由小到大排序,
對于下一個節目,如果開始時間在上一個節目之后,那么這個節目也可以觀看(反正它結束的比下一個早,可以多留時間給更下一個節目)
貪心完成所有節目的選擇即可
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; typedef struct point{int x, y;bool operator < (const point& p) const{if(y == p.y) return x < p.x;else return y < p.y;}bool operator > (const point& p) const{return p < *this;} }p; p program[105]; int n, End, num;int main(){while(scanf("%d", &n), n){num = 1;for(int i = 0; i < n; i++) scanf("%d%d", &program[i].x, &program[i].y);sort(program, program+n);//for(int i = 0; i < n; i++) printf("%d\t%d\n", program[i].x, program[i].y);End = program[0].y;for(int i = 1; i < n; i++){if(program[i].x >= End){num++;End = program[i].y;}}printf("%d\n", num);}return 0; }
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
轉載于:https://www.cnblogs.com/miaowTracy/p/4836756.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的HDOJ 2037 今年暑假不AC 【贪心】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SVPWM实现
- 下一篇: 数据结构——链式队列解析(C语言版)