Решение
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.util.StringTokenizer
import kotlin.math.min
import kotlin.math.max
import kotlin.collections.MutableList
import kotlin.collections.ArrayList
import kotlin.collections.sortWith
import kotlin.Comparator
// Data class to hold normalized pallet dimensions (min, max)
data class Pallet(val minDim: Int, val maxDim: Int)
fun main(args: Array<String>) {
val reader = BufferedReader(InputStreamReader(System.`in`))
val writer = BufferedWriter(OutputStreamWriter(System.out))
// Read the number of pallets
val n = reader.readLine().toInt()
// Create a list to store pallets with normalized dimensions
val pallets: MutableList<Pallet> = ArrayList(n)
// Read pallet dimensions, normalize, and add to the list
for (i in 0 until n) {
val st = StringTokenizer(reader.readLine())
val w = st.nextToken().toInt()
val h = st.nextToken().toInt()
// Normalize: store as (min_dimension, max_dimension)
pallets.add(Pallet(min(w, h), max(w, h)))
}
// Sort the pallets:
// 1. Primarily by min_dimension ascending.
// 2. Secondarily by max_dimension *descending*.
// This grouping ensures that for the same min_dimension, the pallet
// with the largest max_dimension comes first.
pallets.sortWith(Comparator { p1, p2 ->
if (p1.minDim != p2.minDim) {
p1.minDim.compareTo(p2.minDim)
} else {
// Note: Descending order for maxDim
p2.maxDim.compareTo(p1.maxDim)
}
})
var countUnstackable = 0
var maxDimSeenSoFar = 0 // Tracks the maximum maxDim encountered in the processed suffix
// Iterate backwards through the sorted list
// A pallet `p_i` cannot be placed on any other pallet `p_j` if there is no `p_j`
// such that minDim(p_i) < minDim(p_j) and maxDim(p_i) < maxDim(p_j).
// By iterating backwards and tracking the maximum `maxDim` seen so far (`maxDimSeenSoFar`),
// if the current pallet's `maxDim` is greater than or equal to `maxDimSeenSoFar`,
// it means no pallet *after* it in the sorted list (which are the only candidates
// to have a strictly larger `minDim`) has a strictly larger `maxDim`.
// Due to the sort order, it also cannot be placed on pallets with the same `minDim`
// that appear later in the list (as they have smaller or equal `maxDim`), nor can it
// be placed on any pallet appearing *earlier* in the list (as they have smaller or equal `minDim`).
for (i in n - 1 downTo 0) {
val currentPallet = pallets[i]
// If current pallet's maxDim is >= maxDim of all pallets processed so far (from the end)
// then this pallet cannot be placed on any pallet that comes *after* it in the sorted list.
if (currentPallet.maxDim >= maxDimSeenSoFar) {
countUnstackable++
// Update the maximum maxDim seen in the suffix processed so far
maxDimSeenSoFar = currentPallet.maxDim
}
// Note: even if we don't count the pallet, we still consider its maxDim for future checks,
// but since we only update `maxDimSeenSoFar` when counting, we effectively only compare
// against the maximum dimensions of potentially unstackable pallets found so far in the suffix.
// Let's correct this: we need the overall max dim seen so far in the suffix.
// Correction: We must update maxDimSeenSoFar regardless of counting.
// The condition `currentPallet.maxDim >= maxDimSeenSoFar` should compare with the max seen *before* processing this pallet.
// Let's rethink the update logic slightly.
}
// --- Re-evaluation of the backward pass logic ---
countUnstackable = 0 // Reset count
maxDimSeenSoFar = 0 // Reset max dim tracker
// Iterate backwards
for (i in n - 1 downTo 0) {
val currentPallet = pallets[i]
// Check if the current pallet's maxDim is greater than the
// maximum maxDim encountered among pallets STRICTLY AFTER it
// in the sorted list.
// Since we iterate backwards, `maxDimSeenSoFar` holds the max maxDim
// from index i+1 to n-1.
if (currentPallet.maxDim >= maxDimSeenSoFar) {
// This pallet cannot fit onto any pallet p_j where j > i, because
// for all j > i, either minDim_j < minDim_i (not possible by sort)
// or minDim_j == minDim_i (and by sort maxDim_j <= maxDim_i)
// or minDim_j > minDim_i (but maxDim_j <= maxDimSeenSoFar <= maxDim_i).
// So the condition maxDim_i < maxDim_j is never met.
// Pallet p_i also cannot fit onto any p_k where k < i because minDim_k <= minDim_i.
countUnstackable++
}
// Update the maxDimSeenSoFar for the next iteration (for index i-1)
// to include the current pallet's maxDim.
maxDimSeenSoFar = max(maxDimSeenSoFar, currentPallet.maxDim)
}
// Write the result
writer.write(countUnstackable.toString())
writer.newLine()
// Close resources
reader.close()
writer.close()
}