Atcoder Weekday Contest 054 E題(有序容器)

第一次使用Atcoder官方允許導(dǎo)入的Ruby第三方gem sorted_containers解答此題
E - Library Bookshelf Guide
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 433 pts

Problem Statement
Takahashi is working part-time at a library. The library has a bookshelf with N storage spaces arranged in a single row from left to right, and each storage space can hold one book. The storage spaces are numbered 1,2,…,N from left to right. Initially, all storage spaces are empty.

Today, Takahashi is in charge of placing M newly arrived books on the bookshelf. The storage space number where each book should be placed is predetermined: the book with placement order i (1≤i≤M) is placed in storage space Si . No two books are placed in the same storage space. That is, S1,S2,…,SM are all distinct values. Takahashi places the books one by one on the bookshelf in the order of the 1st book, the 2nd book, …, the M-th book.

To make the task more efficient, Takahashi wants to know in advance, when placing each book, "among the storage spaces that are currently empty at that moment, what is the position from the left of the storage space where this book should be placed?"

Specifically, just before placing the i-th book, if we number the currently empty storage spaces
1,2,… from left to right, storage space Si is the Pi -th from the left. Find Pi for each i=1,2,…,M.

Constraints
1≤M≤N≤2×10**5
1≤Si ≤N
All Si are distinct
All input values are integers

Input
N M
S1 S2…SM
The first line contains an integer N representing the total number of storage spaces and an integer M representing the number of books to be placed, separated by a space.
The second line contains the storage space numbers S1,S2,…,SM where each book should be placed, separated by spaces.Si represents the storage space number for the book with placement order i.

Output
P1
P2
?
PM
Output M lines.On the i-th line, output an integer Pi representing the position of storage space Si when counting the empty storage spaces from left to right just before placing the i-th book.
我的解

require "sorted_containers"
n, m = gets.split.map(&:to_i)
s = gets.split.map(&:to_i)
list = SortedContainers::SortedArray.new
(1..n).each do |x|
  list << x
end
s.each do |y|
  pos = list.bisect_left(y)
  puts pos + 1
  z = list[pos]
  list.delete(z)
end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • The Weight of Mountains and the Measure of a Man Chapter ...
    Ross_8d11閱讀 56評(píng)論 0 0
  • Bye, Lonely man Chapter 1 “Look at the mess you made!” A ...
    成鋼他會(huì)改的閱讀 97評(píng)論 0 0
  • """1.個(gè)性化消息: 將用戶的姓名存到一個(gè)變量中,并向該用戶顯示一條消息。顯示的消息應(yīng)非常簡(jiǎn)單,如“Hello ...
    她即我命閱讀 5,843評(píng)論 0 6
  • 一、工具箱(多種工具共用一個(gè)快捷鍵的可同時(shí)按【Shift】加此快捷鍵選取)矩形、橢圓選框工具 【M】移動(dòng)工具 【V...
    墨雅丫閱讀 1,824評(píng)論 0 0
  • 跟隨樊老師和伙伴們一起學(xué)習(xí)心理知識(shí)提升自已,已經(jīng)有三個(gè)月有余了,這一段時(shí)間因?yàn)樘鞖獾脑蛐菡n,順便整理一下之前學(xué)習(xí)...
    學(xué)習(xí)思考行動(dòng)閱讀 1,189評(píng)論 0 2

友情鏈接更多精彩內(nèi)容