zoukankan      html  css  js  c++  java
  • 离散化题目:人潮最多的時段( Interval Partitioning Problem )

    题目:一群访客参加宴会,我们询问到每一位访客的进场时刻与出场时刻(即:已知访客的进场时刻与出场时刻),请问宴会现场挤进最多人的时段。

    这个问题的关键不是访客,而是出入时刻。

     1 struct Guest { int arrival; int leave; } g[7] = { { 10, 12 }, { 12, 14 }, { 12, 13 }, { 9, 15 }, {13, 15 }, { 14, 15 }, {15,15} };
     2 
     3 bool cmp(const int& i, const int& j)
     4 {
     5     return abs(i) < abs(j);
     6 }
     7 
     8 int maximum_guest()
     9 {//返回宾客最多的人数
    10     
    11     vector<int> time;
    12     for (int i = 0; i<7; ++i)
    13     {
    14         time.push_back(+g[i].arrival);
    15         time.push_back(-g[i].leave);
    16     }
    17 for (auto&u : time)
    18     {
    19         cout << u << " ";
    20     }
    21     sort(time.begin(), time.end(), cmp);
    22     cout << endl;
    23     for (auto&u : time)
    24     {
    25         cout << u << " ";
    26     }
    27     int n = 0, maximum = 0;
    28     int i;
    29     for ( i = 0; i<time.size(); ++i)
    30     {
    31         if (time[i] >= 0)
    32             n++;
    33         else
    34             n--;
    35 
    36         maximum = max(maximum, n);
    37     }
    38 
    39     cout<<endl << "人潮最多的時段有" << maximum << "";
    40 
    41     return maximum;
    42 }
    43 
    44 void max_time()
    45 {//问题主函数
    46     int maximum = maximum_guest();
    47     vector<int> time;
    48     for (int i = 0; i<7; ++i)
    49     {
    50         time.push_back(+g[i].arrival);
    51         time.push_back(-g[i].leave);
    52     }
    53     sort(time.begin(), time.end(), cmp);
    54     int n = 0;
    55     int flag = 1;
    56     int temp;
    57     for (int i = 0; i<time.size(); ++i)
    58     {
    59         if (time[i] >= 0)
    60             n++;
    61         else
    62             n--;
    63 
    64         if (n == maximum)
    65         {
    66             temp = i;
    67             flag = 0;
    68         }
    69         if (flag == 0 && n == maximum - 1)
    70         {
    71             cout << endl << "time:" << time[temp] << "to" << time[i];
    72         }
    73     }
    74 }
    75 
    76 int main()
    77 {
    78     max_time();
    79     getchar();
    80     return 0;
    81 }

    转载请注明:http://www.cnblogs.com/firstcxj/p/4566385.html

  • 相关阅读:
    koa学习
    nodejs工作大全
    《程序员周先生之前端开发面试题》
    使用vue技术应当使用的技术和兼容性选择
    IdentityServer4简单入门demo系列 (一)认证服务端
    IdentityServer4客户端获取Token的方法
    wpf 右键菜单的使用
    wpf 在用户控件里,关掉父级窗口
    EntityFramework集成Sqlite的详细步骤
    wpf DataGrid 里的列模版的值绑定
  • 原文地址:https://www.cnblogs.com/firstcxj/p/4566385.html
Copyright © 2011-2022 走看看