Yours fails on the testcase:
1
6 3
15 14 36 1 33 38
The answer should be 21:
Best choice:
[ 15] 14 [ 36] 1 [ 33] 38
R G B R G B
max - min = 21
Yours fails on the testcase:
1
6 3
15 14 36 1 33 38
The answer should be 21:
Best choice:
[ 15] 14 [ 36] 1 [ 33] 38
R G B R G B
max - min = 21
After reading the quick hint, I tried it on my ownâŚ
Although I did almost the same, it is still not optimizedâŚCanât understand why ?
I have just used 2 nested loops like the editorialistâs solution & terminated the inner loop once we select âmâ (in my code k) valuesâŚ
Can anyone help me out ?
My Solution (CAMC)
Youâre resetting c and j for each i, giving an O(N^2) solution.
Edit:
For a concrete testcase that triggers O(N^2) computations, consider this one.
On your testcase my implementation solves it in around 0.5 seconds. I think this is because I skip the similar colors when I encounter them in the sorted sequence.
What I mean by that is say after sorting you have some sequence of colors RGRAGBA... now if the sequence is not complete and you encounter a similar color you can discard the previous occurrence of that color and replace it by the new occurrence.
https://www.codechef.com/viewsolution/27771209
@ssjgz I have used the same approach , still it is giving tle for the 2nd subtask ?
link to my solution
#include<bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#define ll long long int
#define ull unsigned long long int
#define big cpp_int
using namespace std;
using namespace boost::multiprecision;
struct data{
ll val;
int ind;
};
bool compare(data a, data b){
return a.val < b.val;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
data a[n];
ll ans = INT_MAX;
ll k;
for(int i=0;i<n;i++){
cin>>k;
a[i].val = k;
a[i].ind = i%m;
}
sort(a,a+n,compare);
int cnt[m] = {0};
int cnt0 = 0;
int r = 0;
for(int i=0;i<n;i++){
cnt0 = 0;
r = i;
while(cnt0<m && r<n){
if(cnt[a[r].ind]<=i){
cnt0++;
cnt[a[r].ind]++;
}
r++;
}
if(cnt0==m){
ans = min(ans,a[r-1].val-a[i].val);
}
}
cout<<ans<<endl;
}
return 0;
}
Yours seems to time out on the same testcase as this guy, and for the same reason ![]()
Edit:
This testcase is even âbetterâ for your solution.
I donât understand this part of the editorialistâs code, why he is incrementing by 1, whenever cnt[l].S == 0 ?
@ssjgz Also The previous color cnt[l].S can occur in between the range of l and r, when selecting next box as l but the while loop will always look for the color cnt[l].S after the r of previous box.
The colour a[l].S used to be in the range l-r, but weâre about to increment l and then it wonât be.
@ssjgz correct, but why increment only when cnt[l].S ==0, it should always increment , so shouldnât it be cnt0++ instead of cnt0 += cnt[a[l].S] == 0;
If cnt[a[l].S] != 0, then there are still other occurrences of the colour a[l].S in the range even after we increment l, in which case we should not increment cnt0.
Thanks a lot 
i m not getting the point in solutionâŚCan anyone explain it in a more easy way
.
Can anybody please tell at which test case this code is giving WA: CodeChef: Practical coding for everyone
It fails on the testcase:
1
2 2
50 16
Thanks for the test case.
Hi coders,
Please help me out with the test case for which my code fails as I am getting WA on submissions. I have tried to cover all boundary cases however I feel I am not able to cover all. Link to my code is:
https://www.codechef.com/viewsolution/27940370
Thanks,
Learner
Your solution fails for the testcase:
1
6 2
95 38 9 41 57 5
(the answer should be 4:
Best choice:
95 38 [ 9] 41 57 [ 5]
R G R G R G
max - min = 4
)
@ssjgz I really appreciate your quick response. I found my mistake and code has been accepted successfully now. Link:
https://www.codechef.com/viewsolution/27942809
I have used the same appraoch but only getting partially AC. @yash_chandnani
Any help would be really Appreciable !!
https://www.codechef.com/viewsolution/30687657
This is my solution
Your solution is O(N^2) (or O(N \times M), or something equally suboptimal
) and times out on the testcase in this post.