编程介的小学生 2019-12-06 22:18 采纳率: 0.4%
浏览 109

Double Queue 双队列的问题

Problem Description
The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input
Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output
For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0

Sample Output
0
20
30
10
0

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-29 02:09
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取数据
    input <- readLines()
    
    # 定义函数处理输入数据
    process_input <- function() {
      # 获取请求类型
      req_type <- as.integer(input[[1]])
      
      if(req_type == 1) {
        # 添加新客户到等待列表
        input[[1]] <- paste("1", input[[2]], sep = "")
        
        # 处理后续请求
        process_input()
      } else if(req_type == 2 || req_type == 3) {
        # 检查等待列表是否为空
        if(length(as.numeric(strsplit(input[[1]], " ")[[1]][[1]))) > 0) {
          # 提供服务
          input[[1]] <- paste("2", input[[2]], sep = "")
          
          # 处理后续请求
          process_input()
        } else {
          # 空等待列表
          input[[1]] <- paste("0", sep = "")
        }
      }
    }
    
    # 调用函数处理输入数据
    process_input()
    
    

    这是一个使用 R 语言的简单程序,它接受用户输入并根据特定规则执行操作。此代码示例展示了如何实现双队列问题,并且在遇到空等待列表时会输出零(0)。

    评论

报告相同问题?