zoukankan      html  css  js  c++  java
  • 活动安排问题(区间类贪心)

    题意 : 设有n个活动的集合E={1, 2, ..., n},其中,每个活动都要求使用同一资源,如演讲会场等,而在同一时间内只有一个活动能使用这一资源。每个活动i 都有一个要求使用该资源的起始时间 si 和一个结束时间 fi ,且si < fi。如果选择了活动i,则它在半开时间区间[sifi)内占用资源。若区间[sifi)与区间[sjfj)不相交,则称活动i与活动j是相容的。也就是说当 si ≥ fj 或 sj ≥ fi 时,活动 i 与活动 j 相容。活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合。

    分析 : 经典的贪心问题,解决办法是根据结束时间安排,然后从头到尾不冲突地一个个选取

    具体证明可以看 ==> http://blog.csdn.net/liufeng_king/article/details/8709005

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e3 + 10;
    struct Activity{
        int s, f;
        bool operator < (const Activity & rhs) const {
            if(this->f == rhs.f) return this->s < rhs.s;
            return this->f < rhs.f;
        };
    }arr[maxn];
    
    int main(void)
    {
        int nCase, n;
        scanf("%d", &nCase);
        for(int Case=1; Case<=nCase; Case++){
            scanf("%d", &n);
            for(int i=0; i<n; i++)
                scanf("%d %d", &arr[i].s, &arr[i].f);
            sort(arr, arr+n);
            int ans = 1;
            int End = arr[0].f;
            for(int i=1; i<n; i++){
                if(arr[i].s >= End){
                    ans++;
                    End = arr[i].f;
                }
            }
            printf("Case %d: %d
    ", Case, ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    matplotlib 画图
    Mac anzhuangxgboost
    scala _ parameter
    cv 验证
    groupie
    pandas map, apply, applymap区别
    画图
    xgboost dmatrix中的 weight的重要性
    自然语言处理的训练范式
    java-处理大容量文本文件,行内分格符为TAB的方法
  • 原文地址:https://www.cnblogs.com/qwertiLH/p/7922323.html
Copyright © 2011-2022 走看看