zoukankan      html  css  js  c++  java
  • 活动选择问题 贪心

                                                                            

    活动选择问题

    题目描述

     sdut 大学生艺术中心每天都有n个活动申请举办,但是为了举办更多的活动,必须要放弃一些活动,求出每天最多能举办多少活动。

    输入

     输入包括多组输入,每组输入第一行为申请的活动数n,从第2行到n+1行,每行两个数,是每个活动的开始时间b,结束时间e;

    输出

     输出每天最多能举办的活动数。

    示例输入

    12
    15 20
    15 19
    8 18
    10 15
    4 14
    6 12
    5 10
    2 9
    3 8
    0 7
    3 4
    1 3

    示例输出

    5

    #include<stdio.h>
    struct tv{
        int start, end;
    }a[100], b;
    void quick_sort(tv s[], int l, int r){
        if(l < r){
            int i=l, j=r, x=s[l].end;
            b = s[l];
            while(i < j){
                while(i < j && s[j].end >= x)  j--;
                if(i < j)  s[i++] = s[j];
                while(i < j && s[i].end < x)  i++;
                if(i < j)  s[j--] = s[i];
            }
            s[i] = b;
            quick_sort(s, l, i-1);
            quick_sort(s, i+1, r);
        }
    }
    int result(int select[], int n){
        int ans=0;
        for(int i=0; i<n; i++)
          if(select[i] == 1)
            ans++;
        return ans;
    }
    int main(){
        int n;
        while(scanf("%d", &n)!=EOF){
            int select[100], i, Time = 0;
            for(i=0; i<n; i++)
                scanf("%d%d", &a[i].start, &a[i].end);
            for(i=0; i<n; i++)  select[i] = 0;
            quick_sort(a, 0, n-1);
            i=0;
            while(i<n){
                if(a[i].start >= Time){
                    select[i] = 1;
                    Time = a[i].end;
                }
                i++;
            }
            printf("%d
    ", result(select, n) );
        }
        return 0;
    }
    



  • 相关阅读:
    iOS-Core-Animation-Advanced-Techniques(一)
    vue 路由
    Vue 生产环境部署
    vue 单文件组件
    vue 插件
    Vue 混合
    vue 自定义指令
    vue render函数 函数组件化
    vue render函数
    vue 过渡状态
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/8304810.html
Copyright © 2011-2022 走看看