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 }
  • 相关阅读:
    线性表的各种基本操作
    malloc&&free的系统运行机制及其源代码的理解
    剪枝的定义&&hdu1010
    hdu 1045
    hdu2094 stl之set的应用
    关联式容器的总结
    STL之map容器的详解
    2018-2019 ACM-ICPC 焦作赛区 部分题解
    2018-2019 ACM-ICPC 沈阳赛区 K. Let the Flames Begin
    2018-2019 ACM-ICPC 徐州区域赛 部分题解
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4594122.html
Copyright © 2011-2022 走看看