The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 399 Weeks Ago, 1 Day Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
During the summer, the boy-scouts group decides to go out for a ranging. The teacher-in-charge Mr. Anderson is faced with the tough job of forming groups for this ranging. Every student in the boy-scouts group has a rank. Now Mr. Anderson has to ensure that every group has a group leader whose rank is greater than or equal to the other group members rank, and his age should not differ from the other members in the group by more than K years.
Now many students are friends and they want their group to be as large as possible.
So they approach Mr. Anderson in pairs to ask the largest group size in which that pair can be accommodated.
Since the scouts are large in number, Mr. Anderson is finding it difficult to answer the students and hence comes to you to solve his problem.
First line contains two integers N(1 = N = 100000) and K(1 = K = 107), number of scouts and the age difference limit in a group. Each scout has a unique serial number from 1 to N.
Next line contains N space separated integers r1,r2,...,rN (1 = ri = 109), representing the ranks of scouts in order of their serial number. Ranks may be same for two scout.
Then next line again contains N integers a1,a2,...,aN (1 = ai = 109), representing the age of scouts in order of their serial number.
Next line contain number of queries, q (1 = q = 105). Then q lines follow.
ith line contains two integers each Xi and Yi separated by a space, the serial number of the scouts who need be accommodated to largest group.
Output q lines, each containing an integer, maximum group size in which the two scouts queried Xi and Yican be accommodated. The scouts may or may not be the leader of the group.
Input: 6 1 2 5 3 4 3 4 2 4 3 2 1 3 3 2 6 3 6 2 4 Output: 3 5 -1 Explanation: For the first query, Serial number are 2 and 6, the largest group size is 3 (serial 2,3,6), group leader is 2.
For the second query, Serial number are 3 and 6, the largest group size is 5 (serial 1,3,4,5,6),
group leader is 4.
In third query, scout 2 has maximum rank, so he is leader, but his age difference with scout 4 is 2,
violating the age restriction. Hence no group can be formed.
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll