首页 > 其他分享 > Luggage Pickup PTA(栈和队列的使用)

Luggage Pickup PTA(栈和队列的使用)

时间:2023-04-21 16:44:57浏览次数:32  
标签:passenger passengers luggage Luggage up PTA queue Pickup line

题目

When a flight arrives, the passengers will go to the Arrivals area to pick up their baggage from a luggage conveyor belt (行李传送带).

Now assume that we have a special airport that has only one pickup window for each conveyor belt. The passengers are asked to line up to pick up their luggage one by one at the window. But if one arrives at the window yet finds out that one is not the owner of that luggage, one will have to move to the end of the queue and wait for the next turn. At the mean time, the luggage at the window will wait until its owner picks it up. Suppose that each turn takes 1 minute, your job is to calculate the total time taken to clear the conveyor belt, and the average waiting time for the passengers.

For example, assume that luggage i belongs to passenger i. If the luggage come in order 1, 2, 3, and the passengers come in order 2, 1, 3. Luggage 1 will wait for 2 minutes to be picked up by passenger 1. Then the luggage queue contains 2 and 3, and the passenger queue contains 3 and 2. So luggage 2 will wait for 2 minutes to be picked up by passenger 2. And finally 3 is done at the 5th minute. The average waiting time for the passengers is (2+4+5)/3≈3.7.

输入格式

Each input file contains one test case. The first line gives a positive integer N (≤10
3
). Then N numbers are given in the next line, which is a permutation of the integers in [1,N], representing the passenger queue. Here we are assuming that the luggage queue is in order of 1, 2, ..., N, and the i-th luggage belongs to passenger i.

All the numbers in a line are separated by a space.

输出格式

For each test case, print in a line the total time taken to clear the conveyor belt, and the average waiting time for the passengers (output 1 decimal place). The numbers must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.

输入样例

5
3 5 1 2 4

输出样例

9 6.0

代码

#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
int main() {
    int i, j, k, m, n;
    cin >> m;
    vector<int> v;
    queue<int> q;
    for (i = 0; i < m; i++) {
        cin >> n;
        v.push_back(n);
        q.push(n);
    }
    int sum = 0, count = 0;
    for (i = 1; i <= m; i++) {
        n = q.front();
        int cnt = 0;
        while (n != i) {
            q.push(q.front());
            cnt++;
            q.pop();
            n = q.front();
        }
        q.pop();
        count += cnt + 1;
        sum += (m - i + 1) * (cnt+1);
    }
    printf("%d %.1f", count, sum / (float)m);
    return 0;
}

标签:passenger,passengers,luggage,Luggage,up,PTA,queue,Pickup,line
From: https://www.cnblogs.com/index-12/p/17340934.html

相关文章

  • 迁移学习(PAT)《Pairwise Adversarial Training for Unsupervised Class-imbalanced Dom
    论文信息论文标题:PairwiseAdversarialTrainingforUnsupervisedClass-imbalancedDomainAdaptation论文作者:WeiliShi,RonghangZhu,ShengLi论文来源:KDD2022论文地址:download 论文代码:download视屏讲解:click1摘要提出问题:类不平衡问题;解决方法:提出了一......
  • linux IPtable防火墙 禁止和开放端口
    评:1、关闭所有的INPUTFORWARDOUTPUT只对某些端口开放。下面是命令实现:iptables-PINPUTDROPiptables-PFORWARDDROPiptables-POUTPUTDROP再用命令iptables-L-n查看是否设置好,好看到全部DROP了这样的设置好了,我们只是临时的,重启服务器还是会恢复......
  • CentOS linux关闭iptables防火墙
    评:linux服务器下防火墙为iptables组件,在安装一些软件的时候,iptables防火墙会阻止我们一些必要的连接,所以,我代购的美国服务器,荷兰服务器等海外服务器iptables初始状态为关闭。如果有一些服务器没有关闭iptables,并且你还特别想关闭它,哪么以下命令,你可以能用的上。查看iptables状......
  • K8s为啥要启用bridge-nf-call-iptables内核参数?用案例给你讲明白
    使用kubernetes遇到最多的70%问题都可以归于网络问题,最近发现如果内核参数:bridge-nf-call-iptables设置不当的话会影响kubernetes中Node节点上的Pod通过ClusterIP去访问同Node上的其它pod时会有超时现象,复盘记录一下排查的前因后因。1、问题现象集群环境为K8sv......
  • 第六天练习(学习PTA题目的标准答案以及复习string函数知识)
    #include<iostream>#include<string>usingnamespacestd;boolcheck(strings){intp_pos=-1,t_pos=-1;intp_count=0,t_count=0;for(inti=0;i<s.size();i++){if(s[i]=='P'){i......
  • iptables命令常用规则汇总
    iptables非常强大,但是参数选项多,学习成本较高。本文将常用的iptables的命令进行汇总,在关键时刻方便拿来即用。iptables的四表五链iptables的四表五链是指iptables中的四个表和五个链。四个表分别是:filter表:用于过滤数据包,控制网络流量。nat表:用于对数据包进行地址转换,实现......
  • 打卡 pta c++ 汽车收费
    现在要开发一个系统,管理对多种汽车的收费工作。给出下面的一个基类框架classVehicle{protected:stringNO;public:Vehicle(stringn){NO=n;}virtualintfee()=0;//计算应收费用};以Vehicle为基类,构建出Car、Truck和Bus三个类。Car的收费公式为:载客数*8+重量*2......
  • pta程序设计辅助平台-练习
    现在要开发一个系统,管理对多种汽车的收费工作。给出下面的一个基类框架classVehicle{protected:stringNO;public:Vehiclvirtualintfee()=0;//计算应收费用};以Vehicle为基类,构建出Car、Truck和Bus三个类。Car的收费公式为:载客数*8+重量*2Truck的收费公式为:重量*5Bus的收费......
  • firewalld 和 iptables 区别
    在RHEL7里有几种防火墙共存:firewalld、iptables、ebtables,默认是使用firewalld来管理netfilter子系统,不过底层调用的命令仍然是iptables等。firewalld跟iptables比起来至少有两大好处:1、firewalld可以动态修改单条规则,而不需要像iptables那样,在修改了规则后必须得全部刷新才可以......
  • CentOS 7 :Failed to start IPv4 firewall with iptables
    关闭firewalld:systemctlstopfirewalldsystemctlmaskfirewalld使用iptables服务:#开放443端口(HTTPS)iptables-AINPUT-ptcp--dport443-jACCEPT#保存上述规则serviceiptablessave#开启服务systemctlrestartiptables.service一切正常。......