Can someone tell me why this code is giving wrong answer
I have tried similar approach in editorial
it is passing the sample cases and some other cases.
int main(){
int t;
cin>>t;
while(tâ){
int n,k;
cin>>n>>k;
int a[n];
unordered_map<int,int>lastseen;
for(int i=0;i<n;i++){
cin>>a[i];
lastseen[a[i]]=-1;
}
int dp[n][k+1];
//dp states are index and adjacent difference
//dp[index][difference] represents max length possible till this index
//dp[1][2] means till index 1 the adjacent difference of 2 are 1,2,3 or 1,2,1
for(int i=0;i<n;i++){
for(int j=0;j<=k;j++){
dp[i][j]=-1;
}
}
dp[0][0]=1;
lastseen[a[0]]=0;
for(int i=1;i<n;i++){
for(int j=0;j<=k;j++){
if(j>i){
break;
}
if(j==0){
if(lastseen[a[i]]==-1){
dp[i][j]=dp[i-1][j];
}
else{
int idx=lastseen[a[i]];
dp[i][j]=max({dp[i-1][j],dp[idx][j]+1});
}
}
else if(lastseen[a[i]]==-1){
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+1);
}
else{
int idx=lastseen[a[i]];
dp[i][j]=max({dp[i-1][j],dp[idx][j]+1,dp[i-1][j-1]+1});
}
}
lastseen[a[i]]=i;
}
int ans=0;
for(int j=0;j<=k;j++){
ans=max(ans,dp[n-1][j]);
}
//Remove this comment to print dp
I am kind of a newbie to this field. Can you please tell or share the chart, if there is any, how it is decided that how many test cases should take how much time?
When it would give TLE and when it wouldnât.
Thankyou.
I made arrays global and it got accepted ,i donât understand why it happens only on codechef ,that you think you did something wrong and waste time on it but at the end you realize it was not wrong.
Can anyone tell what is wrong in the approach ? I am following notion of standard LIS not exactly but kind of⌠I am maintaining length[] array for length of subsequence and distinct[] array for number of different adjacent elementsâŚ
Why do we need pref_max(i,k)? Why donât we just keep the longest k-beautiful subsequence until i position in f( i, k). We can get it by f(i , k)=max( f(i-1, k-1) , f(last( i ), k ) ) +1.
Please donât talk nonsense about the weak tests and bad constraints. Solutions that compute up to 4 \cdot 10^8 operations can easily pass in one second, so 10^8 is nowhere near tight with a problem of these constraints. O(N \log N) passes for N \leq 10^7 easily in c++, giving issues in java/python, so itâs not weird at all why a TNK passes.
excuse me bro if you understand the editorial can you please explain it with a simple example
especially reducing the transitions part containing the two bullet points
thank you