__baozii__'s blog

By __baozii__, 3 months ago, In English

We hope you enjoyed the problems!

Rate the contest!

2188A - Divisible Permutation

Solution
Code (Python)
Rate the problem!

2188B - Seats

Solution
Code (C++)
Rate the problem!

2187A - Restricted Sorting

Hint 1
Hint 2
Solution
Code (Python)
Rate the problem!

2187B - Shortest Statement Ever

Hint 1
Solution
Proof of the interesting fact
Code (C++)
Rate the problem!

2187C - Jerry and Tom

Hint 1
Hint 2
Solution
Code (C++)
Bonus
Rate the problem!

2187D - Cool Problem

Solution
Code (C++)
Rate the problem!

2187E - Doors and Keys

Hint 1
Hint 2
Hint 3
Solution
Code (Python)
Bonus
Rate the problem!

2187F1 - Al Fine (Maximizing Version)

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
Code (C++)
Fun Fact
Rate the problem!

2187F2 - Al Fine (Counting Version)

Solution
Code (C++)
Bonus
Rate the problem!

2187G - Many Cartesian Trees

Solution
Code (C++)
Rate the problem!
  • Vote: I like it
  • +89
  • Vote: I do not like it

»
3 months ago, hide # |
 
Vote: I like it -6 Vote: I do not like it

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

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

I liked C a lot! Didn't think much about D during contest, but it looks fun and challenging! Anyway, here's my take on problem C: link.

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

Greedy Construction solution for D2D/D1B : Solution Link
Thanks for the contest :)

»
3 months ago, hide # |
 
Vote: I like it -6 Vote: I do not like it

Thanks for the contest and quick editorial!

»
3 months ago, hide # |
Rev. 7  
Vote: I like it +28 Vote: I do not like it

Here's a braindead solution to Div1B where the correctness is completely obvious. It actually looks quite similar to the editorial solution and might end up producing the same output, but with a small sacrifice in runtime it becomes easier to prove.

Iterate over the $$$31^2$$$ possible combinations of the most significant bit you change in $$$x$$$ and $$$y$$$ (the extra 1 is from the possibility of not changing the value). If this changes a $$$0$$$ to a $$$1$$$ then the value increases, so we want to set all future bits to $$$0$$$ to increase it by as little as possible. Otherwise, if this changes a $$$1$$$ to a $$$0$$$ then the value decreases, so we want to set all future bits to $$$1$$$. However, if we are decreasing both values, this is invalid, so we will set the one with the earlier msb to all $$$1$$$s and the one with the later msb to all $$$0$$$s, thereby losing as little value as possible. Then, check if the resulting $$$p$$$ and $$$q$$$ satisfy the given condition and find the minimum over all satisfactory choices of msbs.

Submission: 360566950

Unfortunately I spent an hour doing some horrific casework before thinking of this, then I got nowhere on C, so I guess I'm back to CM for now, but I enjoyed the round nevertheless.

Edit: ok now that I think about it, the proof is not as trivial as I thought it was, since there are some tricky cases where the bits in between the two msbs are 1. There's an easy way to take care of this, though. If you fix the most significant bits you change, now you can greedily assign everything lower.

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

    Can you elaborate a bit on your edit? I'm not sure what exactly changed since you're fixing the MSB and assigning lower bits in both the cases.

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

      Something like, suppose i = position of msb you change in x, j = position of msb you change in y. WLOG, let i > j, that is, i is more significant than j. Maybe x = 6 and y = 3, for example. Furthermore, suppose that x_i = 1 and y_i = 0.

      Then assigning

      p = ...0111111

      q = ...xxx1000

      where x can be anything, won't necessarily be optimal because in particular, it's not always legal since the x's might be 1s. But you can just assign greedily where on each bit you ensure it's legal, and since you already fixed the msbs, it's easy to see how you want to greedily assign each value.

      Anyways, even the original solution still ACs, it's just nontrivial to prove now; you have to show that all choices of i, j that cause this issue are suboptimal, which is probably true by some exchange argument. The greedy is fairly easy to prove, I think.

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

        Can you prove the editorial for this problem? The claims they have made? If you can just explain the editorial to a newbie like me in a layman terms, I would appreciate it.

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

    i basically did this i think, sooo painful

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

On problem D (div 2) "Shortest Statement Ever" the editorial said:

There is another interesting fact: there always exists a solution where p=x or q=y

How to prove that?

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

    It is a direct corollary of the intended solution. However some contestants and testers seemed to have guessed it.

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

      Hi, thanks for the contest! Can you provide a proof for the intended solution? I can't figure out the details myself despite guessing it in the contest.

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

      I don't understand the proof of Lemma 1. If $$$x = 011$$$ and $$$p = 100$$$, isn't it worse to subtract $$$W$$$ from $$$p$$$, which would result in $$$p' = 000$$$? $$$|x - p|$$$ is $$$1$$$ while $$$|x - p'|$$$ is $$$3$$$, right?

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

    My Code I did my solution based on this observation which is O(1). Basically if q=y then you can ALWAYS make the lowerbits (starting from the highest same bit) of x to the complement of lower bits of y, by subtracting.

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

In div1D, I liked to think of it as, if $$$y = 0$$$ and $$$x = 1$$$, "our current sum is $$$0+1+\ldots+k$$$, and by default we are adding the next number/removing the last number". If we read 0 from the string, we do what we are going to do by default, 1 changes this default option and applies the new default immediately. After that it's still the same knapsack etc, but it required me to handle the case where we want to remove the last number from "our current sum is $$$0$$$" separately, so it's not the cleanest way to describe the solution, but it's what I had in mind when solving this.

In div1F1, I copy-pasted the PQ tree from yosupo judge, and then wrote some DP on it, but unlike normal tree DPs, this one required handling the siblings as well.

All in all, thanks for the contest! D was super cool

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

O(n) time complexity and O(1) space complexity solution for 2A: 360643885 — can likely be further optimized!

»
3 months ago, hide # |
Rev. 2  
Vote: I like it +14 Vote: I do not like it

$$$O(1)$$$ time complexity for D2D/D1B: 360647850.

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

.

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

1E can be optimized to $$$O(n\log n)$$$ by FHQ Treap. Code: 360602874.

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

D is the worst problem I have ever seen.

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

The most difficult task was D, div.2

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

Div1 C cool problem

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

I feel like I could have easily solved div.2B and Div.2C but making an "M" on my graph was too tempting for the inner me

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

I think in editorial of Div2 D. the variables x y p q are different in the statement and editorial.

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

My (maybe overcomplicated) DP solution to div1 B / div2 D.

Let $$$x_i$$$ and $$$y_i$$$ be the $$$i$$$-th lowest bit of $$$x$$$ and $$$y$$$ $$$(0 \le x_i, y_i \le 1)$$$.

Let the DP state be $$$D[i, j, k] \; (0 \le i \le 31, -1 \le j, k \le 1)$$$. $$$i$$$ is the bit index (counting from the lowest). $$$j = 1$$$ means the value of $$$p$$$ has a carry in the digit $$$i$$$; $$$j = -1$$$ means $$$p$$$ has a borrow in the digit $$$i$$$; $$$j = 0$$$ means the otherwise. The same applies to $$$k$$$, for the value of $$$q$$$. $$$D[i, j, k]$$$ is the minimum deviation from $$$x$$$ and $$$y$$$ (i.e. the value of $$$\left| x-p \right| + \left| y-q \right|$$$) that's required to arrive at the state.

Initially, $$$D[0, 0, 0] = 0$$$ and $$$D[i, j, k] = \infty$$$ for the rest.

Let's think about the state transition from $$$D[i, j, k] \; (i \le 30)$$$. The current value of $$$p$$$ and $$$q$$$ at the bit $$$i$$$, including a carry or a borrow, is: $$$a = x_i + j$$$ and $$$b = y_i + k$$$ $$$(-1 \le a, b \le 2)$$$.

Let's bruteforce and consider all the combinations of changes to $$$p$$$ and $$$q$$$ at the bit $$$i$$$: you might want to add $$$1$$$ to or subtract $$$1$$$ from the $$$i$$$-th bit of $$$p$$$ and $$$q$$$. For all $$$-1 \le c \le 1$$$ and $$$-1 \le d \le 1$$$, the new $$$i$$$-th bit of $$$p$$$ and $$$q$$$ are: $$$a^\prime = a + c, \, b^\prime = b + d \; (-2 \le a^\prime, b^\prime \le 3)$$$. If $$$a^\prime \mathbin{\&} 1 = 1$$$ and $$$b^\prime \mathbin{\&} 1 = 1$$$, then these don't satisfy the problem condition, so skip. Otherwise, this change pattern can be viable. Note that the carry or the borrow of $$$a^\prime$$$ and $$$b^\prime$$$ affects the next bit only. The effect of the next bit can be denoted as: $$$\displaystyle j^\prime = \left\lfloor a^\prime / 2 \right\rfloor, \, k^\prime = \left\lfloor b^\prime / 2 \right\rfloor$$$. The cost of this change (the deviation from $$$x$$$ and $$$y$$$) can be calculated as: $$$\displaystyle s = \left( \left| c \right| + \left| d \right| \right) \cdot 2^i$$$. With that, update the DP state as follows:

$$$\begin{align*} D[i + 1, j^\prime, k^\prime] \leftarrow \min \left( D[i + 1, j^\prime, k^\prime], D[i, j, k] + s \right). \end{align*}$$$

After all the transitions, the minimum deviation from $$$x$$$ and $$$y$$$ can be obtained as $$$D[31, 0, 0]$$$. You can recover the value of $$$p$$$ and $$$q$$$ by backtracking the DP table, if you leave enough information as you make the transition.

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

I did the solution for Div2 D using the greedy where we fix one of x=p or y=q, and then change the bits of the other one greedily to get to the correct answer. Here is my submission: 360578329, this is getting WA at pretest 2, I have tried almost every testcase I can think of.

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

    I did the same. Getting wa on test 2. Want to find out how it is failing

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

      Yeah, I found a small error in my if else loop, but I even corrected it, still it is failing on some 1020 test, either it is some edge case that we are missing.

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

I made a mistake in problem B and used ceil(n/3) instead of floor(n/3)(( Good tasks! Thanks to the authors

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

C,D are good problems, and I like them much. However, I think it's not suitable to have these four problems as ABCD in div2, since they require no algorithm(need the thought of greedy, maybe)

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

For div2D, abs(x-p) could be x-p, p-x, 0 depending on x > p, x < p, x = p maintain a DP with three states, bit position, state for p {whether, > < or =}, state for q there are three possibilities for bit assignment for p&q=0 at position j get the new state as bit is assigned and maintain current minimum as current state establishes the inequality to calculate abs(x-p) + abs(x-q), store the minimum reached for every such state and trace back to get values of p and q

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

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

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

thanks for the problems was tired of mathforces!

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

Can someone please give a simple solution to Div2 D? Which makes sense like the editorial or comments here are just observations and not the actual proof. What does it take for the editor to also provide proof rather than saying left for readers? Bro, we can also observe it from others solutions and I also observed it on my own but couldn't prove or satisfy myself, so please someone either prove the editorial solution or give a valid simple proof to your solution rather than saying it can be proved or it can be observed. I'm a beginner but would appreciate a humble guy trying to help others than people showing off it can be proved, observed, that doesn't help at all. Please correct the editorial and add the proof. Humble request.

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

    ok so as a pupil myself what i understood first simply by observation and casework that if i choose say my p as x then i can find a q which is the optimal solution this was done purely based on casework. Now once i fix say p=x now i want a q which is as close as possible to y so to minimize the sum so to do that i can only use those bits which are unset in x since we want p&q==0 so i crate a mask where those bits are set which are unset in x now we build our q first as close to y as possible to do this we would build two numbers l and h where l is the largest number closest to y and h be the smallest number greater than y how you build you can see my code it would be much more clear to you

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

      but how can you assume only if you fix p = x (or vice versa) then it's optimal, because this is observation, but you can't prove what if p is also as close as possible to x and y is also as close as possible to y but none are exactly equal. Like we need a way to prove that it's always optimal if either p = x or q = y and any deviation will only increase the answer.

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

        I solved it with DP as abs(x-p) could be x-p, p-x, 0 depending on x > p, x < p, x = p maintain a DP with three states, bit position, state for p {whether, > < or =}, state for q there are three possibilities for bit assignment for p&q=0 at position j get the new state as bit is assigned and maintain current minimum as current state establishes the inequality to calculate abs(x-p) + abs(x-q), store the minimum reached for every such state and trace back to get values of p and q. Why this works is because if you are already larger at a position j, you will always be larger later and if you are already smaller at a position j, you will be always smaller later. That solves it by saving to check all the permutations in 32*3*3*4.

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

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

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

div 2 c was really a good problem. using max and min of element to sort the whole array is a main observation .

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

Can anyone explain me why this code gives wrong answer on some test cases.

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <cmath>
#include <queue>
#include <map>
using namespace std;

int main() {
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
        }
        vector<int> cpy = a;
        sort(cpy.begin(), cpy.end());
        if (cpy == a) {
            cout << -1 << '\n';
            continue;
        }
        map<int, queue<int>> mp1, mp2;
        for (int i = 0; i < n; ++i) {
            mp1[a[i]].push(i);
            mp2[cpy[i]].push(i);
        }
        int ans = 1e9;
        int mx = cpy[n - 1], mn = cpy[0];
        for (int i = 0; i < n; ++i) {
            int f = mp1[a[i]].front();
            int s = mp2[a[i]].front();
            if (f == s) {
                continue;
            } else {
                int idx = mp2[a[i]].front();
                int _max = max(abs(mn - a[idx]), abs(mx - a[idx]));
                ans = min(ans, _max);
            }
            mp1[a[i]].pop();
            mp2[a[i]].pop();
        }
        cout << ans << '\n';
    }
}
»
3 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I sneaked problem D by running a brute force and found that the coefficient only depend on count of prefix xor = 1. Still don't understand why.

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

Any intuition behind div1.D? After reading the editorial it just blows my mind how one can come up with this in contest.

  • »
    »
    3 months ago, hide # ^ |
     
    Vote: I like it +23 Vote: I do not like it

    If $$$x$$$ must be $$$a$$$ or $$$b$$$, then $$$(x - a) \cdot (x - b) = 0$$$ must hold.

    There is no need to rewrite the formula in this magical way. Instead of handling two separate "if" cases, we can pack both possibilities into a single, initially messy equation:

    $$$ (c_i - c_{i - 1} - x) \cdot (c_i + c_{i - 1} - y) = 0 $$$

    Expanding this gives us a much more useful form:

    $$$ (c_i - c_{i - 1})(c_i + c_{i - 1}) - x(c_i + c_{i - 1}) - y(c_i - c_{i - 1}) + xy = 0 $$$
    $$$ c_i^2 - c_{i - 1}^2 - c_i(x + y) - c_{i - 1}(x - y) + xy = 0 $$$

    Now, the magic happens when you sum this up for all $$$i$$$. The $$$c^2$$$ telescoping terms in the middle cancel out, allowing you to isolate $$$\sum c_i$$$.

    I hope this helps!

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

      Yeah, thank you very much. I guess intuition was to try to join the 2 equations into one, and see what we can get from there. Very interesting problem.

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

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

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

you guys should really stop leaving proof to readers for exercise for those problems need more guessing than reasoning, it's not interesting at all.

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

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

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

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

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

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

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

One of the solution for d2D/d1B mentions that p=x OR q = y might be possible I am able to understand it intutively but am unable to figure out the reason why it should be true.

Could someone help out with the proof for the same?

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

thanks a lot for the great problem div2E and the well explained editorial. I think that there is a little typo in the editorial, it should be a "+" instead of a "-" for the last part of the sum (when d(x) == d(y))

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

Everytime when I see "Proofs are left as an exercise for the reader" in the editorial:

Sorry, but I've spent enough time during the contest trying to solve the problem and came to read a full solution after giving up, not to have more questions than answers why certain properties hold :) Apart from that, I think there were interesting problems!

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

I’m trying to understand why the answer is 2 for the string "000000" in the problem 2188B – Seats. Could someone explain how that result is obtained?

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

ignore

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

My approach for div2 problem D is to fix p = x and find the optimal q, and also fix q = y and find the optimal p. However, my solution keeps giving wrong answers, and I don’t know what’s wrong. Here is my code[submission:360724737]—I'd really appreciate it if someone could help me figure it out.

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

__baozii__ What is the name of the trick? They way I have solved this question is through dealing vertex all togather based on depths for each vertex(iterating over every subtree except the one with max depth). You may see the code 360582035

This is $$$O(n)$$$ if I'm not mistaken.

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

This is the intended solution. Proofs of the claims are left as an exercise for the reader.

Can we please forbid authors from writing such statements in the editorial? this is an editorial, not a cpp-to-english translation tool. i am tired of seeing such problem solutions. this is so harmful to the div2 community…

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

if you guys also solve question on leetcode then question B was quite similar with leetcode question "Can Place Flower (605)". A and C was straight forward question just find one simple logic and that's it. D was quite hard for me. i was only able to solve first 3 questions.

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

In Div2 F, "optimized to O($$$n^2$$$ / w) using bitset". n <= $$$10^5$$$, how can we optimise the solution. $$$n^2$$$ solution will not pass.

»
3 months ago, hide # |
Rev. 2  
Vote: I like it +14 Vote: I do not like it

Div 1 C Jerry and Tom Bonus Version:

  • We can precompute distances of each node from root.
  • For every node, we need to compute how many pairs $$$(u,v)$$$ in its subtree, with same depth $$$(depth[u] = depth[v])$$$. which is doable using small to large technique in $$$O(nlog^2(n))$$$. rest all parts of problem can be done in $$$O(N)$$$
  • To optimize this, lets consider a auxiliary tree for all nodes of depth = d. Its a known fact that if # nodes = k, worst case number of nodes for auxiliary tree is $$$2k - 1$$$.
  • So if auxiliary tree is constructed, then we shall process the tree, and for each node $$$u$$$, maintain:

    • Number of nodes of current depth d, in its subtree (good nodes)
    • Number of pairs of nodes (good nodes) in its subtree. (this is important)
  • If observed, the nodes of auxiliary tree are just a compressed versions of nodes of actual tree. To propagate the answers from auxiliary tree to main tree, we shall use difference arrays and prefix sum ideas to add values in range $$$[Aux[U] ... Aux[par[U]])$$$

Construct Auxiliary Tree:

  • Remaining part is to construct Auxiliary tree from depth d to depth d+1, which is also doable in total of O(N).
    • Iterate on nodes of depth d.
    • If they are leaf nodes, remove them and keep removing parents (if orphaned — no children)
    • Iterate on nodes of depth d again, (now to compress paths). If parent has only 1 child, remove parent, attach to grandparent (recursive).
    • Now add nodes of d+1 depth
    • Iterate on nodes of depth d+1, (now to compress paths). If parent has only 1 child, remove parent, attach to grandparent (recursive).

Sorry for overkilling it, but good exercise.

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

Does anyone have any solution different from the editorial for Div 2F/Div 1D ?

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

If anyone wants a dp solution for div 2 D :Problem Link

Here is my code:{If explanation needed , ping me} .

int dp[35][4][4]; 

pair<int,int>  best[35][4][4]; 
vector<pair<int,int>> choice={{0,1},{1,0},{0,0}}; 
int vis[35][4][4];
int timer=0; 
 int x,y;
/*0->equal , 1->less,2->greater*/
int rec(int level,int ps,int qs){
	if(level<0) return 0; 
	if(vis[level][ps][qs]==timer) return dp[level][ps][qs];
	
	int ans=1e16;
		int x_bit=((x&(1LL<<level))>0?1:0); 
		int y_bit=((y&(1LL<<level))>0?1:0);
		int val=(1LL<<level);  
	for(auto &[p_bit,q_bit]:choice){
		int new_ps,new_qs; 
		int cost=0; 
		int p_cost=(x_bit-p_bit)*val;
		int q_cost=(y_bit-q_bit)*val; 
		
		if(ps==0) cost+=abs(p_cost);
		else if(ps==1) cost+=p_cost; 
		else cost-=p_cost; 
		
		if(qs==0) cost+=abs(q_cost);
		else if(qs==1) cost+=q_cost; 
		else cost-=q_cost;
		
		new_ps=(!ps?((p_cost>0)?1:2):ps);
		if(x_bit==p_bit && !ps) new_ps=0;  
		new_qs=(!qs?((q_cost>0)?1:2):qs); 
		if(y_bit==q_bit && !qs) new_qs=0; 
		int temp=rec(level-1,new_ps,new_qs)+cost; 
		if(ans>temp){
			ans=temp; 
			best[level][ps][qs]=make_pair(p_bit,q_bit); 
		} 
		 
	}
	vis[level][ps][qs]=timer; 
	return dp[level][ps][qs]=ans; 
	
}
int p_ans,q_ans; 
void genrate(int level,int ps,int qs,int &p,int &q){
	if(level<0) return; 
	auto &[p_bit,q_bit]=best[level][ps][qs]; 
	p|=(1LL<<level)*p_bit; 
	q|=(1LL<<level)*q_bit; 
	
	int n_ps=ps,n_qs=qs; 
	
	int x_bit=((x&(1LL<<level))>0?1:0); 
	int y_bit=((y&(1LL<<level))>0?1:0);
	
	if(ps==0){
		n_ps=(p_bit>x_bit?2:(x_bit!=p_bit?1:0)); 
		
	}
	if(!qs){
		n_qs=(q_bit>y_bit?2:(y_bit!=q_bit?1:0)); 
	}
	genrate(level-1,n_ps,n_qs,p,q); 
}
void solve(){
 cin>>x>>y; 
 	timer++;  
 	 p_ans=0,q_ans=0; 
 	int ans=rec(32,0,0);
	genrate(32,0,0,p_ans,q_ans); 
	
 	cout<<p_ans<<" "<<q_ans<<'\n'; 
 
    
}
 
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1; 
    cin >> t;
    while(t--){
     solve();
    }
    
    return 0;
}

»
3 months ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

For 2187C/2188E, won't it sometimes be optimal to pick a node that's not the farthest?

Consider the following graph:

3->8 6->9

(Note that 1->2, 2->3, 3->4, ..., 9-10 also exist).

Say Jerry is at 3 while Tom is at 6 and it's Tom's turn. Tom would actually benefit to go from 6 to 7 rather than 6 to 9, since it prevents Jerry from going to 8 (Tom would go there next move) and forces him to, for the rest of the game, move one step at a time. If Tom moved to 9, Jerry could have moved one step at a time and the game would have lasted longer. If Tom stayed, then Jerry could go to 8 and go on to actually win the game.

Let's continue the same graph and look at it from Jerry's perspective. This time, he is at 3 and Tom is at 9. If Jerry moves to 8, Tom could stay and then Jerry would lose next turn. But he can also move to 4, and Tom would stay stationary for 5 more moves for Jerry to catch up. Thus, it would be best for him to go slow.

This example serves as a counter-example right?

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

    Note that there do not exist two directed edges (ui→vi) and (uj→vj) such that ui<uj<vi<vj.

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

Is there a reason why Div 1 B doesn't break after finding the first highest conflict?

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

    The weightage of subtraction here is dependent too much on the highest first conflict. 2^n will always be more than 2^0 + 2^1 + ... 2^(n-1). So assigning this bit to either one of the number will change the answer from there or take one of the numbers a little bigger.

    I solved it by guessing from all 4 options that can happen on the first MSB match.

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

      I understand the 4 options but why does the loop continue after the msb being found for conflict.

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

        It does not. Just terminate the loop on the first msb match. You don't need to go on.

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

          thanks was confused why we would continue on the editorial

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

in restricted string, how If ai−mn≥k and mx−aj≥k, how max-aj will be >=k if its not the toher case aj-min <k

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

360866947

This is my submission for problem B which one can refer as it is kind of mix of dp and greedy as I try all possible ways when I am not sure of which bits need to be placed for some bit i.

If you have any doubts, Feel free to ask. I will try to clarify.

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

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

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

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

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

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

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

Div2E, Got so close but yet so far. Very nice problem :D

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

FireFly __baozii__

Please correct me if I am wrong for problem div1C.

In editorial, for the third summation, there should not be x<y condition when d(x)=d(y) as tom would win for both cases when x<y || x>y. maybe a multiplication factor of 2 should be there outside the third term.

Edit:Nvm, got it.First two term includes that contribution already which is also clear by the way you are calculating the first two terms.Really good Problemset.

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

Did anyone solve problem D using the following observation?

Equivalently, there exists an optimal solution (p, q) such that p = x or q = y.

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

A slightly different intuition for solving the d1D/d2F problem. If we consider the fixed string $$$r$$$, we can see that $$$c[i]$$$ contributes to the total sum $$$y$$$ with a coefficient equal to $$$0$$$ or $$$1$$$, with a coefficient of $$$0$$$ if the number of ones in the prefix $$$s$$$ is even and $$$1$$$ if it is odd (i.e., $$$xor$$$ of prefix $$$i$$$). If, after this observation, we consider by analogy what happens to the $$$x$$$'s on prefixes with different $$$xor$$$, we can see that as long as the number of ones on prefixes $$$i$$$ and $$$i + 1$$$ is the same, $$$c[i+1]$$$ contributes one $$$x$$$ more to the sum than $$$c[i]$$$. after we encounter another unit, this type of operation will reverse all our $$$x$$$'s and we will lose the last member of the arithmetic progression. For example, let it be equal to $$$k*x$$$, then as long as the prefixes' $$$xor$$$ values are equal, we will continue to lose $$$(k-1)*x$$$, $$$(k-2)*x$$$, and so on for the subsequent terms of the arithmetic progression, until we encounter a unit again, at which point we will continue the arithmetic progression, but only from the last term that was not subtracted. This means that the total sum of all $$$c[i]$$$ is $$$y$$$ multiplied by the number of prefixes with $$$xor = 1$$$ plus an arithmetic progression with a step of $$$x$$$ and a length equal to the difference between the number of prefixes with $$$xor = 0$$$ and $$$xor = 1$$$.

I apologize, this was translated using a translation tool.

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

Could anyone please explain where I am going wrong with this solution for Div1 D.

My code keeps failing

Between two question marks, the value of c at the position is a linear function of the value at the question mark. Therefore when summing over all the sequences we can substitute the average value of c at the question mark in the linear function.

But the average value at the question mark is (x+y)/2 because it can be either 0 or 1 for every possible preceding sequence. So we calculate the sequence as normal, replacing c with (x+y)/2 at every question mark, summing the obtained values of c, then multiplying by 2^(no. of question marks)

For (x+y)/2 you can use the modular inverse of 2

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

    You are treating this question as if it asks to compute $$$\sum_{\text{completions}} f(s)$$$ when $$$\sum_{k \in { \text{values that } f(s) \text{ can take} }} k$$$ is being asked. They differ when two completions yield the same value of $$$f$$$.

    Counter-example for your approach: $$$n$$$ = 2, $$$x$$$ = 1, $$$y$$$ = 2, $$$s$$$ = ??

    List Completions are as follows:

    00: $$$c_1$$$ = 1, $$$c_2$$$ = 2, $$$\Rightarrow$$$ $$$f$$$ = 1+2 = 3.

    01: $$$c_1$$$ = 1, $$$c_2$$$ = 1, $$$\Rightarrow$$$ $$$f$$$ = 2.

    10: $$$c_1$$$ = 2, $$$c_2$$$ = 3, $$$\Rightarrow$$$ $$$f$$$ = 5.

    11: $$$c_1$$$ = 2, $$$c_2$$$ = 0, $$$\Rightarrow$$$ $$$f$$$ = 2.

    Cool Integer list is then {2,3,5} and the required answer becomes 10. However, multiplying by $$$2^{\text{count of ?}}$$$, you implicitly count each completion, which forces you to count $$$f$$$ = 2 twice, creating a mismatch.

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

For 2A, an alternate solution can be like this: 1. The first element is always ceil(n/2). 2. For every next element, you can add/subtract index i of the prev_element to the prev element alternatively to get the whole permutation.

import math
test_cases = int(input())

for i in range(0, test_cases):
    n = int(input())
    prev_element = math.ceil(n/2)
    print(prev_element, end = ' ')
    for i in range(1, n):
        if i%2 == 1:
            prev_element += i
        else:
            prev_element -=i
        print(prev_element, end = ' ')
    print('\n')
»
2 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

can somebody Prove to me why this:

auto dfs = [&](auto &&self, int u, int p) -> map<int, int> {
        pa[u] = p;
        map<int, int> mp;
        mp[dep[u]] = 1;
        for (auto v : g[u]) if (v != p) {
            dep[v] = dep[u] + 1;
            auto nmp = self(self, v, u);
            if (mp.size() < nmp.size()) swap(mp, nmp);
            for (auto &[d, f] : nmp) {
                ans += 1LL * f * mp[d] * (d &mdash; dep[u]);
                mp[d] += f;
            }
            sz[u] += sz[v];
        }
        return mp;
};

Its complexity is O($$$n \cdot log^2 n)$$$).

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

In Div1C how do we prove that the game end in finite steps

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

https://mirror.codeforces.com/profile/rom228.js look at this person's decisions

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

For Div 1 C, for the last summation shouldn't you add this term

$$$ \sum\limits_{1 \le x \lt y \le n, d(x)=d(y)} (d(y) - d(\mathrm{lca}(x,y))) $$$

instead of subtracting it?

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

Another solution for A-Divisible Permutation (C++)

One array only!.

Construct the answer in reverse order.

Take: q = [n, 1, n-1, 2, n-2, 3, ...] Then the adjacent differences in q are: n-1, n-2, n-3, ..., 1

After reversing q, the difference at position i becomes exactly i, so |p[i] — p[i+1]| = i, which is divisible by i.

#include <bits/stdc++.h>

using namespace std;

void solve(){
    int n;
    cin >> n;
    int pos = n / 2;
    if(n%2 == 1)pos ++;
    vector<int> np;
    for (size_t i = 0; i <= pos; i++)
    {
        np.push_back(n - i);
        np.push_back(i + 1);
    }
    for(auto i = n - 1;i >=0 ;i--)cout << np[i] << " ";
    cout << endl;
}
int main(){
    int t;
    cin >> t;
    while(t--)solve();
    return 0;
}
»
4 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

What is the intended solution for the bonus of Doors and Keys?