Блог пользователя Ankurkumar6084

Автор Ankurkumar6084, история, 5 лет назад, По-английски

Hi Codeforces Community, A problem came in DE Shaw OA more than 10 days ago , Here is the problem, I guess some kind of sorting is involved but am not able to figure out how to do it. I think it is a very good problem and these problems have become quite standard in online assessments. So if you guys can throw some light onto the problem that would be very helpful. If possible please do provide code for the problem.

Question-

Given two arrays A and B of size n. We are given q queries of the form x y. The answer to a given query is (A[ i ]+B[ i ]) where A[ i ] >=x && B[ i ] >=y && (A[ i ] +B[ i ]) is minimum for 0=< i < n. If answer for a query is not possible then print -1 for that query.

`1=< n,q <= 10^5`
`1=< A[i],B[i] <=10^9`
`Eg-`
`n=5 q=4`             
`6 4 8 9 15`
`7 7 2 9 6`
`4 6 -> 11(4,7)`
`2 8 -> 18(9,9)`
`14 9 -> -1`
`7 1 -> 10(8,2)`
  • Проголосовать: нравится
  • +12
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

Tell me if I am wrong here...

If we select the minimum A[i] such that A[i] >= x and we select the minimum B[i] such that B[i] >= y, then (A[i] + B[i]) is already minimised.

Steps:

1) Sort both A and B --> O(n log n )

2) use lower_bound() to find A[i] and B[i] for each query --> O(log n)

3) report (A[i] + B[i]) as found in step 2.

final time complexity: O(nlogn + qlogn)

»
5 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +10 Проголосовать: не нравится

Possible approach Sort pairs of (Ai, Bi). Perform coordinate compression on Bi & initialize an empty segment tree on a range of B. Start processing queries in descending order of Ai, query range of >=Bi for result at each step & make point update of the sum on the segment tree corresponding value of Bi.

TC O( nlogn + qlogn)