IOI '13 - Brisbane, Australia


Marita's little brother has left toys all over the living room floor! Fortunately, Marita has developed special robots to clean up the toys. She needs your help to determine which robots should pick up which toys.

There are T toys, each with an integer weight W[i] and an integer size S[i]. Robots come in two kinds: weak and small.

  • There are A weak robots. Each weak robot has a weight limit X[i], and can carry any toy of weight strictly less than X[i]. The size of the toy does not matter.
  • There are B small robots. Each small robot has a size limit Y[i], and can carry any toy of size strictly less than Y[i]. The weight of the toy does not matter.

Each of Marita's robots takes one minute to put each toy away. Different robots can put away different toys at the same time.

Your task is to determine whether Marita's robots can put all the toys away, and if so, the shortest time in which they can do this.

As a first example, suppose there are A = 3 weak robots with weight limits X = [6, 2, 9], B = 2 small robots with size limits Y = [4, 7], and T = 10 toys as follows:

Toy Number0123456789

The shortest time to put all the toys away is three minutes:

Weak robot 0Weak robot 1Weak robot 2Small robot 0Small robot 1
First minuteToy 0Toy 4Toy 1Toy 6Toy 2
Second minuteToy 5Toy 3Toy 8
Third minuteToy 7Toy 9

As a second example, suppose there are A = 2 weak robots with weight limits X = [2, 5], B = 1 small robot with size limit Y = [2], and T = 3 toys as follows:

Toy number012

No robot can pick up the toy of weight 5 and size 3, and so it is impossible for the robots to put all of the toys away.

Input Format

The input will be in the following format:

  • Line 1 will contain three integers A, B (0 ≤ A, B ≤ 50,000, and 1 ≤ A + B) and T (1 ≤ T ≤ 1,000,000), respectively representing the number of weak robots, the number of small robots, and the number of toys.
  • Line 2 will contain A integers, the values of X[i] (1 ≤ X[i] ≤ 2,000,000,000), that specify the weight limit for each weak robot.
  • Line 3 will contain B integers, the values of Y[i] (1 ≤ Y[i] ≤ 2,000,000,000), that specify the size limit of each small robot.
  • The next T lines will each contain two integers W[i] and S[i] (1 ≤ W[i], S[i] ≤ 2,000,000,000), the weight and size of each toy, respectively.

If A = 0 or B = 0, then the corresponding line (line 2 or line 3) should be empty.

Output Format

The output should contain one integer - the smallest number of minutes required to put all of the toys away, or -1 if this is not possible.

Sample Input 1

3 2 10
6 2 9
4 7
4 6
8 5
2 3
7 9
1 8
5 1
3 3
8 7
7 6
10 5

Sample Output 1


Sample Input 2

2 1 3
2 5
3 1
5 3
2 2

Sample Output 2


Description of Samples

The first sample input describes the first example above, which is summarized as follows:

X[6, 2, 9]
Y[4, 7]
W[4, 8, 2, 7, 1, 5, 3, 8, 7, 10]
S[6, 5, 3, 9, 8, 1, 3, 7, 6, 5]

The second sample input describes the second example above, which is summarized as follows:

X[2, 5]
W[3, 5, 2]
S[1, 3, 2]


SubtaskPercent of PointsAdditional Input Constraints
114T = 2 and A + B = 2 (exactly two toys and two robots)
214B = 0 (all robots are weak)
325T ≤ 50 and A + B ≤ 50
437T ≤ 10,000 and A + B ≤ 1,000

All Submissions
Best Solutions

Point Value: 20 (partial)
Time Limit: 4.00s
Memory Limit: 64M
Added: Jul 28, 2013

Languages Allowed:

Comments (Search)

Very weak test data. I submitted a wrong greedy solution that should give TLE and WA. I got 76 points. Only 2 tests WA and 3 TLE.

Some people are only satisfied when they actually solve the problem, not when they get points.

Can't get accepted with O(n*(logn)^2)!!!

Why am I getting WA in test #6x only? I can't find any bugs in my code.
Can somebody help me?

I can tell you that it's a randomly-generated case, and that you're vastly understating the amount of time required -- your answer is about half of the correct answer.

Since the IOI publishes its test data, you can always go look it up yourself.

Test case #4p: Runtime Error (8: SIGFPE) [0.008s, 11.2M] (25/25)
But I got 100/100 points...
Is there any input (A+B=0) ?

No, but fixed.