Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*
- import java.util.*
- val reader = BufferedReader(InputStreamReader(System.`in`))
- val writer = PrintWriter(System.out)
- fun solve() {
- val n = reader.readLine()!!.toInt()
- var arr = Array(n) { IntArray(3) }
- for (i in 0 until n) {
- var tk = StringTokenizer(reader.readLine())
- arr[i][0] = tk.nextToken()!!.toInt()
- arr[i][1] = tk.nextToken()!!.toInt()
- arr[i][2] = i + 1
- }
- Arrays.sort(arr, object : Comparator<IntArray> {
- override fun compare(o1: IntArray, o2: IntArray): Int {
- if (o1[0] == o2[0])
- return Integer.compare(o1[1], o2[1])
- return Integer.compare(o1[0], o2[0])
- }
- })
- var set = TreeSet<Int>()
- var map = HashMap<Int, ArrayList<Int>>()
- for (i in 0 until n) {
- for (adding in -1..1) {
- for (j in 0..1) {
- set.add(arr[i][j] + adding)
- if (map[arr[i][j] + adding] == null)
- map[arr[i][j] + adding] = ArrayList()
- }
- }
- map[arr[i][0]]!!.add(arr[i][2])
- map[arr[i][1]]!!.add(-arr[i][2])
- }
- var list = ArrayList<Int>()
- var tmp = 0
- for (x in set) {
- if (map[x] != null)
- for (values in map[x]!!) {
- if (values > 0) {
- list.add(values)
- tmp++
- } else {
- tmp--
- }
- }
- if (tmp == 0 && list.size > 0) {
- break
- }
- }
- if (list.size != n) {
- var ans = Array(n) { 1 }
- for (values in list) {
- ans[values - 1] = 2
- }
- for (values in ans) {
- writer.print("$values ")
- }
- writer.print("\n")
- } else {
- writer.print("-1\n")
- }
- }
- fun main() {
- var t = readLine()!!.toInt()
- while (t-- > 0) {
- solve()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement