zoukankan      html  css  js  c++  java
  • A

    A - 活动安排问题

    有若干个活动,第i个开始时间和结束时间是[Si,fi),同一个教室安排的活动之间不能交叠,求要安排所有活动,最少需要几个教室?  
    Input第一行一个正整数n (n <= 10000)代表活动的个数。 
    第二行到第(n + 1)行包含n个开始时间和结束时间。 
    开始时间严格小于结束时间,并且时间都是非负整数,小于1000000000Output一行包含一个整数表示最少教室的个数。Sample Input

    3
    1 2
    3 4
    2 9

    Sample Output

    2

    虽然说,以前做过这道题了,但是现在再做一遍的时候还是感觉挺吃力的,思想比基本的贪心多了一点,用时间段去找房间,可以用就把该时间段放进去,不能的话就再开一个房间

    AC代码

    #include<stdio.h>
    #include<algorithm>
    #include<iostream>
    #include<string.h>
    const int maxn = 10010;
    
    using namespace std;
    
    std::pair<int, int> ivt[maxn];
    
    int main()
    {
        int n, ans;
        int i, j;
        int b[maxn];
    
        scanf("%d", &n);
    
        ans = 1;
        memset(b, 0, sizeof(b));
    
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &ivt[i].first);
            scanf("%d", &ivt[i].second);
        }
    
        sort(ivt, ivt + n);
    
        b[0] = ivt[0].second;
    
        for(i = 1; i < n; i++)
        {
            for(j = 0; j < ans; j++)
            {
                if(b[j] <= ivt[i].first)
                {
                    b[j] = ivt[i].second;
                    break;
                }
            }
    
            if(j == ans)
            {
                b[ans] = ivt[i].second;
                ans++;
            }
        }
    
        printf("%d
    ", ans);
    
        return 0;
    }
    View Code
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    C#+API实现指定窗体激活
    DEVC++学习之(一)
    javascript 实现原生下载的各种情况
    IssueVision 之WebService安全篇
    Add relationship to BS sample
    ExpandRelationWithCtxt 与 GetRelatedObjects 的区别
    C#调用javascript
    解禁网页限制
    Unix cc options vs gcc options
    IssueVision 之模式篇
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8128103.html
Copyright © 2011-2022 走看看