NOI '22 P5 - Bubble Sort

View as PDF

Submit solution

Points: 35 (partial)
Time limit: 2.0s
Memory limit: 1G

Problem type
Background

Recently, little Z has developed a strong interest in bubble sorting.

Here is the pseudocode for bubble sort:

Copy
Input: a sequence a[1...n] of length n
Output: a result sorted from small to large
  for i = 1 to n do:
    for j = 1 to n - 1 do
      if (a[j] > a[j + 1])
          Swap the values of a[j] and a[j + 1]

The number of swaps in bubble sort is defined as the number of swaps performed during sorting, which is the number of times the sixth line of the above bubble sort pseudocode is executed. He wants to find a sequence with as few exchanges as possible.

Task Description

The sequences studied by little Z consists of non-negative integers. It is of length n and must satisfy m additional conditions.

The i-th condition is: the minimum of the numbers with indices in [L[i],R[i]], namely a[L[i]],a[L[i]+1],,a[R[i]], is exactly V[i].

He knows that bubble sort often times out. So, he wants to know what is the minimum number of swaps for bubble sort among all sequences that satisfy the additional condition.

Input Specification

There are multiple sets of data in this question.

The first line of input contains a positive integer T.

For each set of data, the first row contains two positive integers n, m. Data guarantees 1n,m106.

The next m lines, each with three non-negative integers L[i],R[i],V[i], represent a set of additional conditions. Data guarantees 1L[i]R[i]n,0V[i]109.

Output Specifiction

The output is T lines in total, one integer per line.

For each set of data, if there are sequences that satisfy the m additional conditions, output the minimum number of exchanges in bubble sort among all the sequences that satisfy the additional conditions. If there is no sequence satisfying all conditions, output 1.

Sample Input 1

Copy
1
3 2
1 1 2022
2 3 39

Sample Output 1

Copy
1

Explanation for Sample Output 1

The constraints for this set of data are a[1]=2022,min{a[2],a[3]}=39.

If a[2]=39, and 39a[3]<2022, then the bubble sort has only the first round of swap operations. This round swaps a[1],a[2] and a[2],a[3], and the total number of swaps is 2.

If a[2]=39, and a[3]2022, the bubble sort has only the first round of exchange operations, this round only exchanges a[1],a[2], and the total number of exchanges is 1.

If a[3]=39, and 39<a[2]<2022, the bubble sort algorithm swaps a[1],a[2] and a[2],a[3] in the first round and a[1],a[2] in the second round. The total number of swaps is 3.

If a[3]=39, and a[2]2022, the bubble sort algorithm exchanges a[2],a[3] in the first round, and exchanges a[1],a[2] in the second round. The total number of swaps is 2.

Therefore, the minimum number of swaps is 1.

Additional Samples

Sample inputs and outputs can be found here.

  • Sample 2 (bubble2.in and bubble2.ans).
  • Sample 3 (bubble3.in and bubble3.ans) corresponds to test cases 8-10.
  • Sample 4 (bubble4.in and bubble4.ans) corresponds to test cases 13-14.
  • Sample 5 (bubble5.in and bubble5.ans) corresponds to test cases 15-16.
  • Sample 6 (bubble6.in and bubble6.ans) corresponds to test cases 23-25.

Constraints

There are 25 test cases in this question. All test points satisfy: 1T1000, 1n,m106, 1L[i]R[i]n, 0V[i]109.

CaseConstraintsSpecial Property
1 - 4 n,m7, all but 2 cases have n,m5
5 - 7n,m17, all but 3 cases have n,m9A
8 - 10n,m100, n3,m44107
11, 12n,m2000, n2,m24107
13, 14B
15, 16C
17, 18
19n,m106A
20B
21, 22C
23, 24, 25

where n,m represent the sum of n and m of all test points, respectively. n2,m2,n3,m3 meanings are similar.

  • Special property A: For 1im,0V[i]1.
  • Special property B: L[i]=R[i]for1im.
  • Special property C: The input gives m intervals [L[i],R[i]] Pairs do not intersect.

Hint

Some of the test points in this question have a large amount of input. We recommend that you use the fast I/O.


Comments

There are no comments at the moment.