• 公交车栈和队列应用(CodeForces 982B)


    Description

    在角色巴士中有n排座位,每排都有2个座位。 第i排的两个座位的宽度均为w i厘米。 没有相同宽度的座椅。

    公共汽车最初是空的。 每个2n站都会有一位乘客进入巴士。 有两种类型的乘客:

    • 一个内向者总是选择两个座位都没人的一排。 在这些排中,他选择座位宽度最小的,并占据了其中的一个座位;
    • 一个外向型的人总是选择有人的一排。 在这些排中,他选择了座位宽度最大的那个,并占据了空位。

    你会得到每排座位的宽度和乘客进入公共汽车的顺序。 确定每位乘客将乘坐哪一排。

    Input

    第一行包含一个整数n(1 ≤ n ≤ 200000) - 总排数。

    第二行包含整数w 1,w 2,...,w n(1 ≤ w i ≤ 10 9)的序列,其中wi是第i行中每个座位的宽度。 保证所有 w i 都不同。

    第三行包含一个长度为 2n 的字符串,由数字“0”和“1”组成 - 描述乘客进入公共汽车的顺序。 如果第j个字符是 '0',那么在第 j 个车站进入公共汽车的乘客是内向的。 如果第j个字符是 '1',则在第j个车站进入公交车的乘客是外向型的。 保证外向者的数量等于内向者的数量(即两个数字等于 n),并且对于每个外向者总是有合适的行。

    Output

    打印 2n 个整数 - 乘客将坐的排。 乘客的顺序应与输入的顺序相同。

    Sample Input

    Input

    2
    3 1
    0011
    

    Output

    2 1 1 2 

    Input

    6
    10 8 9 11 13 5
    010010011101
    

    Output

    6 6 2 3 3 1 4 4 1 2 5 5 
    

    Hint

    在第一个例子中,第一个乘客(内向)选择第二排,因为它具有最小宽度的座位。 第二位乘客(内向)选择第1行,因为它现在是唯一的空行。 第三位乘客(外向型)选择第一排,因为它只有一个占用的座位,座位宽度是这些排中最大的。 第四位乘客(外向性)选择第二排,因为它是唯一一个有空位的排。

    #include<iostream>
    #include <deque>
    #include<stack>
    #include<queue>
    #include<math.h>
    #include<algorithm>
    #include<stdlib.h>
    
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    struct W
    {
        int num,wi;
    }a[200200];
    int cmp(W a,W b)
    {
        return a.wi<b.wi;
    }
    queue<W> qn;
    stack<W> qw;
    int main()
    {
        int n,i,j,k;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i].wi);
            a[i].num=i;
        }
        getchar();
         sort(a+1,a+n+1,cmp);
         for(i=1;i<=n;i++)
         {
    
             qn.push(a[i]);
         }
         n=n*2;
         while(n--)
         {
             int c;
             W t;
             c=getchar();
             if(c=='0')
             {
                 t=qn.front();
                 qw.push(t);
                 qn.pop();
                 printf("%d ",t.num);
             }
             if(c=='1')
             {
                 t=qw.top();
                 qw.pop();
                 printf("%d ",t.num);
             }
    
         }
    }<iostream>
    #include <deque>
    #include<stack>
    #include<queue>
    #include<math.h>
    #include<algorithm>
    #include<stdlib.h>
    
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    struct W
    {
        int num,wi;
    }a[200200];
    int cmp(W a,W b)
    {
        return a.wi<b.wi;
    }
    queue<W> qn;
    stack<W> qw;
    int main()
    {
        int n,i,j,k;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i].wi);
            a[i].num=i;
        }
        getchar();
         sort(a+1,a+n+1,cmp);
         for(i=1;i<=n;i++)
         {
    
             qn.push(a[i]);
         }
         n=n*2;
         while(n--)
         {
             int c;
             W t;
             c=getchar();
             if(c=='0')
             {
                 t=qn.front();
                 qw.push(t);
                 qn.pop();
                 printf("%d ",t.num);
             }
             if(c=='1')
             {
                 t=qw.top();
                 qw.pop();
                 printf("%d ",t.num);
             }
    
         }
    }
  • 相关阅读:
    从底层来看Promise
    Promise初探
    时间复杂度和空间复杂度
    IDEA maven Run Maven 启动方式
    IDEA 运行maven工程报错:No goals have been specified for this build.....解决办法
    12.外键约束
    11.约束的概念及分类
    10.创建表-备注与创建表的细节说明。
    9.创建表-自增长(AUTO_INCREAMENT)
    8.创建表--主键的概念与应用
  • 原文地址:https://www.cnblogs.com/jk17211764/p/9677412.html
Copyright © 2020-2023  润新知