Home
Posts
Categories
Series
Tags
About
Sorting Problem
with java
postedOn: 2024-3-29   updatedOn: 2024-3-29   includedIn: Algorithms
wordsCount: 393   readingTime: 2 mins   viewers:

Problem

Jay likes sequences, so he use to playing with a sequence of size N. He will transform the sequence according to K queries, and the format and process of the queries are as follows:

  • L R X: Let’s say the sequence sorted in ascending order is A[1], A[2], …, A[N].
  • First, add X to A[L], A[L+1], …, A[R].
  • Then, sort the sequence again in ascending order.

Print the sequence after performing all the queries in order.

Input

tip The first line is given with N and K separated by a space. (1 ≤ N ≤ 100000, 1 ≤ K ≤ 1000)
The second line is given with N integers that make up the sequence, each having an absolute value of less than or equal to 10^18.
From the third line to the (K+2)th line, the queries L R X are given. (1 ≤ L ≤ R ≤ N, |X| ≤ 10^9)

Output

tip Print the sequence after performing all the queries in order.


Example: (tip)

\(Input:  
7 3  
1 2 3 4 5 6 7  
1 6 3  
2 7 -4  
4 5 6  

Output: 
1 2 3 4 5 9 10\)

How to Approach?

Though there is no single correct solution, there are effective ones. I usally take follow steps:

  1. we need a input function
  2. validation function to prove user input proper or not
  3. print function to print out our output
  4. do function (This can be further divided into several actions)
  5. (if we need)calculate function

step1. Input function

First we need input function. In java, there exist some other input method but I usually use this method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public static void getUserInput(ArrayList<Long> sequence, int[] nk) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        nk[0] = Integer.parseInt(st.nextToken()); // N
        nk[1] = Integer.parseInt(st.nextToken()); // K
        
        st = new StringTokenizer(br.readLine()); // Tokenizing the sequence line
        while (st.hasMoreTokens()) {
            sequence.add(Long.parseLong(st.nextToken()));
        }
    }

By using BufferedReader, you can quickly get input values.

step2. validation function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
n, k = map(int, input().split())
sequence = list(map(int, input().split()))

while(k > 0):
    l, r, x = map(int, input().split())
    for i in range(l, r+1):
        sequence[i] += x
    sequence.sort()
    k -= 1

print(*sequence)
Aaron Oh
고려대코딩개발협동조합 창단 멤버
Table of Contents
Related Posts
How to Sort Data
Introduction What this post focus on This posting will cover almost every concept of How to sort (primary). Plus, I’ll post realistic algorithm problem of our real world so that you can apply those concept to real world problem and learn how to use it. I really hope so. Let’s see the below example.
2024-3-29
High Score Kit: Sorting#3
Problem The H-Index is a metric that measures the productivity and impact of a scientist’s research. It represents the highest number h such that the scientist has published h papers that have each been cited at least h times. According to Wikipedia, the H-Index is calculated as follows: For a given scientist who has published n papers, if h of these papers have at least h citations each, and the other papers have no more than h citations each, then h is the maximum value for this scientist’s H-Index.
2024-4-10
High Score Kit: Sorting#2
Problem Given an array of non-negative integers, your task is to determine the largest number that can be formed by concatenating the integers together. For example, with the integers [6, 10, 2], you can form the numbers [6102, 6210, 1062, 1026, 2610, 2106] through different arrangements. The largest number from these combinations is 6210.
2024-4-10
High Score Kit: Sorting#1
Problem To find a number at the k-th position after slicing and sorting the array from the i-th to j-th number, consider the following: For example, if the array is [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, and k = 3, slicing the array from the 2nd to the 5th number yields [5, 2, 6, 3].
2024-4-10
tuple
Problem A “tuple” is defined as an ordered collection of elements which can include duplicate values and each element is assigned an order. A tuple with n elements (a1, a2, a3, ..., an) can be expressed using the set notation in various ways, with any ordering of the subsets as:
2024-5-1
braket rotating
Problem Define a string as a “valid bracket string” if it follows the given rules: “()”, “[]”, and “{}” are all valid bracket strings. If A is a valid bracket string, then “(A)”, “[A]”, and “{A}” are also valid bracket strings. For example, since “[]” is a valid bracket string, “([])” is also valid.
2024-4-27