awoo's blog

By awoo, history, 5 weeks ago, translation, In English

2025A - Two Screens

Idea: BledDest

Tutorial
Solution (BledDest)

2025B - Binomial Coefficients, Kind Of

Idea: adedalic

Tutorial
Solution (adedalic)

2025C - New Game

Idea: fcspartakm

Tutorial
Solution (awoo)

2025D - Attribute Checks

Idea: adedalic

Tutorial
Solution 1 (adedalic)
Solution 2 (adedalic)

2025E - Card Game

Idea: BledDest

Tutorial
Solution (Neon)

2025F - Choose Your Queries

Idea: BledDest

Tutorial
Solution (BledDest)

2025G - Variable Damage

Idea: BledDest

Tutorial
Solution (awoo)
  • Vote: I like it
  • +77
  • Vote: I do not like it

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Finally

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Hii everyone can anyone help me D problem?

https://codeforces.net/contest/2025/my

this was my solution

i would keep count of zeros till i

then our s could range from 0 to zeros

so my state is dp[s] which will have max check passed, so my time complexity was 5000*2*10^6

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    https://codeforces.net/contest/2025/submission/286105592

    here i have reduced most of the redudant operations still i get tle

    what i am thinking is to have a vector which stores starting and ending point of zeros if more than 1 then i would just increment my of zeros to difference between start and end their by saving some more time am i going in the right direction?

    i would be using a unordered map to store my records.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I think you can safely do 1e8 complexity per second in c++ on codeforces; maybe 1e9 with luck (including simple operations, good caching and etc.); almost never > 5e9.

      so my time complexity was 5000*2*10^6

      If the above is correct, it's 1e10 and should TLE.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        yes i have optimized it by using binary search and sorting between 2 zeros.

»
5 weeks ago, # |
  Vote: I like it +25 Vote: I do not like it

For those, who interested in combinatorial way for problem E, using something similar to Catalan numbers: I wrote a post about it

»
5 weeks ago, # |
  Vote: I like it -10 Vote: I do not like it

Does Problem D fall under some Dp pattern saw this pattern of pushAndClear in many submissions it didn't click to me at all.

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

isn't the code in problem A's solution $$$ O(n^2) $$$ due to string slicing?

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It is, and it was kind of explained in the solution. They say it can be done in O(n) but there is no need to optimize it because the constraints are way too low.

»
5 weeks ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

.

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

for Problem c , can anyone tell why this solution is giving wrong answer on test 4 (i used binarysearch to solve this): https://codeforces.net/contest/2025/submission/285946605 thanks in advance !!

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    By binary research, you can only find the position of the last card which number is less than $$$(x+k-1)$$$, but you are not able to know if there are some numbers missing (between $$$x$$$ and the number of the last card). It’s the reason why you get wrong answer.

    BTW, if you choose to check through the whole interval between $$$x$$$ and $$$(x+k-1)$$$ to make sure there are no missing numbers, the time complexity would be something like $$$O(N^2)$$$ in worst case and fails the time limit.

    Therefore, I don’t think it’s possible to use binary research to solve this problem.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      If you check my binary search function i have added this line :

      if(ss.get(m) <= (ss.get(i)+k-1) && Math.abs(ss.get(m)-m) == Math.abs(ss.get(i) -i)){
      

      here in the second part(after &&) i am checking if difference between the number and index is equal for start and end card. for ex:

      0 1 2 3 4 5 6
      3 4 5 6 8 9 10
      

      if we consider from index 1 to 3 : diff is same (4-1 == 6-3) but for index 1 to 4 : (4-1 != 8-4).

      by using this i was able to get correct answer with binary search. But on test case 4 it is failing.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        The method works only when all numbers are distinct.

        However, the problem allows number to be repeated. For example:

        (0 1 2 3 4 5)
         4 5 5 6 8 9
        

        For index 1 to 4, diff is same (8-5 == 4-1), but obviously the number 7 is missing.

        As you can see, if there are repeated numbers in the interval, the method cannot handle this situation properly.

        More precisely, if the amount of repeated numbers is same as the amount of missing numbers (while both amounts are not 0), then the result of your method would be wrong.

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          The array on which I am running the binary search function only contains unique elements.

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I calculated prefix sum for number of times a unique element in present. And stored all unique elements in an array on which i ran bs to find last index. Then i calculated the and from prefix sum based on the index i found from bs. Does this sound correct ?

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Note about Problem A ( or any other problem concerning strings ) if you use Python and fast IO like: input = sys.stdin.readline then make sure to strip the string from whitespace and newlines.

this code was tested in Windows fine:

import sys

# defined functions for fast input
input = sys.stdin.readline
readListInts = lambda type_="int", sep=" ": list(
    map(eval(type_), input().strip().split(sep))
)

# defined functions for fast output
output = lambda x: sys.stdout.write(str(x) + "\n")
outputString = lambda x: sys.stdout.write(x + "\n")

def main():
    for _ in range(int(input())):
        s = input()
        t = input()

        lcp = 0
        n = len(s)
        m = len(t)
        for i in range(1, min(n, m) + 1):
            if s[:i] == t[:i]:
                lcp = i
        output(n + m - max(lcp, 1) + 1)

if __name__ == "__main__":
    main()

However, the judges are based on POSIX systems and newlines are treated differently between the two systems. So I needed to make this small adjustment:

 s = input().strip()
 t = input().strip()

A small detail that may cost you a submission.

I guess for veteran competitive programmers this is trivial but it cost me some time and a wrong submission on an easy problem.

»
5 weeks ago, # |
  Vote: I like it +9 Vote: I do not like it

Problem B Binomial Coefficients Kind of Video Editorial Link: https://youtu.be/y_b2Khyk28w?si=Ku5V5m1jtT8EtA1e

  • »
    »
    5 weeks ago, # ^ |
    Rev. 3   Vote: I like it +2 Vote: I do not like it

    Honestly, instead of doing this video... up solve next time.

    Or may be just downvote this comment and rant over it.

    Not that anyone asked, but felt like sharing constructive feedback.

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

_i want to know,would anyone please tell me why rating going back;

»
5 weeks ago, # |
Rev. 2   Vote: I like it +12 Vote: I do not like it

my solution to D is a little different and it does not require bs or lazysum.

let $$$dp[i]$$$ denotes the $$$max \ score$$$ we can get if we are at index $$$j$$$ and have total $$$i$$$ intelligence points
if $$$A[i] == 0$$$ we will update dp values as follows.
let $$$cnt1[x] \ and \ cnt2[x]$$$ stores no. of checks with values x of intelligence and strength respectively.

if we choose to increase intelligence points then we are sure that we will pass all the intelligence checks having value $$$<=$$$ to our current intelligence values and because all the checks with value $$$<$$$ $$$current intelligence$$$ are already covered we will need to calculate how many values = current intelligence are there on the right on the right of index i and this value is stored in cnt1[current intelligence].

$$$dp[i] \ = \ max(dp[i] \ + \ cnt2[s - i], \ dp[i - 1] \ + \ cnt1[i])$$$
here $$$i$$$ is $$$current_intelligence$$$ and s is $$$current total score = intelligence + strength$$$

here is the 285916766 to explain better (ignore the code above solve function).

feel free to ask me if you have any queries. Thanks!

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    great solution... thanks for sharing. I think editorial solution for same is unnecessarily made complex (although nice explaination, but conclusion is messy)

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

E can be solved using DP + OEIS sequence A050155.

Define $$$g(m,k)$$$: Number of ways to choose $$$(m+k)/2$$$ elements for Player 1 out of m cards in a given suit such that Player 1 wins as given in Problem.

Write a brute force for function $$$g$$$ -> generate the sequence and paste on OEIS -> Find the formula $$$g(m,k)$$$ given in OEIS link above -> Precompute it -> Multiply $$$g(m,j-k)$$$ with $$$DP[i-1][k]$$$ and then sum over all values of $$$0\leq k\leq j$$$ to find $$$DP[i][j].$$$ $$$1\leq i\leq n,0\leq j\leq m$$$.

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Can someone explain why its showing TLE in this submission in problem C , in the contest it got accepted but later on it came out to be TLE 285923775 in test case 24

»
5 weeks ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

.

»
5 weeks ago, # |
  Vote: I like it +9 Vote: I do not like it

E is a typical problem that requires $$$y-x\ge k$$$ on a path from $$$(0,0)$$$ to $$$(m,n)$$$. See here for more details.

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

In Problem C, I have an issue:

I have two pieces of code. The first one:

from collections import deque
from sys import stdin
input = lambda: stdin.readline().rstrip()

def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    hs = {}
    for i in a:
        if hs.get(str(i)) is None:
            hs[str(i)] = 0
        hs[str(i)] += 1
    mapLi = []
    mx = 0
    for i in hs:
        mx = max(mx, hs[i])
        mapLi.append([i, hs[i]])
    mapLi.sort(key=lambda i: (int(i[0]), -i[1]))
    ans = 0
    queue = deque([])
    cnt = 0
    for i, v in mapLi:
        ans = max(ans, cnt)
        while queue and len(queue) >= k:
            cnt -= queue.popleft()[1]
        ans = max(ans, cnt)
        while queue and abs(int(queue[-1][0]) - int(i)) > 1:
            cnt -= queue.pop()[1]
        ans = max(ans, cnt)
        queue.append((i, v))
        cnt += v
        ans = max(ans, cnt)
    ans = max(ans, cnt)
    return ans

for _ in range(int(input())):
    print(solve())

The second one:

from collections import deque
from sys import stdin
input = lambda: stdin.readline().rstrip()

def solve():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    hs = {}
    for i in a:
        if hs.get(i) is None:
            hs[i] = 0
        hs[i] += 1
    mapLi = []
    mx = 0
    for i in hs:
        mx = max(mx, hs[i])
        mapLi.append([i, hs[i]])
    mapLi.sort(key=lambda i: (int(i[0]), -i[1]))
    ans = 0
    queue = deque([])
    cnt = 0
    for i, v in mapLi:
        ans = max(ans, cnt)
        while queue and len(queue) >= k:
            cnt -= queue.popleft()[1]
        ans = max(ans, cnt)
        while queue and abs(int(queue[-1][0]) - int(i)) > 1:
            cnt -= queue.pop()[1]
        ans = max(ans, cnt)
        queue.append((i, v))
        cnt += v
        ans = max(ans, cnt)
    ans = max(ans, cnt)
    return ans

for _ in range(int(input())):
    print(solve())

I submitted the second piece of code to Codeforces Edu Round 170, Problem C, but it resulted in a Time Limit Exceeded (TLE). Then, I submitted the first piece of code, and it passed successfully. Is this an issue with the judging system, or is there a problem with my code? Can someone explain the reason? I would be very grateful!

link1:https://codeforces.net/contest/2025/submission/286123006 link2:https://codeforces.net/contest/2025/submission/286122969

If this problem is solved, you will get one of Python's optimization ticks!!!!!

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Avoid using dict or set in an open-hack round as they're to be easily hacked. If you have to, write your own hash function.

    By using str(i) instead of i as the key, you actually define another hash function for $$$i$$$ so you pass the test.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      In fact, my program passed the hack attack, but I did not pass the final system test, but I think it is the same, thank you very much for your answer to me!

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Is it possible for the O(mn) solution for D to be posted? I'm having a hard time understanding what the difference is between "last state" and "last record".

  • »
    »
    5 weeks ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    and are we supposed to increment $$$I$$$ counter while iterating through the input array?

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I think an expert has the right answer

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I read some of the other D solutions here but I cannot understand the intuition behind any of them so I think I'll give up on this question even though it's extremely frustrating.

»
5 weeks ago, # |
  Vote: I like it +6 Vote: I do not like it

Problem F is essentially equivalent to 858F - Wizard's Tour after some transformations, but I still believe it is a valuable problem for an Educational Round.

»
5 weeks ago, # |
Rev. 5   Vote: I like it +21 Vote: I do not like it

A $$$O(m\log m)$$$ solution for problem E.

From the derivation of the editorial, we can transform the question into the following form:

Cards of suit $$$1$$$ form a parenthesis sequence with $$$x$$$ extra open parentheses, and cards of suit not $$$1$$$ form a parenthesis sequence with $$$a_i$$$ extra close parentheses, $$$\sum a=x$$$.

Through the blog from darthrevenge can easily calculate a color answer. Write a generating function for a color suit:

$$$f(x)=\sum\limits_{i=0}(\binom{m}{\frac{m+i}{2}}-\binom{m}{\frac{m+i+2}{2}})x^i$$$

Then the anwser is

$$$\sum\limits_{i=0}^m [x^i]f(x)\times [x^i]f^{n-1}(x)$$$

The only problem is to calculate

$$$f^{n-1}(x)$$$

There's two ways. One is use quick power with NTT in $O(n\log^2 n)$, and the other is use $$$\ln$$$ and $$$\exp$$$ with $$$f^n(x)=e^{n\ln f(x)}$$$ in $$$O(n\log n)$$$. Notice that calculate $$$\frac{f^n(x)}{f_0}$$$ instead of $$$f^n(x)$$$ when $$$f_0\ne 1$$$ because of $$$\ln$$$. submission

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    After contest i was thinking about solution one step lower than this. i was to create matrix and create solution of O(m^3log(n)).if you can give any insight about it. i tried but couldn't create base matrix it got little complicated for me.

»
5 weeks ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

"Alternate" solution to D:

We can imagine an $$$m$$$ by $$$m$$$ grid, where $$$a[i][j]$$$ represents the state of having intelligence $$$i$$$ and strength $$$j$$$. By the end, we should reach the

Unable to parse markup [type=CF_MATHJAX]

diagonal while reaching the maximum amount of checks.

In order to get the value of a check, the following conditions must be met:

Unable to parse markup [type=CF_MATHJAX]

(or

Unable to parse markup [type=CF_MATHJAX]

) and $$$i + j = k$$$

...where $$$k$$$ is the number of zeroes before the check. In other words, it corresponds to adding $$$1$$$ to a prefix (or a suffix) of that diagonal. This can be done using prefix sums. Code: https://codeforces.net/contest/2025/submission/285891703

It's basically the same as the model solution, just using more memory. But I feel this is more intuitive and satisfactory.

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

how to solve E recursively ?

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone please help me find out what's wrong with my submission to problem D? 286175219

In my submission: $$$f(l, r, s, i)$$$ = score if I am passing through $$$[l, r)$$$ having $$$strength = s$$$ and $$$intelligence = i$$$, and $$$dp[i][j]$$$ = max. score when I am at $$$i$$$-th zero and having $$$intelligence = j$$$

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Actually I like my D approach better — https://codeforces.net/contest/2025/submission/285910844

»
5 weeks ago, # |
  Vote: I like it +4 Vote: I do not like it

Can anyone suggest DP optimisation problems similar to D?

»
5 weeks ago, # |
  Vote: I like it +18 Vote: I do not like it

I have a slightly different approach for problem F, though I think it actually ends up being the same as the one explained in the editorial.

Just like the editorial, I reduce the problem to making pairs of edges. Now, notice that on tree problem is quite straightforward as you merge all the leaves of the same parent together and then destroy them. If you have some node v, with only one leaf u and parent w, make pair ((u,v), (w,v)). This guarantees optimal answer.

Now, let's say we have some additional edge a-b, that is not in the tree. We can just direct it towards a and that is identical as if we added a leaf to node a. So we will get a new tree that has Q edges and Q+1 nodes, which we can solve using the algorithm previously mentioned.

Basically, the core of the idea is that as we don't care about the nodes and just edges, we can make multiple copies of one node in order to get a tree.

»
5 weeks ago, # |
  Vote: I like it -8 Vote: I do not like it

Hey can anyone help me in D, I think my TC is (m^2+n)*log(n), used dp to solve recursively and sorting checks separately between 0s, failed on test case-8 due to TLE, but saw some other solutions of same TC(I think) get all passed. Here is my code :


#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; int main(){ int n,m; cin>>n>>m; vector<int> v(n),z; int i1 = 0; z.push_back(0); vector<vector<int>> pos(m+1); vector<vector<int>> neg(m+1); for(int i = 0; i < n; i ++) { cin>>v[i]; if(v[i] == 0) {z.push_back(i);i1++;} else if(v[i] > 0) pos[i1].push_back(v[i]); else neg[i1].push_back(abs(v[i])); } for(int i = 1; i < m+1; i ++) sort(pos[i].begin(),pos[i].end()); for(int i = 1; i < m+1; i ++) sort(neg[i].begin(),neg[i].end()); vector<vector<int>> dp(z.size()+1,vector<int>(z.size()+1,INT_MIN)); function<int(int,int)> solve = [&](int i, int j){ if(dp[i][j] != INT_MIN) return dp[i][j]; if(i == z.size()) { int tot = (lower_bound(pos[i-1].begin(),pos[i-1].end(),j+1) - pos[i-1].begin()) + (lower_bound(neg[i-1].begin(),neg[i-1].end(),i-j) - neg[i-1].begin()); return dp[i][j] = tot; } int tot = (lower_bound(pos[i-1].begin(),pos[i-1].end(),j+1) - pos[i-1].begin()) + (lower_bound(neg[i-1].begin(),neg[i-1].end(),i-j) - neg[i-1].begin()); return dp[i][j] = max(solve(i+1,j),solve(i+1,j+1)) + tot; }; int ans = solve(1,0); cout<<ans<<endl; return 0; }
»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Are there any Binary Search & (non-DP) solutions to problem D?

  • I tried implementing a solution where I Binary Searched the number of points I could gain in the Intelligence Attribute. Using this, the number of points in the Strength Attribute could be found. Now by traversing the array 'r' backwards, we could decrease the Strength and Intelligence Attributes & parallelly counting the maximum score.

  • Coming to the comparison part, I compared the score to the edge cases (i.e Intelligence = m or Strength = m), If the Intelligence edge case gives a higher score, then I would try to increase the mid value in the Binary search, and if its the other way around, I would decrease the mid value in the BS.

»
5 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

I see some people getting either WA or TLE on TC-8 of problem D, using a $$$O((m^2+n) \cdot \log n)$$$ solution. I also got stuck on this during the contest, but was able to make it work afterwards: 285935817.

The issue was that my DP was storing the maximum checks for each attribute separetely (as a pair), whereas in the accepted solution I merged them into one value. Although I'm not sure why this works. If anyone has proof of why the first approach fails, I'd be interested to know.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    facing the same issue with 286249680

    how can i correct mine?

    appreciated!

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Hey mate. I was looking at your code, but can't quite figure out how it works.

      One thing that I noticed is that you're using a std::map, which is cache-unfriendly and may be causing your algorithm to have a high constant factor. Could you try using a std::vector instead? In this problem it's possible because the values in the input sequence are limited by m.

      Other things you can try: avoid a second pass over the input array; eliminate the first pass over the dp array, by replacing it with a std::vector with zero-initialized values; use two one-dimensional dp vectors instead of a full matrix.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thank you so much.. I will surely try to implement these changes and update the results. Appreciate it alot!

»
5 weeks ago, # |
  Vote: I like it +5 Vote: I do not like it

dpforces

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Problem C: New Game Video Editorial Link : https://youtu.be/_0f9L1gBOP0?si=CHCgboEznPLOYpip

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

GG

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Did anyone consider using Euler cycles/paths on problem F?

Is it possible by applying any trick to complete those components where don't exist any Euler cycle?

I just wonder, thanks in advanced.

»
5 weeks ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

**For D, An Alternative

$$$O((m^2 + n) \cdot \log N)$$$

tabulation that worked**

Let ( dp[i][j] ) denote the maximum checks passed having acquired ( i ) points and using ( j ) into intelligence, leaving ( ij ) for strength.

Now for the dp transition, we simply check if we reached this state by using the ( i )-th acquired point into strength or intelligence and binary searching for the checks passed after this.
This can be done in

$$$O( \log N)$$$

by storing the strengths and intelligence tables where the i -th element stores a vector of sorted attribute checks of the form.

My Implementation
»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

In Problem F solution what is int u = v ^ qs[e][0] ^ qs[e][1] in DFS? Is this selecting random vertex for some reason?

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

what's wrong with my Solution D. Please someone explain. CODE

»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Is D's constraints meant to be so that $$$O(m^2)$$$ memory is too much? My DP runs in $$$O(n + m^2)$$$ with $$$O(m^2)$$$ memory but that seems to be too much. I have three $$$m \times m$$$ size arrays, with about $$$m^2$$$ states for the DP and the other two $$$m \times m$$$ frequency arrays store the number of occurences of each type of check between consecutive attribute point increases.

I think it can be reduced easily to $$$O(m)$$$ by storing only two layers and computing the frequencies during the DP but it didn't seem necessary before, I'm not too sure now. (Edit: I did this and it works now.)

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it
**Problem D** : Can someone point out the error here 
int dynamic(int ind,vector<int>&v,vector<int>&dp,int sct,int ict)
{
    if(ind>=v.size())return 0;
    if(dp[ind]!=-1)return dp[ind];
    if(v[ind]==0)
    {
        return dp[ind]=max(dynamic(ind+1,v,dp,sct+1,ict),dynamic(ind+1,v,dp,sct,ict+1));
    }
    else if(v[ind]<0)
    {
        int a=0;
        if(abs(v[ind])<=sct)a=1;
        return dp[ind]=a+dynamic(ind+1,v,dp,sct,ict);
    }
    else 
    {
        int a=0;
        if(abs(v[ind])<=ict)a=1;
        return dp[ind]=a+dynamic(ind+1,v,dp,sct,ict);
    }

}
int32_t main() {
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   
        int n,m;
        cin>>n>>m;
        vector<int>v;
        vin(v,n);
        vector<int>dp(n,-1);
        int sct=0,ict=0;
        int ans=dynamic(0,v,dp,sct,ict);
        cout<<ans <<endl;

}
»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

For problem c, my approach was to form a sorted array of pair {number, frequency} using unordered_map first. Then use sliding window to keep a track of the maximumcards that i can gather. however it throws TLE on TestCase 9. I am unable to determine why is that so. Help would be appreciated here thanks!

submission link — https://codeforces.net/contest/2025/submission/287020150

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    try using just map

    • »
      »
      »
      3 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Hey thanks, I changed unordered_map to map, and got AC. I have noticed in other problems to, where unordered_set/map gives a TLE and set/map gives AC. Is it because worst case time complexity of unordered_set/map is O(n) and for set/map it is O(log(n)) ?

      • »
        »
        »
        »
        3 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Always use map and set instead of unordered_map and unordered_set.

        Even though unordered_map and unordered_set might work during initial tests, they can fail later if someone adds a large test case that triggers their worst-case performance, which is O(n). This can cause your solution to be too slow and get a "Time Limit Exceeded" error.

        map and set are more reliable because they guarantee O(log(n)) performance.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Spoiler

»
7 days ago, # |
  Vote: I like it 0 Vote: I do not like it

why i am not able to come up with the solution of even 2nd and 3rd question contest(div2)??