zoukankan      html  css  js  c++  java
  • 简单贪心

    输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。 
    Output对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。Sample Input

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

    Sample Output

    5
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<set>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<algorithm>
    #include<cstdio>
    #include<algorithm>
    #include<functional>
    #include<sstream>
    #define maxn 102
    using namespace std;
    struct Node {
        int l, r;
    } arr[maxn];
    
    bool cmp(Node a, Node b) {
        return a.r < b.r;
    }
    int main()
    {
        int n, i, ans, flag;
        while (scanf("%d", &n), n) {
            for (i = 0; i < n; ++i)
                scanf("%d%d", &arr[i].l, &arr[i].r);
            sort(arr, arr + n, cmp);
            flag = arr[0].r; ans = 1;
            for (i = 1; i < n; ++i)
                if (arr[i].l >= flag) {
                    ++ans; flag = arr[i].r;
                }
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    WebStorm破解方法
    jQuery学习笔记2
    jquery.js 3.0报错, Uncaught TypeError: url.indexOf is not a function
    animate.css –齐全的CSS3动画库--- 学习笔记
    MySQL数据库---表的操作
    MySQL数据库---库的操作
    MySQL数据库---前言
    CppUnit使用和源码解析
    代码覆盖率检查
    代码Verify简介
  • 原文地址:https://www.cnblogs.com/edych/p/7252251.html
Copyright © 2011-2022 走看看