zoukankan      html  css  js  c++  java
  • [优先队列]HDOJ5360 Hiking

    题意:有n个人,每个人有两个参数$l$和$r$

    邀请他们去hiking, 当  当前已经邀请到的人数大于等于$l$,并且小于等于$r$,那么这个人就会去

    问最多能邀请到几个人

    并输出 依次要邀请的人的编号(编号1~n)

    先要按$l$排序($l$小的在前),因为所有$l$小于等于当前已经邀请到的人数的人才能被邀请

    对上述能被邀请的人 要在对$r$排序($r$小的在前), 优先邀请$r$小的

    用优先队列搞一下就好了

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const LL mod=1000000007;
     5 
     6 struct node
     7 {
     8     int l, r;
     9     int id;
    10     friend bool operator < (node a, node b)
    11     {
    12         return a.r>b.r;
    13     }
    14 } a[100005];
    15 
    16 bool cmp(node a, node b)
    17 {
    18     return a.l<b.l;
    19 }
    20 
    21 int ans[100005];
    22 bool vis[100005];
    23 priority_queue<node> q;
    24 int main()
    25 {
    26     int t;
    27     scanf("%d", &t);
    28     while(t--)
    29     {
    30         int n;
    31         scanf("%d", &n);
    32         for(int i=1; i<=n; i++)
    33         {
    34             a[i].id=i;
    35             a[i].l=read();
    36             scanf("%d", &a[i].l);
    37         }
    38         for(int i=1; i<=n; i++)
    39             scanf("%d", &a[i].r);
    40         sort(a+1, a+1+n, cmp);
    41         while(!q.empty()) q.pop();
    42         int cur=1, num=0;
    43         memset(ans, 0, sizeof(ans));
    44         memset(vis, 0, sizeof(vis));
    45         for(int i=0; i<n; i++)
    46         {
    47             while(cur<=n && a[cur].l<=i)
    48                 q.push(a[cur]), cur++;
    49             while(!q.empty() && q.top().r<i)
    50                 q.pop();
    51             if(!q.empty() && q.top().l<=num && q.top().r>=num)
    52                 ans[++num]=q.top().id, vis[q.top().id]=1, q.pop();
    53             else
    54                 break;
    55         }
    56         printf("%d
    ", num);
    57         for(int i=1; i<=n; i++)
    58             if(!vis[i])
    59                 ans[++num]=i;
    60         for(int i=1; i<=n; i++)
    61             printf("%d%c", ans[i], (i==n? '
    ': ' '));
    62     }
    63     return 0;
    64 }
    HDOJ 5360
  • 相关阅读:
    English,The Da Vinci Code, Chapter 23
    python,meatobject
    English,The Da Vinci Code, Chapter 22
    English,The Da Vinci Code, Chapter 21
    English,The Da Vinci Code, Chapter 20
    English,The Da Vinci Code, Chapter 19
    python,xml,ELement Tree
    English,The Da Vinci Code, Chapter 18
    English,The Da Vinci Code, Chapter 17
    English,The Da Vinci Code, Chapter 16
  • 原文地址:https://www.cnblogs.com/Empress/p/4709123.html
Copyright © 2011-2022 走看看