• 南阳OJ-14-会场安排问题---区间不相交


    题目链接:

    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=14

    题目描述:

    学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办。小刘的工作就是安排学校小礼堂的活动,每个时间最多安排一个活动。现在小刘有一些活动计划的时间表,他想尽可能的安排更多的活动,请问他该如何安排。

    思路:

    尽可能多的选取区间使得两两不相交。按照右端点排序,然后依次选取第一个右端点,然后除去与之相交的区间,选下一个右端点,重复如此。证明过程见算法竞赛入门经典P232

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 const int maxn = 1e4 + 10;
     8 int T, n;
     9 struct node
    10 {
    11     int x, y;
    12     bool operator < (const node a)const
    13     {
    14         return y < a.y;
    15     }
    16     node(){}
    17     node(double x, double y):x(x), y(y){}
    18 };
    19 node a[maxn];
    20 int main()
    21 {
    22     cin >> T;
    23     while(T--)
    24     {
    25         cin >> n;
    26         for(int i = 0; i < n; i++)
    27         {
    28             cin >> a[i].x >> a[i].y;
    29         }
    30         sort(a, a + n);
    31         int time = 0, ans = 0;
    32         for(int i = 0; i < n; i++)
    33         {
    34             if(a[i].x > time)
    35             {
    36                 time = a[i].y;
    37                 ans++;
    38             }
    39         }
    40         cout<<ans<<endl;
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest E:Black or White
    树状数组--二叉索引树
    P1654 OSU!-洛谷luogu
    P1365 WJMZBMR打osu! / Easy-洛谷luogu
    P4550 收集邮票-洛谷luogu
    P2257 YY的GCD--洛谷luogu

    P3200 [HNOI2009]有趣的数列--洛谷luogu
    catalan数
    lucas定理
  • 原文地址:https://www.cnblogs.com/fzl194/p/8684059.html
Copyright © 2020-2023  润新知