zoukankan      html  css  js  c++  java
  • 51nod 活动安排问题

    有若干个活动,第i个开始时间和结束时间是[Si,fi),同一个教室安排的活动之间不能交叠,求要安排所有活动,最少需要几个教室? 

    输入

    第一行一个正整数n (n <= 10000)代表活动的个数。
    第二行到第(n + 1)行包含n个开始时间和结束时间。
    开始时间严格小于结束时间,并且时间都是非负整数,小于1000000000
    输出
     
    一行包含一个整数表示最少教室的个数。
     
    输入示例

    3
    1 2
    3 4
    2 9

    输出示例
     2
     
    题解:
      两边排序,start比end小说明此时需要安排教室,反之继续进行
    #include<cstdio>  
    #include<iostream>  
    #include<algorithm>  
    typedef long long LL;  
    using namespace std;  
      
    LL startime[10001];  
    LL endtime[10001];  
      
    int main()
    {  
        int n;  
        scanf("%d",&n);  
          
        for(int i=0;i<n;i++)  
            scanf("%LLd%LLd",&startime[i],&endtime[i]);  
          
        sort(startime,startime+n);  
        sort(endtime,endtime+n);  
    
        int counter=0;  
        int answer=0;
          
        for(int i=0;i<n;i++)
        {  
            if(startime[i]<endtime[counter])
               answer++;    
            else counter++;  
        }  
        printf("%d",answer);    
        return 0;  
    }   
    View Code

    题解2;

      一样的思路,用队列实现

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    struct node
    {
        ll x,y;
        friend  bool operator <(node a,node b)
            {
                return a.x<b.x;
            }
    }a[100001];
    priority_queue<int ,vector<int >,greater<int> >s;
    
    int main()
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
            scanf("%lld%lld",&a[i].x,&a[i].y);
     
        sort(a+1,a+1+n);
        
        int ans=1;
        s.push(a[1].y);
        
        for(int i=2;i<=n;i++)
        {
            if(!s.empty())
            {
                int t=s.top();
                if(a[i].x>=t)
                {
                    s.pop();
                    s.push(a[i].y);
                }
                else
                {
                    ans++;
                    s.push(a[i].y);
                }
            }
            else
            {
                ans++;
                s.push(a[i].y);
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    View Code
    
    
  • 相关阅读:
    浏览器基础知识点及常考面试题
    java设计模式之综述
    maven的基本原理和使用
    maven的介绍和安装
    Spring整合Struts2的方法
    Spring整合Hibernate的方法
    Spring中的事务管理
    Spring中的JDBC操作
    基于XML配置的Sping AOP详解
    基于注解的Sping AOP详解
  • 原文地址:https://www.cnblogs.com/kekit/p/7605294.html
Copyright © 2011-2022 走看看