cry's blog

By cry, 13 days ago, In English

1996A - Legs

Problem Credits: cry
Analysis: cry

Solution
Code (C++)

1996B - Scale

Problem Credits: sum
Analysis: cry

Solution
Code (C++)

1996C - Sort

Problem Credits: cry
Analysis: cry

Solution
Code (C++)

1996D - Fun

Problem Credits: sum
Analysis: cry

Solution
Code (C++)

1996E - Decode

Problem Credits: cry
Analysis: cry

Solution
Code 1 (C++)
Code 2 (A Little Different) (C++)

1996F - Bomb

Problem Credits: sum
Analysis: cry

Solution
Code 1 (C++)
Code 2 (C++)

1996G - Penacony

Problem Credits: cry
Analysis: cry, awesomeguy856

Solution - Segment Tree
Code (C++)
Solution - XOR Hashing (by awesomeguy856)
Code (C++)
  • Vote: I like it
  • +85
  • Vote: I do not like it

»
19 hours ago, # |
  Vote: I like it -12 Vote: I do not like it

cry orz

»
19 hours ago, # |
  Vote: I like it +28 Vote: I do not like it

Couldn't solve D. RIP my brute force skill.

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

After reading problem F You realize that nuclear bombs take so much time to bomb!

  • »
    »
    18 hours ago, # ^ |
      Vote: I like it -8 Vote: I do not like it

    sadly this is not the case in palestine!

  • »
    »
    16 hours ago, # ^ |
      Vote: I like it +15 Vote: I do not like it

    This is because all of 1,000 nuclear bombs are false and Sparkle-sama only want to fool you, haha.

  • »
    »
    110 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    orz

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

G reminds me of this JOISC problem

»
18 hours ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

In problem F: We are searching for the largest x such that f(x)≤k

Won't we reach infinity by doing that?

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

My solution for problem F:

let's find the minimum number X, where X is the smallest number that all numbers in array a can be less or equal than using at most k operations.

then, you calculate the answer for making all the numbers in a less than X, and maintain values k and a[i] for each i.

After all you will be left with k operations, so you add to the answer : number_in_a_equal_X * k

link to my solution

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

    isn't that the same solution as mentioned in the editorial?

    oh, ohk i get it, you sped up the last part of solution by doing number_in_a_equal_X * k. That's good.

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

Amazing xor-hashing solution for G, love it!

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

great contest, really fun problems. i wish i managed to solve E on time as well but oh well

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

Can anybody tell why this solution for C is getting MLE?

Submission

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

    You use map. Try replacing it with arrays and you'll be fine.

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

      yeah I also used a hashmap, but why would arrays be faster? I thought Hmaps had O(1) look up. Unless I'm misunderstanding smth

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

please can anyone explain why my code works or if it can get hacked please hack it : ), i will be so happy (https://mirror.codeforces.com/contest/1996/submission/272827778)

  • »
    »
    18 hours ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    I think it can't be hacked because worst case I found O(2 * 10^9) , and it's fast enough for your code to pass because the operations sums and multiply aren't that heavy.

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

      If in the same code we replace int by long long, it gives tle on the first test case itself. Why is this the case?

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

        Arithmetic of long long (64 bits) is slower than int (32 bits)

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

Can anyone explain how this code works? I don't understand it. Also, can you provide some sources that explain how it works? Thanks! 272756177

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

cry sum Is there a penalty on unsuccessful hacking attempt?

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

Thank you for the editorial ! There is just a little typo in the explanation of problem E. You wrote in the line before the last one $$$(x+1) \cdot ((n-y_1+1) + (n-y_2+2) + ... + (n-y_k+1))$$$. It shouldn't be $$$n-y_2+2$$$ but $$$n-y_2+1$$$

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

Solved ABCD, hope to become pupil

»
16 hours ago, # |
  Vote: I like it +44 Vote: I do not like it

O(n) solution of D :

wlog assume a <= b <= c

claim : atleast a, b <= √n

proof : if b > √n, b * c >= b * b > n which proves the claim so iterate on a and b till √n and count the permutations, this can be done in many ways

submission

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

Why will the "remaining operations will all add (x — 1)" in the solution for F?

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

    We know that there must be at least one element that is x-1, because otherwise it's clearly not the smallest x. Then, if there are less (x-1)'s than there are operations left, you could perform operations on those elements, and thus this not the smallest x. But if there were more (x-1)'s than operations left, then the they would all be (x-1).

  • »
    »
    112 minutes ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Let us note that $$$f(x - 1) - f(x)$$$ will mean how many element can start from $$$x - 1$$$ [check the last paragraph], because an increase in count between consecutive arguments can only occur if the smaller one is having some elements start for that argument,

    If x is the smallest value for which our $$$f(x)$$$ is $$$\leq k$$$, means $$$f(x - 1) > k$$$. This means that $$$f(x - 1) - f(x)$$$ number of elements have started from $$$x - 1$$$. We have $$$k - f(x) < f(x - 1) - f(x)$$$. Therefore the number of elements which will start from $$$x - 1$$$ is larger than the leftover operations.

    By elements starting from a number, I mean lets say $$$a_i$$$ = 7 and $$$b_i$$$ = 4, and $$$x$$$ = 4, then the AP of elements we can pull from this index is 7. Hence this element "starts from" 7. Also, within this example, consider what happens when $$$x$$$ = 3 (instead of 4), the AP of elements we pull is 7, 3. There is an increase in count from 1 to pulling 2 elements, and this change occurred because an element started from 3, (this 3 will not be pulled from this index if our $$$x$$$ is 4, but it will be ready to be pulled from the leftover operations).

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

DIV 3 — C

Easiest python implementation using 26 cross n array:

272842563

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

can anyone please explain me the editorial of Problem D "Since ab+ac+bc≤n , we know at the minimum, ab≤n . " or is their any other approach please share

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

    The constraint ab + ac + bc ≤ n implies that individually, each pairwise product should also be less than or equal to n . Thus, we know that:

    ab ≤ n , ac ≤ n , bc ≤ n

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

Did some people really did 4th one with brute force??

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

Why I'm Getting TLE 272870887 (problem C).

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

Great contest :) Hope to regain pupil title.

My math ain't mathing for D :( . What a mathy math

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

Anyone tried G with greedy then use segment tree? Like sort or pair by ascending order of min(b-a, n+a-b), assume a<b, then tried to input range [a,b] or [b,n],[1,a] to find which one is minimum. I do not know whether is works, as I tried sometimes but fail

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

Would a Randomizing Approach work for G? If you choose which edge to be deleted first there will be only one path which makes it clear which edges to remove after. Would randomly choosing the first edge to be deleted solve the problem (Assuming you only randomly choose from valid edges to be removed)? Too lazy to try coding it.

  • »
    »
    14 hours ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    no, consider the case where every adjacent points are friends except the two. in optimal answer only 2 of the edges can be removed so 2/n probability to choose right one

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

      I was actually thinking of only having the option to remove the edge from points that are not actually friends because it can be proven that either the answer is 1 or at some point you'd remove an edge from points that are not friends, I guess there must be a case that makes it fail but not on the top of my mind.

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

    That was my idea during the contest and it worked. However, I tried all possible edges to remove, not just by random.

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

in D, it is mentioned in the problem statement that the triplet (a, b, c) are positive numbers, i.e., > 0.

Given ab+bc+ca <= n, at minimum, ab+b+a <= n should be true, since c>=1 Which impliesb <= (n-a)/(a+1), instead of n/a as mentioned in the editorial.

When I apply these constraints, I get WA on test 2 for an unknown test case. Can someone please explain where I am going wrong ???

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

Can someone please explain why in problem 1996F - Bomb we need to add exactly (v*(k-all_ops)). In tutorials they just go through it and they don't even explain. I always trying to understand by myself till i'm done but its first time when im asking help(i watched all possible tutorials). My code 272878301 .

What i dont understand.
  • »
    »
    7 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I guess there are \textbf{at least} $$$k - all ops$$$ values $$$=v$$$

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

    Like the other guy said, there would be at least k-allops values of v, because otherwise you could use the remaining operations to eliminate v and reach a smaller value in binary search

  • »
    »
    85 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Check my comment

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

Anyone know any problems similar to D? If so can you recommend. Thanks in advance!

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

Why is this code not working?

为什么这段代码不起作用?

Почему этот код не работает?

このコードが機能しないのはなぜですか?

272886072

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

    First, don't use endl except the case which the problem is interactive problem(you will recieve an unknown verdict when you don't use 'cout.flush()' or sth like that, and endl will automatically perform cout.flush()). It is very slow.

    Second, you are outputing too much '\n'(ASCII 10)s. So even you switch it to '\n', you will still recieve a WA verdict.

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

    原来可以写中文的吗(雾)

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

      Yeah, true, but please communicate in ENGLISH.

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

非常难

»
9 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

Now i can't solve D just because i was thinking too complex... That's funny while I can solve E

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

Problem A is I think available on USACO guide or somewhere because I have solved same question with same problem statement.

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

F is equivalent to aliens' trick (A.K.A. wqs's binary search algorithm), for the functions mapping k (the number of operations) to answer/the minimum selected are both convex.

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

Problem D provides an O(n) solution where no two numbers in a,b, or c are simultaneously greater than the square root of n, so the two square roots traverse a,b, find c, and then remove the duplicates.

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

HSR reference in the questions. Am I the only one?

»
7 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

For problem G there is an interesting property that if two edges (e.g. $$$(a,b)$$$ and $$$(c,d)$$$) cut with each other, they must connect to each other $$$(a,b,c,d)$$$. So finally we will reach a partition of a circle (here I indicate a partition of a circle means that there do not exist two edges which cut with each other in two complete maps of point sets), and we can find the answer quickly by some simple method.

The problem is how to find the partition of the circle. First, we sort the edges, and then we need to perform a stack of sets which indicates that all the points in a set needs to be connected. We can easily find whether an edge cuts an edge of a set. To lower the time complexity, you will find if we pile the stack by the time we build it, if the edge cuts with one set, all the sets which on the top of this set must cut with this edge. Then just merge the sets by size. The total time complexity is $$$O(n \log n)$$$.

I must mention that I haven't implement this, but I think it is an interesting approach of this problem.

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

can anyone help why is this TLE ? problem 1996F - Bomb

Code

I literally cannot imagine edge case, feels like it should work logically

the idea is basically take the max of a[i] until it becomes smaller than second largest a[i] (if it exists)

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

    Your code will TLE in the case like this:

    Hack

    Where $$$n \ge 2$$$, and $$$k$$$ is sth very large.

    We have to compress these steps use binary search, then the rest "useful" operation will be at most $$$n$$$.

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

My thought process for C and D for newbies like me...

C

  • (obs) if we find the positions where the two sorted ranges differ we would be done
  • it was evident by the number of queries and input range that precomputation is required
  • but precomputation of where the two strings differ only if we don't have to sort each range...
  • (obs from examples) no need of sorting range, we just have to find that in the range which characters are extra in a
  • so precompute frequencies of all characters in the ranges:)
  • a hint could have been that it was mentioned only small latin chars

    D

  • (obs1): think of what the bounds on values of a,b and c are(makes sense as we need to find the count of possible tuples)
  • a,b and c can lie between 1 and min((n-1)/2, x-2) => O(max(n,x)^3) solution possible
  • (obs2): if we just iterate over a and b then number of tuples for given pair will be possible values of c and for this setup c is btw 1 and min(n-(ab)/(a+b),x-(a+b)) => O(max(n,x)^2)
  • (obs3 (which I missed in contest)) => ab is less than n so that gives us a bound on values of b too.(well this should have been obvious as otherwise why would we be given 2 inequalities one was enough for square complexity)
»
6 hours ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

;

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

Didn't understand G's train of thought

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

    Here is the explantion of the solution with segment tree.

    So first we have a cycle of $$$N$$$ nodes.

    Then, it is obvious that when we force to delete an edge, this cycle will become a chain which can be assumed to be an array.

    And we just enumerate the edge we force to delete, then use sth fast to calculate the answer of the unique way to maintain the roads.

    And to do this, a segment tree is enough.

    • »
      »
      »
      74 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I get it, because in the worst case there is a road that doesn't need to be repaired, so it's a matter of enumerating the roads that don't need to be repaired and then using the line segment tree to maintain the rest of the information

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

In problem F, I want to use slow solution for these remaining operations but get TLE on test 10. May I ask if there is any problem with this code?(https://mirror.codeforces.com/contest/1996/submission/272913613)

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

did anyone write the code for problem C in python. Mine is getting TLE

Code:

for _ in range(int(input())):
    n,q = map(int,input().split())
    a = input()
    b = input()
    dp1=[[0]*26 for i in range(n+1)]
    dp2=[[0]*26 for i in range(n+1)]
    curr1=[0]*26
    curr2=[0]*2
    for i in range(1,n+1):
        for j in range(26):
            dp1[i][j]=dp1[i-1][j]
            dp2[i][j]=dp2[i-1][j]
        dp1[i][ord(a[i-1])-97]+=1
        dp2[i][ord(b[i-1])-97]+=1
        
    for i in range(q):
        l,r=map(int,input().split())
        c=0
        for i in range(26):
            c+=abs(dp1[r][i]-dp1[l-1][i]-(dp2[r][i]-dp2[l-1][i]))
        
        print(c//2)
  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    for i in range(q):
            l,r=map(int,input().split())
            c=0
            for i in range(26):
                c+=abs(dp1[r][i]-dp1[l-1][i]-(dp2[r][i]-dp2[l-1][i]))
    
    

    u used same i,perhaps because of that ? i usually don't code in python, so is it allowed in python ?

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

在G题里面,为什么这样的维护道路方案是有效的? 我觉得sort道路长度的顺序应该也是可以的感觉

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

我不到啊orz

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

Problem E: why we have x+1 options as l? I think l should be in a range from 1 to x, then it's x , not x+1

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

Can anyone tell me how in F. How we came to know that all the remaining operation will add x-1 to the answer

»
115 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

D was such a head crusher

»
21 minute(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

I am new and i was only able to solve A and B i solved C and F but until i wrote code time was up! Any tips how should i get better?