zoukankan      html  css  js  c++  java
  • hdoj2037 今年暑假不AC(贪心)

    题目链接

    http://acm.hdu.edu.cn/showproblem.php?pid=2037

    思路

    想要看的节目尽可能的多,则首先要将节目按照结束时间从早到晚排序,因为一个节目越早结束,留给后面的节目的时间就越多,也就能看到更多的节目。如果按照开始时间从早到晚排序,由于节目可能持续很长时间,所以将会导致最后的结果不准确。举个例子,有三个节目(1,10),(3,5),(6,8),按开始时间从早到晚排序,则只能看1个节目(1,10),而按照结束时间从早到晚排序,则能看2个节目(3,5)和(6,8)。

    代码

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 using namespace std;
     7 
     8 struct Node
     9 {
    10     int s, e;
    11 
    12     Node(int s, int e):s(s), e(e) {}
    13     bool operator < (const Node& node) const
    14     {
    15         return e < node.e;  //将节目按照结束时间从早到晚排序
    16     }
    17 };
    18 vector<Node> v;
    19 
    20 int main()
    21 {
    22     //freopen("hdoj2037.txt", "r", stdin);
    23     int n;
    24     while(cin >> n && n)
    25     {
    26         v.clear();
    27         int s, e;
    28         for(int i=0; i<n; i++)
    29         {
    30             cin >> s >> e;
    31             v.push_back(Node(s, e));
    32         }
    33 
    34         sort(v.begin(), v.end());
    35         int ans = 1;
    36         int t = v[0].e;
    37         for(int i=0; i<n; i++)
    38         {
    39             if(v[i].s >= t)
    40             {
    41                 ans++;
    42                 t = v[i].e;
    43             }
    44         }
    45         cout << ans <<endl;
    46     }
    47     return 0;
    48 }
  • 相关阅读:
    Codeforces Canda Cup 2016
    Codeforces Round #377(div 2)
    后缀数组专题
    Codeforces Round #375(div 2)
    Codeforces Round #374(div 2)
    [HDU5902]GCD is Funny(xjb搞)
    [HDU5904]LCIS(DP)
    HDU 1251统计难题
    POJ2104 K-TH NUMBER 传说中的主席树
    poj 3041
  • 原文地址:https://www.cnblogs.com/sench/p/7998038.html
Copyright © 2011-2022 走看看