zoukankan      html  css  js  c++  java
  • 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门

     1 /*
     2     题意:求邀请顺序使得去爬山的人最多,每个人有去的条件
     3     贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能                 被邀请,而且不用考虑最少人数的条件。网上的写的比我简洁,学习了。
     4     详细解释:http://blog.csdn.net/queuelovestack/article/details/47319361
     5 */
     6 /************************************************
     7 * Author        :Running_Time
     8 * Created Time  :2015-8-6 14:51:47
     9 * File Name     :H.cpp
    10  ************************************************/
    11 
    12 #include <cstdio>
    13 #include <algorithm>
    14 #include <iostream>
    15 #include <sstream>
    16 #include <cstring>
    17 #include <cmath>
    18 #include <string>
    19 #include <vector>
    20 #include <queue>
    21 #include <deque>
    22 #include <stack>
    23 #include <list>
    24 #include <map>
    25 #include <set>
    26 #include <bitset>
    27 #include <cstdlib>
    28 #include <ctime>
    29 using namespace std;
    30 
    31 #define lson l, mid, rt << 1
    32 #define rson mid + 1, r, rt << 1 | 1
    33 typedef long long ll;
    34 const int MAXN = 1e5 + 10;
    35 const int INF = 0x3f3f3f3f;
    36 const int MOD = 1e9 + 7;
    37 struct Node {
    38     int l, r, id;
    39     bool operator < (const Node &x) const {
    40         return r > x.r;
    41     }
    42 }node[MAXN];
    43 priority_queue<Node> Q;
    44 bool vis[MAXN];
    45 int ans[MAXN];
    46 int n;
    47 
    48 bool cmp(Node x, Node y)    {
    49     if (x.l == y.l) return x.r < y.r;
    50     return x.l < y.l;
    51 }
    52 
    53 int main(void)    {     //HDOJ 5360 Hiking
    54     int T;  scanf ("%d", &T);
    55     while (T--) {
    56         scanf ("%d", &n);
    57         for (int i=1; i<=n; ++i)    {
    58             scanf ("%d", &node[i].l);   node[i].id = i;
    59         }
    60         for (int i=1; i<=n; ++i)    {
    61             scanf ("%d", &node[i].r);
    62         }
    63         sort (node+1, node+1+n, cmp);
    64 
    65         if (node[1].l != 0) {
    66             puts ("0");
    67             for (int i=1; i<=n; ++i)    printf ("%d%c", i, (i==n) ? '
    ' : ' ');
    68             continue;
    69         }
    70         memset (vis, false, sizeof (vis));
    71         while (!Q.empty ()) Q.pop ();
    72         int i = 1, p = 0;
    73         for (; i<=n && p==node[i].l; ++i)   Q.push (node[i]);
    74         while (!Q.empty ()) {
    75             while (!Q.empty ()) {
    76                 if (p <= Q.top ().r)    {
    77                     ans[++p] = Q.top ().id; 
    78                     vis[ans[p]] = true;    Q.pop ();   break;
    79                 }
    80                 else Q.pop ();
    81             }
    82             for (; i<=n && p==node[i].l; ++i)   Q.push (node[i]);
    83         }
    84         
    85         printf ("%d
    ", p);
    86         printf ("%d", ans[1]);
    87         for (int i=2; i<=p; ++i)    {
    88             printf (" %d", ans[i]);
    89         }
    90         for (int i=1; i<=n; ++i)    {
    91             if (!vis[i])    printf (" %d", i);
    92         }
    93         puts ("");
    94     }
    95 
    96     return 0;
    97 }
    编译人生,运行世界!
  • 相关阅读:
    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
    Visual Studio断点调试, 无法监视变量, 提示无法计算表达式
    ASP.NET MVC中MaxLength特性设置无效
    项目从.NET 4.5迁移到.NET 4.0遇到的问题
    发布网站时应该把debug设置false
    什么时候用var关键字
    扩展方法略好于帮助方法
    在基类构造器中调用虚方法需谨慎
    ASP.NET MVC中商品模块小样
    ASP.NET MVC中实现属性和属性值的组合,即笛卡尔乘积02, 在界面实现
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4709048.html
Copyright © 2011-2022 走看看