Jousting Tournament
Time limit1sMemory limit256 MB
Given the starting order of N-1 knights and C fixed round intervals, find the smallest insertion position for a late knight with skill R that maximizes the number of rounds it wins.
- Level
Hard8 of 10
- Topics
- Array, Simulation, Binary search, Segment tree
- Solved
- No attempts yet
Problem
A jousting tournament is held with knights. The knights first stand in a single line and, from the front, are numbered to in the order they stand.
A round begins by announcing two positions and with . Every knight currently standing at positions through (inclusive) competes, and exactly one of them wins. The winner goes back into the line while the losers drop out; the remaining knights then move toward position , keeping their relative order, to close the gaps, so the winner ends up at position . The line is thus renumbered to (previous length) . The next round works the same way, and rounds continue until a single knight is left.
Every knight has a distinct skill, given as an integer from to (a larger value means a stronger knight). The position ranges of all rounds are known in advance, and in every round the competitor with the highest skill always wins.
Of the knights, have already arrived and are standing in line; only the most popular knight has not arrived yet. The late knight has skill . To make the festival as exciting as possible, we want to insert this knight at the position that maximizes the number of rounds it wins. Rounds in which the late knight does not take part are irrelevant — only the rounds it enters and wins are counted.
For example, if the current line has skills and a round announces , then the knights at positions (skills ) compete, the skill- knight wins, and the line becomes .
You are given:
- : the total number of knights ().
- : the number of rounds ().
- : the skill of the late knight. All skills, including , are distinct integers from to .
- : an array of integers, the skills of the already-present knights in line order.
- and : arrays of length . For each with , round involves the knights currently at positions through . It is guaranteed that , that is smaller than the number of knights in line when that round starts, and that exactly one knight remains after all rounds.
Find the best position () at which to insert the late knight so that the number of rounds it wins is as large as possible. If several positions are optimal, choose the smallest. Here is the late knight's position after insertion, i.e. the number of knights standing in front of it: places it at the very front, and places it at the very back.
Input
The first line contains , , and . Each of the next lines contains one value . Each of the following lines contains and .
Output
Print , the smallest optimal insertion position for the late knight, on a single line.