#P11085. [ROI 2019] 学生座位 (Day 2)

[ROI 2019] 学生座位 (Day 2)

Background

Translated from ROI 2019 D2T2.

The school wants to buy nn two-person desks for a new special classroom. These desks have kk types, and each type has different dimensions. A desk of type ii is suitable for students whose height is within the range from LiL_i to RiR_i (inclusive). For other students, sitting at this type of desk is uncomfortable. The discomfort is defined as the absolute difference between the student’s height and the nearest boundary of the suitable height range (that is, LiL_i or RiR_i). If the desk is suitable for the student, the discomfort is 00.

For example, if Li=100,Ri=120L_i = 100,R_i = 120, then a student with height 8080 has discomfort 2020, a student with height 130130 has discomfort 1010, and a student with height 114114 has discomfort 00.

Students from mm classes will take turns coming to the special classroom for lessons, and each class has exactly 2n2n students. The purchased desks will be placed in the classroom, and each desk seats two students.

Problem Description

You are given the heights of every student in every class. You need to buy nn desks and arrange seats for the students of each class so that the total discomfort of all students studying in the classroom is minimized.

Write a program that, based on the information of each desk type and the students’ heights in each class, determines the minimum possible sum of discomfort that can be achieved by purchasing desks and arranging seats in an optimal way.

Input Format

The first line contains three integers m,n,km,n,k, representing the number of classes that will study in the classroom, the number of desks to buy, and the number of different desk types.

Each of the next kk lines contains two integers LiL_i and RiR_i, describing the suitable height range for desk type ii.

Each of the next mm lines contains 2n2n integers h1,h2,,h2nh_1,h_2,\dots,h_{2n}, the heights of the students in that class.

Output Format

Output one number PP, the minimum possible sum of discomfort after buying desks and arranging seats in an optimal way.

1 2 2
5 25
50 90
60 5 10 40
10
2 3 3
200 400
300 500
100 600
300 330 440 40 30 300
150 250 350 450 550 300
130
1 3 4
10 100
200 200
10 100
300 1000
5 10 20 15 200 90
105

Hint

Explanation of Sample 11: In the first sample, only one class studies in the classroom. You should buy one desk of each type, then seat the students with heights 55 and 1010 at the first type of desk, and the students with heights 4040 and 6060 at the second type of desk. Then only the student with height 4040 will feel uncomfortable, with discomfort 1010.

Subtask Score Special property mm Special property nn Special property kk Other special properties
11 1010 100\le100 =1=1 50\le50
22 =1=1 1000\le1000
33 50\le50 5\le5 3\le3
44 100\le100 1000\le1000 =2=2
55 3\le3
66 50\le50 Li=RiL_i=R_i
77
88 88 Li=RiL_i=R_i
99 100\le100
1010 100\le100
1111 44

For 100%100\% of the testdata, $1 \le m\times n \le 200000,2 \le k \le 200000,1 \le L_i \le R_i \le 10^9,1 \le h_i \le 10^9$。

Translated by ChatGPT 5