maroonrk's blog

By maroonrk, history, 16 months ago, In English

We will hold AtCoder Regular Contest 164.

The point values will be 300-500-500-600-700-900.

We are looking forward to your participation!

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

| Write comment?
»
16 months ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

Good luck to all participants.

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

I keep participating in coding contests like a true code connoisseur, gracefully decreasing my rating with each attempt. It's like I have a secret mission to redefine the concept of 'progress' in the coding universe!

»
16 months ago, # |
  Vote: I like it -9 Vote: I do not like it

java 20 when

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

The first question was quite interesting :)

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

The webpage keeps returning 403. Does anyone have the same problem?

»
16 months ago, # |
  Vote: I like it +12 Vote: I do not like it

Is the website down for about half an hour? I received 403 status code.

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

Is this closer to div2 or div1?

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

    div 1 i guess

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

    This current contest or ARC usually? Usually ARC falls in between the two, I'd call it Div 1.5 compared to CF contests. I don't know if I can give my opinion on the current difficulty during the contest, but you can check the solve counts of problems and compare them to previous ARC's.

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

D seems impossible :/ , would appreciate it a lot if anyone could explain it after the contest , I feel like there is some interpretation or observation that makes things much easier , couldnt noticed it tho

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

    Each substring of balanced charges (sum = 0) should cancel each other out separately.

    Try to implement F(s) for some minimal input strings s of balanced charges (minimal meaning there is no prefix of sum = 0). You should notice that $$$F(s) = s$$$ or $$$F(s) = s'$$$ depending on whether $$$s$$$ starts with a $$$+$$$ or $$$-$$$. ($$$s'$$$ is the opposite of $$$s$$$)

    We can reduce this to counting the number of balanced bracket strings, where we allow the string to start with either $$$)$$$ or $$$($$$, and calculating the sum of $$$R - L$$$ for all matching brackets for each string using dp.

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

$$$D$$$ has very complex model. It took me much time to understand, what happens. There should be more complex samples, as with such first sample I spent much time writing solution, which finds incorrect thing.

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

The problems are really interesting and challenging. (for me)

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

I think I've just participated in ParityForces.

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

AC'ed E 47s after the contest ended. My contest ruined by the corner case for [L, R]=[1, n].

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

    I've covered this case but I still was having 10 WAs. 3 minutes before the end of the contest I just decided to delete my DP completely and write it again from scratch, and it worked...

»
16 months ago, # |
  Vote: I like it +23 Vote: I do not like it

E is a very good problem, though it would be better to add some corner cases in the sample :(

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

Can anyone tell what's the problem in my code? It passed all test cases except 2 :(.

The question in B- Switching Travel.

Code: https://atcoder.jp/contests/arc164/submissions/43434476

I used the same logic as given in editorial, find cycles and checking if there exists a cycle with alternating node color except 2 consecutive nodes.

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

in problem B I find all cycles in the graph and then for every cycle, if is from pattern 10101... or 010101... etc , I test all possible starts in the cycle for example 100 is also valid because I can express it as 010 if i start from second position get all test ac but wa in the last test can anyone provide me a counter test for my solution because i can't find it

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

My code for B:

...
bool h[400012];
int de[400012];
bool alr[400012];
int cnt=0;
bool dfs(int x,int d,int y)
{
	cnt++;
	if(!y) alr[x]=true;
	if(de[x]!=-1&&de[x]%2!=d%2&&y==1) return true;
	if(y>=2) return false;
	if(de[x]!=-1) return false;
	de[x]=d;
	int ii=fdlks.first[x];
	while(ii!=-1)
	{
		if(dfs(fdlks.sds[ii].to,d+1,y+(h[x]==h[fdlks.sds[ii].to]))) return true;
		ii=fdlks.sds[ii].next_side;
	}
	de[x]=-1;
	return false;
}
...

This one got AC, but get $$$cnt = 1.26 \cdot 10^8$$$ in Test 40, which is obivously not $$$O(N)$$$. Is it $$$O(N^2)$$$?

(Whole code: Submission 43433400)

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

Can anyone provide the logic or observations in problem D, I was not able to think of any during the contest.

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

    I don't AC it during the contest as C takes up me too much time.

    The observation is that,

    Case 1: the acculumated Coulomb in interval $$$[1, i] \geq 0$$$ and s[i+1]=='+', then $$$(i+1)$$$ contributes $$$-(i+1)$$$ to each case.

    Case 2: the acculumated Coulomb in interval $$$[1, i] <0$$$ and s[i+1]=='+', then $$$(i+1)$$$ contributes $$$(i+1)$$$ to each case.

    Case 3: the acculumated Coulomb in interval $$$[1, i] \leq 0$$$ and s[i+1]=='-', then $$$(i+1)$$$ contributes $$$-(i+1)$$$ to each case.

    Case 4: the acculumated Coulomb in interval $$$[1, i] >0$$$ and s[i+1]=='-', then $$$(i+1)$$$ contributes $$$(i+1)$$$ to each case.

    For example, s = '+-+-', the expression is $$$2-1+4-3$$$, and the contribution of index $$$1$$$ and $$$3$$$ are $$$-1$$$ and $$$-3$$$, respectively.

    Here the word each case might be a little bit ambiguous. For example, in case 1, each case means the case where the accumulated Coulomb in $$$[1, n]$$$ is non-negative. Therefore, we have to maintain two arrays: (1)$$$dp[i][c]$$$: We have handled $$$i$$$ elements and the accumulated Coulomb is $$$c$$$; (2)$$$num[i][c]$$$: The number of cases where the accumulated Coulomn in the first $$$i$$$ elements is $$$c$$$. We need to maintain $$$num[i][c]$$$, as in the observation above, we need to multiply the contribution of each case by the number of cases.

    Code: https://atcoder.jp/contests/arc164/submissions/43436250

    F**k, the Devil is in the Details (WA): https://atcoder.jp/contests/arc164/submissions/43435075

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

Damn! Miss ARC D because I use = instead of +=.

Anyway I debugged this 15 minutes after contest, that is not a short time so I deserve it.

WA: https://atcoder.jp/contests/arc164/submissions/43435075

AC: https://atcoder.jp/contests/arc164/submissions/43436250

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

How do you solve A? I followed the logic in the editorial but still got WA. Here's my last submission: https://atcoder.jp/contests/arc164/submissions/43432250.

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

    you can first convert the given number N to base 3 and see how many minimum powers of 3 can be used to represent N. Lets call this value OP. If the given K is less than OP, then the ans is NO. Otherwise we can se 3^x = 3 * 3^(x — 1) which means one power of 3 can be replaced by three powers of 3 thus incrementing the total powers by 2. So just subtract OP from K and if the remaining K is even, then the ans is Yes otherwise No

    Code

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

For E, I had a O(n^3 + Q) solution, with dp of the form:

dp[L][R] = min( max(combine(dp[L][k], dp[k+1][R]), {1, #queries intersecting [L,R]*2})) over all k inside [L,R].

Can this be optimised by knuth as shown in here: https://cp-algorithms.com/dynamic_programming/knuth-optimization.html#generic-implementation ?

It seem to give WA for some reason. Is the cost function and combine function (instead of simple plus) valid to be used here? (Brute n^3 solution seem to work and give TLE instead so the dp should be correct)

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

    I solved it by n^3 dp and knuth optimization, it’s right.

»
16 months ago, # |
  Vote: I like it +70 Vote: I do not like it

Thank you for your participation!

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

Enjoyed the contest. Had fun solving D. I see most of the people tried to do it using combinatorics I had a rather simple solution using dp. I had two dp , $$$dp[index][balance$$$ $$$till$$$ $$$index$$$ $$$i]$$$ = sum of distances till index i, $$$ways[index][balance$$$ $$$till$$$ $$$index$$$ $$$i] =$$$ # of strings with the current balance. The transitions would be $$$dp[index + 1][balance$$$ $$$till$$$ $$$index$$$ $$$i$$$ $$$+ (s[i] ==$$$ '+' $$$? +1 : -1)]$$$ += $$$dp[index][balance] + ways[index][balance] * abs(balance + (s[i] ==$$$ '+' $$$? +1 : -1)$$$.

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

any one can give me hints or the full idea for C?

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

    I thought it like this.

    Let's say initially the score is sum of every red value and Alice has certain deltas by which she can change the score which are all $$$b_i - a_i$$$. Alice in every step would choose the smallest of this deltas (let's call it $$$x$$$), add it to the score, remove it from the deltas set and insert $$$-x$$$ to it. Bob in every move would remove the smallest delta. In this way you can simulate the game.

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

Whats the deal about drafts by gpt-4 ?
Could it really solve even the last question ...

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

    No, it probably couldn't even solve the first question. The editorials are translated from the japanese versions using gpt-4.

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

I think I have a cleaner and better solution for problem D. (I read other people's solutions as well as the editorial, but I didn't understand anything from them, so I think it's worth to mentioning it.)

You can just skip this to the end and see the code and think about it to see how it actually works!

Simpler Version (sub-problem)

First suppose our string is fixed and there is no $$$?$$$ in it.
We call this string $$$s$$$.

We will use Double Counting Technique (in CP, it is known as Contribution to the sum Technique).

Definitions:

  • i-th Gap: i-th gap is the gap between number i and i+1 in 1-dimensional coordinate which in the initial state i-th ball and (i+1)-th ball placed in them.

  • Ball Pair: The pair of a particular ball is a ball in which the two cancel out each other in some position and both disappears.

  • $$$ans_i$$$: The sum of the movements that the balls made on the i-th gap.

There are two cases for a ball moving in i-th gap:

  1. If the ball disappears in this gap, it can also be concluded that its pair is also moving in this gap from opposite direction and somewhere in the middle they meet each other and will disappear. In this case the ball and its pair both together contribute 1 to the $$$ans_i$$$.
  2. If the ball doesn't disappears in this gap, it can also be concluded that its pair doesn't even enter in this gap. In this case we contribute 1 to the $$$ans_i$$$.

Actually in both cases the ball and its pair both together contribute 1 to the $$$ans_i$$$.

Now, using the above facts, it is enough to know how many balls enter in the i-th gap from the left side of it. This number is equals to absolute of cumulative sum of ballses values in left of this gap (*).

Finally we have $$$p(s) = \sum_{i=1}^{n-1} ans_i$$$.

Original Problem

But now how to solve the original problem!?

Definitions:

  • Balance of a prefix: sum of +1 and -1 in that prefix.

  • Pattern: a string $$$s$$$ with size $$$i$$$ has pattern of the prefix $$$t[1...i]$$$ if it can be obtained from $$$t[1...i]$$$ by only replacing $$$?$$$ with $$$+$$$ or $$$-$$$.

  • $$$f(i, b) := $$$ number of strings which has pattern of prefix $$$t[1...i]$$$ and the balance of them is $$$b$$$.

  • $$$g(i, b) := $$$ sum of the $$$\sum_{j=1}^{i} ans_j$$$ (with the same definition as to solve the simpler problem that doesn't include $$$?$$$.) for every possible string $$$s$$$ which has the pattern of prefix $$$t[1...i]$$$ and the balance of that is $$$b$$$. (Note that this value actually stores the sum of partial sum for $$$ans_{j=1...i}$$$)

Now to calculating the values of two functions $$$f$$$, $$$g$$$ recursively we need to consider three cases:

"$$$\times |b|$$$" part for each case comes from the sub-problem that we solved.

  1. $$$t[i]= \space$$$'$$$+$$$':
    • $$$f(i, b) = f(i-1, b-1).$$$
    • $$$g(i, b) = g(i-1, b-1) + f(i-1, b-1) \times |b|.$$$
  2. $$$t[i]= \space$$$'$$$-$$$':
    • $$$f(i, b) = f(i-1, b+1).$$$
    • $$$g(i, b) = g(i-1, b+1) + f(i-1, b+1) \times |b|.$$$
  3. $$$t[i]= \space$$$'$$$?$$$':
    • $$$f(i, b) = f(i-1, b-1) + f(i-1, b+1).$$$
    • $$$g(i, b) = (g(i-1, b-1) + f(i-1, b-1) \times |b|) \space \space + \space \space (g(i-1, b+1) + f(i-1, b+1) \times |b|).$$$
  • Base Cases: $$$f(i=0, b=0) = 1, \space \space g(i=0, b=(-n)...(+n)) = 0.$$$

Now we can just implement this two functions using $$$dp$$$ and also shift $$$b$$$-dimension by $$$+n$$$ to avoiding "Out Of Memory Error".

Here is my submission (using forward dp)

(*): We don't care about how actually pairing of balls done before the i-th gap we just care about how many of them remains unpaired in left of i-th gap, the number of them is the number of remained + or - in left of this gap (or to right depend on how you implement it) which is equals to absolute of cumulative sum of ballses values in left of this gap. but why? If you are ok with its intuition, you don't need to read the rest of this section.

Here we should see where is pair of a ball actually located? if you use two stack one for '$$$+$$$' and call it $$$s_+$$$ and one for '$$$-$$$' and call it $$$s_-$$$ then each time you encounter a '$$$+$$$' you should pair it with back of $$$s_-$$$ if it's not empty otherwise you should add it to back of $$$s_+$$$ and same thing for when you encounter a '$$$-$$$'.

But again, why? We can use kinda inductive prove, assume that all pairs are correctly found before the i-th gap,
now without loss of generality suppose we are going to pair a '$$$+$$$' to a '$$$-$$$' at the back of $$$s_-$$$, this means that both of them must move towards each other and eventually meet somewhere in the middle so it's enough to prove that $$$F$$$ of our '$$$+$$$' is negative and $$$F$$$ of our '$$$-$$$' is positive.

We already know $$$F$$$ of our '$$$-$$$' is positive, because it is not paired with anyone on its left side, which means that the sum of its left side is a non-positive number suppose this number is $$$X$$$ then it means that $$$F$$$ of our '$$$-$$$' is equals to $$$(X - (0 - X + 1)) \times (-1) = -2X + 1 \gt 0$$$ ($$$X$$$ is the sum of its left and $$$(0 - X + 1)$$$ is the sum of its right and it comes from the fact that the total sum is $$$0$$$).

And now what about $$$F$$$ of our '$$$+$$$'. In fact, because everything we had between these two are paired together, so the number of '$$$+$$$' and '$$$-$$$' between them is the same, so we can ignore them while calculating $$$F$$$ of our '$$$+$$$', so it means that $$$F$$$ of our '$$$+$$$' is equals to $$$((X - 1) - (0 - (X - 1) - 1)) \times (+1) = 2X - 1 \lt 0$$$. $$$\blacksquare$$$