시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
5 초 | 512 MB | 39 | 20 | 17 | 68.000% |
Bubble sort is an algorithm to sort a sequence. Let’s say we are going to sort a sequence A0, A1, . . . , AN−1 of length N in non-decreasing order. Bubble sort swaps two adjacent numbers when they are not in the correct order. Swaps are done by repeatedly passing through the sequence. Precisely speaking, in a pass, we swap Ai and Ai+1 if Ai > Ai+1, for i = 0, 1, . . . , N − 2 in this order. It is known that any sequence can be sorted in non-decreasing order by some passes. For a sequence A, we define the number of passes by bubble sort as the number of passes needed to sort A using the above algorithm.
JOI-kun has a sequence A of length N. He is going to process Q queries of modifying values of A. To be specific, in the (j + 1)-th query (0 ≤ j ≤ Q − 1), the value of AXj is changed into Vj.
JOI-kun wants to know the number of passes by bubble sort for the sequence after processing each query.
4 2 1 2 3 4 0 3 2 1
1 2
11 12 11 4 13 6 7 3 5 12 4 10 11 8 11 4 4 6 20 0 2 7 2 3 18 5 9 0 6 8 8 9 4 0 8 6 18
5 5 5 4 6 6 6 7 7 7 7 7
Given a sequence A = {1, 2, 3, 4} of length N = 4 and Q = 2 queries: X = {0, 2}, V = {3, 1}.
Bubble sort for A = {3, 2, 3, 4}:
Hence, the number of passes by bubble sort is 1 for A = {3, 2, 3, 4}.
Bubble sort for A = {3, 2, 1, 4}:
Hence, then number of passes by bubble sort is 2 for A = {3, 2, 1, 4}.
Contest > JOI Open Contest > JOI Open Contest 2018 1번
Olympiad > International Olympiad in Informatics > IOI 2018 > Practice 1번