I am trying to find out the logic to solve this problem , KOIREP — Representatives , i tried for 7 days still get no idea to solve it .
Can anyone please tell me how can i effeciently solve it ? Problem tagged with Sliding window alias two pointer .
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
I am trying to find out the logic to solve this problem , KOIREP — Representatives , i tried for 7 days still get no idea to solve it .
Can anyone please tell me how can i effeciently solve it ? Problem tagged with Sliding window alias two pointer .
Name |
---|
This could be solved using two pointers. Firstly sort all numbers and for each of them remember the group ID to which it belong to. Then for each position l in sorted array we can find the smallest possible r such as there are n different groups on the segment from l to r. Time Complexity : O(n * m * log(n * m));
awesome thanks
You're welcome! :)
how can I efficiently implement "distinct group from index L to R "?. I was thinking that inserting group no. into set, set size will tell me number of distinct group from L to R, I will erase a data from set if the frequency of that data from index L to R becomes 0. But inserting into set complexity is nlog2n.
You can just keep a cnt[] array where cnt[i] tells you the number of elements of the i'th Group ID. Now keep a variable answer. Whenever you move a pointer update cnt[i] i.e. if it comes inside the sliding window then increase otherwise decrease it. If cnt[i] becomes one then answer++ and if cnt[i] becomes 0 then update answer--. This is a common technique and it it will be beneficial to get familiar with it.
Can you please share your solution?
My approach is similar to what mentioned above. You can see my code here.
I did it storing pointers to every class and moved only that pointer which has least ability(made sure I sorted every class) if you want I can share my solution.