zoukankan      html  css  js  c++  java
  • 51nod 1428 活动安排问题 (贪心+优先队列)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428

    开始一直题意理解错误.

    首先按照开始时间从小到大排序.

    其实只要维护一个结束时间的最小堆,每次比较开始时间和堆中最小时间的大小,如果比它大就放入堆中并且时间就要变成当前任务的结束时间,

    否则就要新开一个教室.并且把结束时间加入堆中,注意判断堆是否为空.

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #include <algorithm>
     5 using namespace std;
     6 struct point
     7 {
     8     int x,y;
     9     bool operator < (const point a) const
    10     {
    11         return x<a.x;
    12     }
    13 }p[10001];
    14 
    15 int main()
    16 {
    17     //freopen("a.txt","r",stdin);
    18     int n;
    19     priority_queue<int,vector<int>,greater<int> >que;
    20     scanf("%d",&n);
    21     for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y);
    22     sort(p,p+n);
    23     //for(int i=0;i<n;i++) printf("%d %d
    ",p[i].x,p[i].y);
    24     que.push(p[0].y);
    25     int ans=1;
    26     for(int i=1;i<n;i++)
    27     {
    28         if(!que.empty())
    29         {
    30             int a=que.top();
    31             //printf("%d
    ",p[i].x);
    32             if(p[i].x>=a)
    33             {
    34                 que.pop();
    35                 a=p[i].y;
    36                 que.push(a);
    37             }
    38             else
    39             {
    40                 ans++;
    41                 que.push(p[i].y);
    42             }
    43         }
    44         else
    45         {
    46             ans++;
    47             que.push(p[i].y);
    48         }
    49     }
    50     printf("%d
    ",ans);
    51     return 0;
    52 }
  • 相关阅读:
    Winform 时间
    button的后台点击事件
    Winform文本框只能输入限定的文本
    vue的生命周期函数
    ES6新增语法
    购物车案例(JavaScript动态效果)
    前端es6总结
    jQuery与vue的区别是什么?
    vue实现双向绑定原理
    JS实现简单分页功能
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4594122.html
Copyright © 2011-2022 走看看