atcoder_official's blog

By atcoder_official, history, 9 months ago, In English

We will hold Toyota Programming Contest 2024#3 (AtCoder Beginner Contest 344).

We are looking forward to your participation!

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

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

Good luck to everyone:)

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

Hope G be as friendly as previous rounds.

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

Good luck to everyone!

»
9 months ago, # |
Rev. 3   Vote: I like it -15 Vote: I do not like it

[Deleted]

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

why T.size() — sz gives some random large value wasted 1hr debugging this

  • »
    »
    8 months ago, # ^ |
    Rev. 4   Vote: I like it +1 Vote: I do not like it

    Because sz can be larger than T.size(), and since T.size() is an unsigned int it will overflow when doing the subtraction.

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

      oh thanks! didn't know about this.

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

        It is strange though. Looking better at your code both the numbers are integers, so it should give you an integer and not unsigned. Idk if what I told you above is right.

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

          Sorry the code I attached is correct ac code which I submitted after debugging

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

Same idea problem to E, D. Berserk Monsters

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

Any hint for problem 'E — Insert or Erase' would be appreciated.

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

Can anyone help me with my submission of Problem E:-

My logic is maintaining 2 maps, one for storing the next element and one for storing the previous element.

It's failing on 2 test cases :- random_12.txt and random_21.txt

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

I feel a little confused about the editorial of problem F. The dp state is dp[i][j][pmax] = pair<number of action, current earned money>. I think the editorial means that, for the same pmax, there can only be one single optimal pair of <number of action, current earned money>.

But, we can reach cell (i,j) from different ways, and I think it is possible that, there are two ways, so that, we have the same pmax, but different <(number of action)_1, (current earned money)_1>, and <(number of action)_2, (current earned money)_2>, where (number of action)_1 is less than (number of action)_2, while (current earned money)_1 is "very very"" less than (current earned money)_2.

In other words, one of the way has more actions, but very very much earned money, and how to determine which one is better? If it can not be determined, then the number of states may be much more larger than O(N^4)

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

    You are confusing DP states with DP values. You are also confusing current earned money with the left over money. I've added more intuition for this DP solution on CF Step

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

    I have the same doubt I do not see how we can simply choose the less number of actions over more number of actions when we have money left under consideration too.

    What is the guarantee that this local optimization reflects globally as well.

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

      We are not, we actually evaluate both possibilities. As I discussed in my previous comment, if you have to spend 5 actions to go to the right cell with $$$p[i][j + 1] = 10$$$, and spend 10 actions to go to the down cell with $$$p[i + 1[j] = 300$$$, we do not discard the downward path, we actually consider both possibilities.

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

    I think this is the same confusion I had when I was doing the VC. I worked on a proof to understand why I had this misconception.

    Take a scenario where you are moving to cell (i, j) from previous cells (i, j — 1) and (i — 1, j) with the same pmax value. And suppose the pmax doesn't change at cell (i, j). These represent dp state transitions from (i, j — 1, pmax), (i — 1, j, pmax) -> (i, j, pmax).

    Suppose you have the following possible values for state (i, j, pmax), (a, x), where a = number of actions taken, x = leftover money. Now consider two competing values for the state (i, j, pmax) => (a, 0), (a + 1, pmax — 1). Note that the x is constrained because you only take what you need so leftover will always be x mod(pmax). If there is ever going to be a case where you'd take a value with more action because it has more leftover money this would certainly be it.

    • Consider if you transition to another adjacent cell (right or down) from both these values. Let's say the cost to transition is c.

    1) (a, 0) so to transition from the value (a, 0), you will need to spend some action on accumulating c money. so you will do it need this many more actions m = ceil(c / pmax), and you will be left with some left over money from the remainder of c divided by pmax. so (a, 0) -> (a + m + 1, c mod(pmax))

    example, cost = 10, pmax = 4, m = 3, and (a, 0) -> (a + 3 + 1, 2)

    2) (a + 1, pmax — 1) You will not have to spend as much action right, cause you had more leftover money. But you will still need m = ceil((c — pmax — 1) / pmax), and that will just reduce your number of actions to achieve the cost c by 1. So it just balances out, you spend 1 less action cancels the fact it was 1 more action. The extra money did not really help.

    example, cost = 10, pmax = 4, m = 2, because 10 — 3 = 7, and (a + 1, 3) -> (a + 1 + 2 + 1, 1). This is worse then the value above (a + 4, 2) vs (a + 4, 1).

    In general, I get a sense that you can only reduce number of actions by 1 by having more left over money. So it will at best be a tie in number of actions, but it will never lead to an output with fewer actions. And that is the reason you can take values with fewer actions regardless of money situation.

    I assumed pmax was constant in these transitions. But I believe if you plug in an increasing pmax in the same proof it follows for that as well. This only works because pmax is weakly increasing I realized when I went over this.

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

    Your question is given a dp state = $$$(i, j, pmax)$$$, how to compare two values of (min moves, money earned), say $$$(a, b)$$$ and $$$(a', b')$$$. We can indeed compare them lexicographically, i.e. if $$$a < a'$$$ or $$$(a = a'$$$ and $$$b > b')$$$ set dp(state) = $$$(a, b)$$$.

    So why is this optimal? Because when money is needed to make the transition it is as if we can use $$$pmax$$$ not just $$$p[i][j]$$$. This implies that there is no need to bring large money surpluses, you can always use $$$pmax$$$ if needed at a later move, so trade 1 action for $$$pmax$$$ money just enough to make the transition.

    Because, you use just enough actions to make the transition, the current money in the dp value is such that $$$b < pmax$$$ (if it were not the case we could do $$$a -= 1$$$, $$$b -= pmax$$$). So this actually implies that you can lexicographically compare the dp values has I stated before.

    Your scenario of having $$$a < a'$$$ and $$$b «< b'$$$ does not occur, as from state $$$(a, b)$$$ you can reach $$$(a + 1, b + pmax)$$$ and as argued $$$b + pmax > b'$$$.

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

      Thank you so much for your kind reply! I think your arguments really make sense, and I will try to think over this again.

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

    Hello I have put some thoughts after reading the replies in this comment section (Very helpful replies and I am very grateful to them)

    This is my proof.

    Claim:- The leftover money at any point in time is < pmax. This is the max over all P(i, j) from 1,1 till i, j

    Proof:- Let's assume leftover money is x and assume that we are updating dp for i + 1, j. The same can be done i, j + 1.

    So, if we directly took modulo with Pmax we would have (R(i, j) $$$-$$$ x) mod Pmax as the remainder. let's name it REM. Now we would have Pmax $$$-$$$ REM leftover money, always < Pmax.
    or
    x >= R(i, j) then trivially x $$$-$$$ R(i, j) < Pmax.

    The claim is proven. Suppose we had two (action, money leftover) pairs for i + 1, j. Let's take the worst possible case.

    A, 0
    A + 1, Pmax $$$-$$$ 1.

    We can see that with just 1 operation we can go from A, 0 to A + 1, Pmax. So taking the lower action count pair is always optimal.

    I have only made a more verbose comment over existing replies. It's just a more wordy version of the proof people already mentioned. Thanks once again!

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

      Nice proof! Also thank you so much for your great work. I think now I can convince myself that the solution in the editorial is completely correct. Thank you for your kind help!

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

Can we solve G with half-plane mo's algorithm? It's not well known I think and I'm unsure how to implement it. I suppose its time complexity is $$$\mathcal O(n\sqrt m)$$$.

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

Can you anyone help me understand why my dp code for D is returning -2147483648 for every input

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

    dp[k — 1] + 1 is overflowing intilize dp with some max value say 1e5 and the change last condition (== INT_MAX) to (> n) after doing this 53 / 58 test pass link

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

      Thanks for the reply, that was a silly error on my part. Is it solvable using 1D DP ?

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

        yes

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

          The idea seems almost similar to mine. Can you tell what does ndp do in your code ? Removing ndp passes all the testcases except one.Submission

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

            if you don't use ndp you have to make dp array of size n * T_size. if you just use 1D dp then the condition of taking atmost 1 string from the bag is not fulfilled

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

Given a collection of strings and a string t. Find the minimum string taken from the collection and join it to form the t.

Note: Collection has a finite number of strings i.e. each string can be taken once.

HOW TO SOLVE THIS EFFICIENTLY.

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

    Use dp, want the min yen to reach the state (bag index, index of matched prefix of T)

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

Problem D WA on a single test. Could anybody please help me find where the solution fails.

Thanks.

https://atcoder.jp/contests/abc344/submissions/51103999

UPDATE: Got it!

https://atcoder.jp/contests/abc344/submissions/51107878

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

Damn,everytime i participate in ABC i get to learn some new stuff and for this one i got to know about linked list using maps.Really interesting