PCB

시간 제한1초메모리 제한2048 MB

요약
왼쪽 변의 전원 n개와 내부의 소비자 n개를 서로 교차하지 않는 L자 전선으로 연결해 전체 전선 길이의 합을 최소로 만든다.
난이도

어려움10점 중 8점

유형
그리디, 정렬, 기하, 동적 계획법
정답자
아직 제출이 없습니다

문제

In designing a printed circuit board (PCB), each consumer must be connected to a power supply via conductive wires. The PCB is a rectangle of width WW and height HH. It is represented as a grid of integer coordinates from (0,0)(0, 0) to (W+1,H+1)(W + 1, H + 1).

There are nn power supplies along the left edge of the board and nn consumers each located somewhere inside the board. The iith power supply is located at position (0,h_i)(0, h\_i) and the iith consumer is located at position (x_i,y_i)(x\_i , y\_i). Each power supply must connect to exactly one consumer and vice versa.

Each wire must run along the grid lines, bending at most once. i.e., each wire is either a straight vertical or horizontal line or makes exactly one 9090-degree turn, forming an "L" shape. Wires cannot cross or overlap with each other anywhere along their paths.

Your task is to determine a matching between power supplies and consumers such that the total length of all wires is minimized.

입력

The input consists of several lines:

  • The first line contains three integers WW, HH and nn (1≤W,H≤1081 \le W, H \le 10^8; 1≤n≤1061 \le n \le 10^6).
  • Each of the next nn lines contains an integer h_ih\_i (1≤h_i≤H1 \le h\_i \le H).
  • Each of the next nn lines contains two integers x_ix\_i and y_iy\_i (1≤x_i≤W1 \le x\_i \le W; 1≤y_i≤H1 \le y\_i \le H).

It is guaranteed that each point in the board contains at most one power supply or consumer. Moreover, no two consumers ii and jj exist where x_i=x_jx\_i = x\_j.

출력

If it is not possible to find such a matching under the given constraints, output a single line containing −1-1.

Otherwise, output a single line containing nn space-separated integers. The iith integer describes p_ip\_i, indicating that power supply ii is connected to consumer p_ip\_i.

예제2

  1. 예제 1

    입력
    5 5 2
    2
    4
    3 2
    5 4
    
    예상 출력
    1 2
    
  2. 예제 2

    입력
    10 10 5
    9
    6
    2
    8
    1
    2 3
    5 8
    3 8
    4 8
    1 2
    
    예상 출력
    2 4 5 3 1