Need help in a 1700-ish problem
source link: https://codeforces.com/blog/entry/125717
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Hello, CF community, I found this 1700*-ish problem, can somebody help?
Given nn, dd positive integers, and the array aa with length nn
for each ii s. t 1≤i≤n1≤i≤n
Find the maximum j s. t |ai−aj|≤d|ai−aj|≤d
Input format:
Integers n,d: 1≤n≤1e5,1≤d≤1e91≤n≤1e5,1≤d≤1e9
In the next line given nn integers representing a1a1, a2a2, ..., an−1an−1, anan
Output format:
In single line for each 1≤i≤n1≤i≤n output the corresponding jj
if there is no corresponding jj output -1
I'd appreciate a solution without any kind of advanced data structure(___Trees, ___Arrays, etc)
Thanks for any help
For all ai, Find max j where aj is (> ai and <= ai + d) and( < ai and >= ai — d ). You can use binary search for this |
-
You know you can't run binary search on a random array with no logic? and if you know a way please provide me code and I'll try to understand it, thx
-
store the array with index then sort, then use binary search to find the range. then max j in that range using segment tree
-
As I mentioned earlier in the blog, I don't know any advanced data structures including segment trees, and I don't believe in knowing one until I'm not around 1900 and I believe every question can be solved without them(and it worked out better than you're strategy, knowing one)
So any other solution without a segment tree be helpful thx again
-
Not knowing is fine, but refusing to know until reaching some level is stupid. It's reasonable to not want to spend time learning it, but refusing to ponder specific solutions paths will only hinder you from having more creative and exploratory thought process. Instead try to find any solution path while considering what things that seem reasonably possible you aren't sure of, then ask yourself how to do or how to simplify those parts of it (or eventually consider it's wrong approach).
Also segment tree is not advanced in itself, it is basically just writing out all possibilities of binary search.
lopital24's idea with two pointers is what I would do tho.
-
-
37 minutes ago, # | We can scan through given array from right to left, supporting set of indices, answer for which has not been found yet.
|
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK