#BS0000221. Người dẫn đầu cuộc bỏ phiếu (Online Election)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Người dẫn đầu cuộc bỏ phiếu (Online Election)
Online Election
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
There are votes in strictly increasing chronological order. Vote is for candidate at time . Candidate identifiers range from 0 to .
For each query time , count votes with . The leader is the candidate with the greatest count. Among tied leaders, choose the candidate whose most recent vote occurred latest.
Input
The first line contains . The second line contains the candidate identifiers . The third line contains the vote timestamps . Each of the next lines contains a query time .
Output
Print lines, one leader identifier for each query in input order.
Subtasks
- Subtask 1 (20 points): ; ; for every ; ; for every .
- Subtask 2 (30 points): ; ; ; for every ; ; for every ; .
- Subtask 3 (50 points): ; ; ; for every ; ; for every .
Examples
Input
4 5
0 1 1 0
0 5 10 15
0
5
14
15
3
Output
0
1
1
0
0
Explanation
At time 0 only candidate 0 has a vote. At 5 both have one, and 1 wins the tie by recency. At 14 candidate 1 has two against one. At 15 both have two, and 0 received the latest vote. At 3 only the first vote counts, so the leader is 0.