valerikk's blog

By valerikk, history, 18 months ago, In English

Hello, Codeforces!

I invite you to participate in Codeforces Round 876 (Div. 2), which will be held on Jun/03/2023 17:35 (Moscow time).

The round will be rated for participants with rating lower than 2100. Participants with higher rating can participate in round unofficially.

You will be given 5 problems and 2 hours to solve them. I recommend you to read all problems. One of the problems will be interactive, so please read guide for interactive problems before the contest.

All problems are authored by me.

I would like to thank:

Score distribution: 500 — 1000 — 1500 — 2250 — 2750.

Good luck to all participants!

Editorial has been published.

Winners:

  1. arvindf734
  2. caubetraidatmarvin
  3. AmazingTalker_Frank
  4. Perfectt
  5. 1437vszombies

Unofficial winners:

  1. Geothermal
  2. Rubikun
  3. Roundgod
  4. neal
  5. A_G

First-to-solve:

  • Vote: I like it
  • +336
  • Vote: I do not like it

| Write comment?
»
18 months ago, # |
  Vote: I like it +30 Vote: I do not like it

Auto comment: topic has been updated by valerikk (previous revision, new revision, compare).

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

As a tester, I really loved the problems! Don't miss an opportunity to take part in a contest written by the IOI 2023 Winner ;)

»
18 months ago, # |
  Vote: I like it +20 Vote: I do not like it
»
18 months ago, # |
  Vote: I like it +11 Vote: I do not like it

hopefully +delta

»
18 months ago, # |
  Vote: I like it +24 Vote: I do not like it

5 problems round after a long time, will enjoy this round!

»
18 months ago, # |
  Vote: I like it +8 Vote: I do not like it

as a tester, the problems were great. Recommend participating.

»
18 months ago, # |
  Vote: I like it +11 Vote: I do not like it

Good luck to everyone participating!!! Also on a random note, what song have you been listening to lately?

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

I enjoy contests which have only 5 problems with no subtasks

»
18 months ago, # |
  Vote: I like it +28 Vote: I do not like it

As a tester, ahmet23 orz!..

»
18 months ago, # |
  Vote: I like it -11 Vote: I do not like it
»
18 months ago, # |
  Vote: I like it +9 Vote: I do not like it

As a purple tester, round is very good!)

»
18 months ago, # |
  Vote: I like it +8 Vote: I do not like it

Thanks Codeforces for another contest!

»
18 months ago, # |
  Vote: I like it +11 Vote: I do not like it

As a tester orz

»
18 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Codeforces contests help me overcome the exams period.

»
18 months ago, # |
  Vote: I like it +27 Vote: I do not like it

As not a tester, i didn't tested.

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

How to unregister for a contest ?

»
18 months ago, # |
  Vote: I like it +38 Vote: I do not like it

My first unrated Div 2! Excited!

»
18 months ago, # |
  Vote: I like it +1 Vote: I do not like it

hopefully reach cyan

»
18 months ago, # |
  Vote: I like it +1 Vote: I do not like it

orz sir

»
18 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Thanks Codeforces

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

Good luck to all participants. Wishing you a positive delta!

»
18 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Waiting....

»
18 months ago, # |
  Vote: I like it +9 Vote: I do not like it

Score distribution when?

»
18 months ago, # |
  Vote: I like it +3 Vote: I do not like it

I don't know when will be my "Promotion to CM" round!!

»
18 months ago, # |
  Vote: I like it -19 Vote: I do not like it

Hope no adhoc forces

»
18 months ago, # |
  Vote: I like it +13 Vote: I do not like it

Ormlis Orz

»
18 months ago, # |
  Vote: I like it +17 Vote: I do not like it

Score distribution gives speedforces vibes.

»
18 months ago, # |
  Vote: I like it +13 Vote: I do not like it

Really Hope the interactive problem Is D or E

»
18 months ago, # |
  Vote: I like it -14 Vote: I do not like it

Its been long time since we encountered Good Binary Search problem. Hoping to get one.

»
18 months ago, # |
  Vote: I like it +2 Vote: I do not like it

As a participant, I will participate and try the best for great problems today.

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

Rank 400 and Rank 2500 both solved same number of problem:)

»
18 months ago, # |
  Vote: I like it +10 Vote: I do not like it

Problem D:

The first line contains two integers $$$n$$$ ($$$1 \le n \le 500$$$) — the number of balls.

should be

The first line contains integer $$$n$$$ ($$$1 \le n \le 500$$$) — the number of balls.

  • »
    »
    18 months ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    That's

    The first line contains an integer $$$n$$$ ($$$1 \le n \le 500$$$) — the number of balls.

»
18 months ago, # |
  Vote: I like it +32 Vote: I do not like it

PLEASE FOR THE LOVE OF GOD STOP SPEEDFORCES!

MAKE ACTUAL CONTESTS!!

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

Time limit for B was very strict... Am I the only one who had troubles with TL in B?

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

    Is it possible to solve B using priority queue by pushing pair<index, pair<a , b> > into it in an ascending order . Any ideas ??

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

      Maybe, but you don't really need to make it that complicated,better to use the simple solution

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

A: For each 0<=i<=n, there are at least ceil(i/k) ones on the prefix with length i, and ceil((n-i)/k) ones on the corresponding suffix, therefore ans>=ceil(i/k)+ceil((n-i)/k). We can solve the problem by brute force for 0<=i<=n.

B: Let's denote S[k]={1<=i<=n: a[i]==k}. For each integer k, we can get points from at most k lamps in S[k], because all lamps in S[k] will break simultaneously, and there we can turn on no more than k lamps in S[k] before they break. So we can classify lamps by a[i], and choose min(k,S[k].size()) lamps with maximum b[i], then we can get an upper bound of the answer. In fact, this answer is also a lower bound: If we turn on the chosen lamps by the non-decreasing order of a[i], because lamps with lower a[i] will break strictly before lamps with higher a[i], they will not make lamps with higher a[i] break before turned on, so we can get points from all chosen lamps. Therefore we can solve the problem in O(n).

C: By induction we can prove that the last element of a is 0.

Proof

So if b[n]=1, there's no solution. Otherwise we can construct a solution: First we let ans be an empty list. Then we read b[i] reversely: Let i be the largest index such that b[i]=1 and b[i+1]=0, we can assume there's an operation with p=i+1, so we append i+1 to ans and filp elements on [1, i]. If there's no such i, we end this process, append zeros to make ans.size()==n and reverse it as the answer.

D: Let [L1, R1], [L2, R2], ..., [Lm, Rm] be the ranges of indexes which has been moved in operations, then other indexes must form an increasing subsequence, and we must add a 0-ball in each [Li, Ri]. Therefore, we can solve an another problem: We can remove at most k subarrays from c[i] to make it increasing, minimize the number of elements removed. We can solve this problem by naive O(n^3) dp.

E: For any subset of {1, 2, ..., n}, we denote it S1 and it's complement S2, if sum(i in S1)(a[i])=sum(i in S2)(a[i]), then the second player can win the game: If the first player choose i in S1, the second player choose j in S2, and vice versa. We can see by this strategy sum(S1) will always be equal to sum(S2), so the first player has valid move in S1 --> sum(S1)>0 --> sum(S2)>0 --> the second player has valid move in S2. Therefore, the second always has valid move before the first player lose the game. If there're no such S1 with this property, the first player will always win: Assume for all subset S1 we have sum(S1)!=sum(S2) before a turn, and assume indexes chosen in this turn are i, j, WLOG assume a[i]>=a[j]. Then if there sum(S1)==sum(S2) after this turn, if i belong to S1, then we can assume j belong to S2(because a[i]>=a[j] before this turn, we have a[j]==0 after this turn, so move it from S1 to S2 will not change sum(S1)-sum(S2)), then we have sum(S1)==sum(S2) before this turn, which is a contradiction, so for all subset S1 we have sum(S1)!=sum(S2) after this turn. If the first player lose the game, when there's 2 positive elements in a[i], assume they are i1, i2, then we have a[i1]==a[i2] so that first player has no valid move in next turn. But in this case if we let S1={i1} then sum(S1)==a[i1]==a[i2]==sum(S2), which is a contradiction, so the first will always win the game.

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

    My solution for A is: ((n-2) / k) + 2.

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

      Can you please explain the reason, like how you came to this conclusion?

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

        you will always need an element at the first and last position after that you can divide the inner n-2 elements into partitions of size k and insert an element in each and you should always have an answer.

  • »
    »
    18 months ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    Very fast editorial...

»
18 months ago, # |
  Vote: I like it +21 Vote: I do not like it

I liked problem $$$D$$$. First, its obvious that the sequence of colors of those non-zero color balls on which we don't perform an operation must be an increasing sequence. After that its $$$O(n^3)$$$ dp.

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

    I could see that we only have to perform operations on elements not present in the increasing subsequence, how did you minimize it using dp tho?

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

      Just see the general structure of a solution. If $$$X$$$ denotes the non zero color element on which I perform operation and $$$Y$$$ denotes the non zero color element on which I don't perform operation, then if my non-zero array of balls looks like this,

      $$$XXXYYXXYYYXXY$$$

      Then, I can insert a single zero color ball on every segment of $$$X$$$. (Here I require at least $$$3$$$ zero color balls and cost is $$$7$$$ units because I'm moving $$$7$$$ non zero color balls). Also another constraint is that sequence of $$$Y$$$ must form an increasing color sequence. So, that's why we can define $$$dp[i][j]$$$ as the min cost required such that there are exactly $$$j$$$ segments of $$$X$$$ (so at least $$$j$$$ balls of zero color required) and an operation is performed over the $$$i^{th}$$$ non zero color ball.

      Transitions:

      $$$dp[i][j] = min(dp[i][j],dp[w][j-1] + i-w-1) \forall w < i-1 $$$ and $$$c[w] < c[i]$$$

      That's because if I'm performing operation on $$$i^{th}$$$ non zero color element, then the last element $$$w$$$ on which I perform operation, its color must be less than $$$c[i]$$$ also, since $$$w<i-1$$$ there is a segment of non zero length which requires a ball of zero color to be placed inside it.

      $$$dp[i][j] = min(dp[i][j],dp[i-1][j])$$$ if $$$c[i-1] \le c[i] $$$ This case is trivial.

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

OMG i solved the last problem of the round for the first time

what's funny is that i was writing the code like "ok i obviously wont finish in time, right"

Never give up!!! great round, although really speedforcish, luckily not for me

  • »
    »
    18 months ago, # ^ |
      Vote: I like it +25 Vote: I do not like it

    Amazing! Yeah it was speedforces till problem A, B, C. From D onwards the problems were nice.

»
18 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Cool problems! Really, for me, A, B, C are tasks where you have to think instead of looking at the tests and finding a correlation, like in the previous recent rounds.

»
18 months ago, # |
  Vote: I like it +10 Vote: I do not like it

Read B for a long time, more than half of the time thought ai is three states, ha ha ha ha

»
18 months ago, # |
  Vote: I like it -7 Vote: I do not like it

ADHOCforces

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

HELP in Problem C I was able to recognize that if last element was "1", then it was impossible thus "NO" is the ans. But was not able to get "b" array. Please guide.

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

    Try doing it backwards. Start from array b and reach empty array by only deleting 0 and flip

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

    You are correct. Now assuming that A, is basically a series of one '0', each followed by zero or more '1's, when scanned from right to left, try to handle each group of '0' led by zero or more '1's seperately (from right to left).

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

    You try to make the array from the back eg 1 1 0 so try to form from the back. first, we insert at the 0th index (0) then again at the 0th index (00) now at the 2nd index (110). if we have all zeros then we add every 0 at 0 index. What happens when we have consecutive 1's like k ones Eg:- (1 1 1 1 1 0) we have 5 ones here we can first add 0 at 0th index(0) and for 5 ones we add 4 zeros at 0th index (0 0 0 0 0) now to form consecutive 1's add 0 at 4th index (1 1 1 1 1 0)`

    In short we traverse from the back, if we have 0 then we print 0 if we have k consecutive ones we print k — 1 0's and 1 k.

    My Solution

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

what's the point of setting the memory limit for problem D to 256MB ?!

»
18 months ago, # |
  Vote: I like it +34 Vote: I do not like it

imo E is easier than D but I just need one more minute so submit it...

»
18 months ago, # |
  Vote: I like it +13 Vote: I do not like it

Can someone explain to me how D is solved by $$$O(n^3)$$$ dp? I know that the non-zero color balls which we don't perform an operation form an increasing sequence, but I never thought about $$$O(n^3)$$$ dp.

Also, maybe someone can give me a little E idea?

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

Why my O(n^3) dp solution for problem D got TLE? 208344465

»
18 months ago, # |
Rev. 3   Vote: I like it +11 Vote: I do not like it

Although my solution of E is wrong, why did I get penalties because I got WA/RE on pretest 2 which's a sample?

  • »
    »
    18 months ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    Here's one of my submissions. This code can pass the samples locally, so is there something wrong with the verdict?

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

      Sadly, you don't get penalty only on pretest 1. Even if there are many of them.

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

My O(nlogn) Python solution for problem B gave me TL. Could someone help me identify how I could have made it pass?

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

Why I got RTE on Main Test 5? 208322908 Can anyone please explain? I can't find the error.

  • »
    »
    18 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    bool cmp(ii a, ii b){
        if(a.ff<b.ff){
            return 1;
        }
        if(a.ff>b.ff){
            return 0;
        }
        return a.ss>=b.ss; //-> replace it by a.ss > b.ss;
    }
    

    While making a comparator make sure there is no = operator in it.

»
18 months ago, # |
  Vote: I like it +10 Vote: I do not like it

My feedback: a round is susually concidered unbalanced when you can get 2300 performance while only solving ABCD. This round, you could get 2300 performance for solving ABC.

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

When will the Editorial for contest will be released ?

Not able to solve D, waiting for the approach in editorial.

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

Thank you for rejudging my submission for QD multiple times :)

I got a bunch of TLEs and then finally ACed with 1965ms :)))))

(BTW, is it my problem or is my solution not intended? The time limit seems a bit strict for O(n^3))

»
18 months ago, # |
  Vote: I like it +10 Vote: I do not like it

Although the first 4 problems are not fun to solve... but problem E is beautiful!

»
18 months ago, # |
  Vote: I like it +3 Vote: I do not like it

I'm really happy with how I did in this contest, but I honestly had no idea that an O(N^3) solution for D would pass. I've been using C++ and Python interchangeably, but I guess I've been using Python for too long if I thought that would be too slow :)

Thanks to the writer and testers.

»
18 months ago, # |
  Vote: I like it +2 Vote: I do not like it

I like the tasks but half of problem D was figuring out what the statement is trying to say

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

Can any one please help during contest on second question submission it is showing error exit code: -1073741819 (STATUS_ACCESS_VIOLATION), checker exit code: 0, verdict: RUNTIME_ERROR

Your text to link here... please helpe??

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

    static bool comp(pair<long long,long long>&p1,pair<long long,long long>&p2){ if(p1.first < p2.first){ return true; }else if(p1.first == p2.first){ if(p1.second>= p2.second){// -> change this by p1.first > p2.second; return true; } return false; }else{ return false; } //also a smart way of doing this is //if(p1.first == p2.first)return p1.second > p2.second; //return p1.first < p2.first; }

    Never use a = in your comparator for some reason it gives segmentation fault.

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

Enjoyed all the problems.

»
18 months ago, # |
  Vote: I like it +17 Vote: I do not like it

Ratings updated preliminary, it will be recalculated after removing the cheaters.

»
18 months ago, # |
  Vote: I like it -19 Vote: I do not like it

from collections import defaultdict from collections import Counter from itertools import combinations from itertools import permutations import heapq import math import bisect

def presum(arr): d = [] sum1 = 0 n = len(arr) for i in range(0,n): sum1 += arr[i] d.append(sum1)

return d

def factorial(mod):

p = 1
d = [1]
for i in range(1,10**5 + 1):
    p = p * i
    p = p % mod
    d.append(p)

return d

def lst1(): return list(map(int,input().split()))

def map1(): return map(int,input().split())

def int1(): return int(input())

t = int1()

while t!=0:

n = int1()

dict1 = defaultdict(list)

for i in range(0,n):
    a,b = map1()
    dict1[a].append(b)

for i in dict1.keys():
    dict1[i].sort()



e = sorted(dict1.keys())

dict2 = defaultdict(int)

on = 0
sum1 = 0
d = []
"""
for i in e :

    while len(dict1[i]) > 0 and on < i :
       print("i :",i)
       print("Dict2[",on,"]:",dict2[on])

       print("On :",on)
       x = dict1[i].pop()
       print("x :",x)
       sum1 += x
       print("Sum1",sum1)
       d.append(x)
       print("D:",d)
       on += 1
       print("On :",on)
       dict2[i] += 1
       print("Dict2[",i,"] :",dict2[i])
       print("Dict2[",on,"]:",dict2[on])
       on -= dict2[on]
       print("On :",on)
       print("Dict2[on] : ",dict2[on])
       dict2[on] = 0
       print("dict2[on]:",dict2[on])
       print()
    if on == i:
       on -= dict2[on]
       print("ON : I :",on)
       dict2[on] = 0

print(d)
print(sum1)

"""

for i in e:

    while len(dict1[i]) > 0:


       x = dict1[i].pop()
       sum1 += x
       d.append(x)
       on += 1
       dict2[i] += 1

       if on == i:
         val = dict2[on]
         on -= val
         dict2[on + val] = 0
         break
       else :
         val = dict2[on]
         on -= val
         dict2[on + val] = 0


print(sum1)








t = t - 1

B)Lamps I solved this in the contest in PyPy 3 interpreter which is supposed to be faster than Python 3.8 interpreter and after the contest was over i just changed the interpreter to Python 3.8 and It was Accepted . This Solution gave Time Limited Exceeded on testcase 3 on PyPy 3 and Accepted Verdit on Python 3.8 . I Cant Understand why this happened but my solution was correct during the contest

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

isn't the 6th example in the A wrong?

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

    Why do you think it is wrong? $$$[1,0,0,0,1,0,0,0,1]$$$ is good, you can't use less ones than that.

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

      i/k = 5/5 = 1 != 2 Or i didn't understand the problem '_'

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

        the question said that at-least [i/k] elements should be 1 in the first i elements and in the last i elements.

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

Why doesn't this round have 12 hours hacking phase, or am I misunderstanding something?

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

    That is only for educational/div3 or 4 rounds.

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

My solution gave TLE on Test Case 3 on Pypy 3 interpreter during the contest and gave the Accepted Verdict on Python 3.8 when I upsolved the contest.I didnt even change a single word when sumbitting on Pyth3.8 Pls tell me the issue

»
18 months ago, # |
  Vote: I like it +20 Vote: I do not like it

Thanks! Loved E a lot!

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

C was fun. Good problems. Enjoyed it

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

Problem D be like :

... so the ball is a male

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

The presets in B are weakly designed, can't even catch a simple runtime.

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

Can someone please explain why this submission gives RE on test 5 of problem B? I looked at some previous comments and this happened when they used "=" sign in comparator, but I have not done that also.

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

Thank you to everyone who helped with this round! I found C, D, and E all very enjoyable (maybe E is a bit easier than normal but that is not necessarily a bad thing in my opinion, since it's not easy enough to decrease the fun i had :P )

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

emm,I am told that the solution conflicts with others, in what way should I prove that this is a coincidence?

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

Dear MikeMirzayanov I recieved a mail regarding my solution for 1839C - Insert Zero and Invert Prefix which is 208330178 , is similar to some other people . I sincerely request to please reconsider , as I made the intuition and solution completely myself on pen and paper . The Xor trick in this question is something I was very happy to crack and once u get the intuition , the code was quite simple . I have been doing codeforces for quite a long time , I enjoy giving more and more contests and this is definetly a mere coincidence and the problem code was quite straight forward. Also u can see my previous submissions to many problems , it can clearly been seen that it is my my own code with similar intuition to others. Please dont skip my solutions as my happiness was at peak solving the 3rd question in this contest.