Hello. I was trying to solve Problem C, and used the following approach.
Using dynamic programming, for all dp(i, j) such that i, j >= 1 if dp(i, j-1) = 0 || dp(i+1, j) = 0 || sum from index i to j = 0, then dp(i, j) = 0 else dp(i, j) = 1
I then changed the 2-dim array dp[][] to 1-dim and used this approach. dp[i] = 0, if dp[i-1] = 0 or d[i] = 0, or sum from i to j = 0
Again I was getting correct output for all my testcases, but pretest 3 is giving me TLE
This is my solution using this approach.
β Submission #75942879 - Codeforces
Can someone please help me with this problem? Is the dp approach that Iβm using correct?
The problem is that in pretest 3, the value of N = 2 x 10^5. Please help.
What i did was i calculated ans like how many subarrays ending at ith index have a subsegment with sum=0,so all those for each index i-I calculated the largest index j(j<=i) such that sum(j,i)=0,so we cant include the subarrays with starting point <=j ,if there is no subarrys ending at ith position having sum=0,then we just add the maximum value of starting index occured previously because
since we have calculated ans for subarrays ending at [1,i-1] if for current index i we cant include those with starting index<=max starting index uptil now so just add ans+=(max starting index uptil now),if(there is a subarray ending at ith position having sum 0 and starting at pos=j){
if(max starting index<j)ans+=j,max starting index=j;
else ans+=max starting index
}
and finally ans=n*(n+1)/2 - ans
my solution :Submission #75895071 - Codeforces