awoo's blog

By awoo, history, 4 hours 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
  • +24
  • Vote: I do not like it

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

Finally

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

Hii everyone can anyone help me D problem?

https://mirror.codeforces.com/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

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

    https://mirror.codeforces.com/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.

»
3 hours ago, # |
  Vote: I like it +3 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

»
3 hours ago, # |
  Vote: I like it 0 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.

»
3 hours 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?

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

.

»
3 hours 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://mirror.codeforces.com/contest/2025/submission/285946605 thanks in advance !!

  • »
    »
    117 minutes 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.

»
3 hours 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.

»
3 hours ago, # |
  Vote: I like it +2 Vote: I do not like it

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

  • »
    »
    2 hours ago, # ^ |
    Rev. 3   Vote: I like it 0 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.

»
3 hours 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;

»
2 hours ago, # |
Rev. 2   Vote: I like it +1 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!

»
109 minutes 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$$$.

»
91 minute(s) 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

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

.

»
79 minutes ago, # |
  Vote: I like it 0 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.

»
69 minutes 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://mirror.codeforces.com/contest/2025/submission/286123006 link2:https://mirror.codeforces.com/contest/2025/submission/286122969

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

  • »
    »
    45 minutes 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.