szilb's blog

By szilb, 10 months ago, In English

2118A - Equal Subsequences

Hint 1
Hint 2
Tutorial
Implementation

2118B - Make It Permutation

Hint 1
Hint 2
Hint 3
Tutorial
Implementation

2118C - Make It Beautiful

Hint 0
Hint 1
Answer to hint 1
Tutorial
Implementation

2118D2 - Red Light, Green Light (Hard version)

General outline (hint for both subtasks, read first)
Subproblems for D1
Subproblems for D2
Implementation

2118E - Grid Coloring

Hint 1
Hint 2
Hint 3
Tutorial
Implementation
Extra

2118F - Shifts and Swaps

Hint 1
Hint 2
Hint 3
Hint 4
Tutorial
Implementation
  • Vote: I like it
  • +91
  • Vote: I do not like it

»
10 months ago, hide # |
 
Vote: I like it -147 Vote: I do not like it

first.

»
10 months ago, hide # |
Rev. 2  
Vote: I like it -82 Vote: I do not like it

4 th approach

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

In reference to extra for E bullet 1:

My construction for problem E was sort all the coordinates first by checkerboard distance from the center with ties broken by Manhattan distance from the center. Then all further ties broken by sorting the tied points in clockwise order alternating starting from 12 and 6 for each of these ties broken sequentially.

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

Almost implemented D1 during contest, but debugged bugs for too long(

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

    How to do it I tried via recursion got stuck hard af then tried apply iterative approach still got fked af

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

      Here is my submission: https://mirror.codeforces.com/contest/2118/submission/324142498 You just need to maintain reverse flag, and go forward or backward to next light (depends on reverse flag), maintain time variable to decide whether you need to reverse or not on current light and also visited array, so when you visit same light twice with same reversed flag, you are in loop and the answer is no. And when you are on lights array index which is out of range, then the answer is yes.

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

Fastest editorial I have seen (only slower than um_nik's one)

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

woooow! fastest editorial

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

Amazing contest! B was awesome! This was my first time participating in a CF round and I really enjoyed it!

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

B was an interesting problem. It's one of those problems where you just suddenly get a solution in your mind.

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

In B, I first fixed the first column to be 1 2 3 ... n by reversing [1, i] on row i. Now, the second column was close to a permutation (like 2 1 2 3 ... n — 1), so I reversed [1+1, n] on row 1, this lead me to reverse [i+1, n] on row i. This way, each column gradually becomes a permutation. Total 2n — 1 operations.

My solution : 324069981

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

    I did some calculation and used 2n-3 operations. I started from the bottom row and the gradually moved up to the top. [n, 1, n] --> [2, 1, 2] This will give us n-1 operations. After this I moved from top to bottom. [1, 2, 3] --> [n-2, n-1, n] This will be n-2 operations. Adding up we get a total of 2n-3 operations.

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

      Got it, i just did two more operations which were choosing just one element and reversing, I guess you skipped this cuz it makes no difference.

      I went from [n, 1, n] --> [1, 1, 1] it will give n operations similarly one more form top to bottom. But getting this logic is totally random.

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

UPD in 2025/6/15: fix bugs and reformat.

Below is one solution to problem E which satisfys the tighter restriction: no more than two operations per cell.

Assume that $$$n \geq m$$$. Also, reverse the whole operation and consider deleting one cell at a time and reducing the furthest cells by one. There are mainly three steps to go:

Step 1: n > m, each corner has a number not less than 1 and all other cells have a number equal to 2.

After using several times of Step 1, the initial grid can be shrinked to a square. The strategy is trivial when $$$n = m = 1$$$, but some discussion should be done when $$$n = m \geq 3$$$.

Step 2: n = m > 3, each corner has a number not less than 1 and all other cells have a number equal to 2.

This time, we can alternate between Step 2 and Step 1 to reduce the side to 3. As this method failed when $$$n = m = 3$$$, we need the third step:

Step 3: n = m = 3.

While coding, you need to be careful of the position of the cells with the number $$$1$$$ and properly rotate or symmetry the strategy. A possible implementation: 324437128

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

    Hi, I am a bit confused about the case where $$$n=3$$$ and $$$m=3$$$ (the sample input). Are you doing the removals like this:

    01 05 02
    07 09 08
    03 06 04
    

    So your adding order is like:

    09 05 08
    03 01 02
    07 04 06
    

    And then the final penalty matrix becomes:

    0 2 0
    2 1 3
    1 1 1
    

    If I misunderstood your approach, sorry. Could you please provide a detailed example for the case when $$$n=3$$$ and $$$m=3$$$?

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

      Actually I havn't thought about this corner case. Maybe some special strategy should work for this case.

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

      I see now. The original strategy failed when $$$n=m$$$. These cases should be considered independently. I'll try to find out the solution for this.

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

        I believe your approach only works when $$$n=1$$$ or $$$m=1$$$; it fails for other cases. You are using induction, but your approach fails on the base case.

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

          You're right. I've slightly adjusted the strategy and thus fixed the issue. Sorry for the inconvenience.

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

    Yes, now it is working 🎉: 324418651

    But I want to add something:

    1. I don't know why the first if condition is needed.
    2. I rotated your 3x3 matrix by 90 degrees.
    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      The main reason for these problems is that you need to consider the position of the grid where the number 1 is written. Here is my implementation: 324437128

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

https://pastebin.com/LWEC8Ai4 what wrong in this code..? i am trying to convert each number into a nearest number in which all bits are set (2^i — 1) and simultaneously decreasing k

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

    actually strategy is flawed, there could be a solution sometimes when you have to set only a few bits of some number, for that notice that adding 1<<x, where x is the unset bit is the fastest way to set a bit, so simply keep track of positions of all unset bits, and then sort them and subtract one by one from k.

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

Wasted so much time on B, I solved D just 2 min after the contest was over. Btw nice contest.

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

If we reach the same traffic light from the same direction twice than the answer is no.

can someone explain it. I was thinking that if we reach the traffic light twice with same time modulo k with same direction then only answer in no. Thanks in advance.

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

    they are same as light only turns when time modulo k = delay .. so if you reach it twice you will reach whentime modulo k = delay` only ..

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

      so if you reach it twice you will reach when time modulo k = delay only

      I am unable to grasp it. Can you provide a proof ?

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

        oh the light only turns red when time = k.l + delay so time % k = delay % k = delay because it is given that delay < k

        so if you meet a red light .. then time % k = delay ... not just for twice.. anytime you meet a red light this has to follow from the given equation

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

    i have another condition that didn't work: if we leave the traffic light in the same direction twice, then the answer is no. Would appreciate if someone how it's different from editorial condition

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

      if you are at a red light initially ... then you will immediately reverse.. did you handle this case ?

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

    Aree just think it this way,.. u are proposing that we need to make sure we reached the traffic light both the times with same (t%k) value. But the point is, when u reach a red light, t%k should be d... Else it's green isn't it. So t%k always d, whenever u crash the same red light.

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

    If you reach the light twice from the same direction. Then it means you have completed a whole loop which takes x*k time where, x>=1. As you reverse direction after visiting it the first time and then somehow make it back again. It can be concluded that you are stuck in an infinite loop.

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

Can someone share beautiful and clean code for D1?

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

thank you for a superfast editorial

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

Here's how I came up with the solution to problem F. The text is too long, so I put it into the spoiler:

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

Hopefully i reach pupil after this contest :)

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

neat problems B and C

misread D rip

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

nice problems A bit of tricks into each one of them! Thumbs up for the problemsetter.

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

Hello, My implementation for problem C is this -> 324129946. It was giving WA in pretest 5 . Can anyone please point out the mistake .

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

Rank 9, PAPABOLO has a very interesting submission order. A, (incorrect B), (incorrect C), (incorrect B), D1, E, D2, (incorrect C), B, (incorrect C), (incorrect F), C

Its a real story of overcoming adversity to achieve such a high place with this submission history and starting the contest 30 minutes late.

Spoiler
  • »
    »
    10 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it -62 Vote: I do not like it

    Hey, I started coding 2-3 minutes into the competition. And then after that, because previously I was hitting like incorrect solutions and facing penalties on previous competitions, so I was building my own test cases, so by the time half an hour was complete I believe I had already coded up the first few problems which I thought would be easier for me to solve, and they were running on different test cases(mine). So once that was done I submitted them in the order the test cases I made stop running.

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

Hello, My implementation for problem D is this -> 324142473. It was giving WA in test 10 . Can anyone help me in this?

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

can anyone provide a proof for why sorting by distance from centre works for problem E

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

    okay so my observation is that if we take the middle element and then go layer by layer, for any layer k, only the cells of the layer k-1 will be penalized (think about it).

    now as we are going layer by layer we will compare the chessboard distance of that specific layer from the middle element and then go from in to out.

    and as the problem itself suggests that the tiebreaker is manhattan distance, we will then choose those cells with the highest manhattan distance for adding the penalty.

    let m = n = 3

    initially we choose the middle element.

    x x x
    x 0 x 
    x x x
    

    from the middle elements all the cells of layer 1 are equidistant, but the closest are the adjacent cells and then the diagonal ones(bcs of manhattan distance)

    now if we choose the the diagonal ones first then for every next coloring of element the penalty of any diagonal cell will exceed 3

    x x 0      0 x 1      0 x 2      1 x 2      1 x 3     2 x 4 
    x 1 x  =>  x 1 x  =>  x 1 x  =>  x 1 x  =>  0 1 x  => 0 1 x     
    x x x      x x x      0 x x      0 x 0      0 x 1     0 0 1
    

    as one can see that the top left cell exceeds 3. so the better approach is to go from in to out(from closer cells to farther according to the tiebreaker)

    x 0 x      x 1 x      x 2 x      x 2 x      0 2 x      0 2 0      0 2 1      1 2 1
    x 1 x  =>  x 1 0  =>  x 1 0  =>  0 1 1  =>  0 1 2  =>  1 1 2  =>  1 1 2  =>  1 1 2
    x x x      x x x      x 0 x      x 0 x      x 1 x      x 2 x      0 2 x      0 2 0
    

    thus we will extend this for multiple layers

    now this can be easily done by sorting the coordinates with the first key as chessboard distance and the second key as manhattan distance.

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

E was basically guessforce and it alone was worth more than D1 + D2...

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

just me or you can't view solution for e?

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

we cant view any of the implementations

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

Can someone point out the error in my logic in problem C

f(x, j) = minimum cost to make x having at least j bits on by definition if x have already n bits on, then if j < n f(x, j) = 0

we can say that if x is fixed f(x, j + 1) — (x, j) >= f(x, j) — f(x, j — 1)

meaning making j-th bit on costs more or equal than (j-1)-th bit on

so we can just sort by f(x, j + 1) — f(x, j)

then greedily take till which we can, taking one element means ans++

it gets WA at tc 9 Edit Got AC [was a silly mistake]

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

    Your logic is okay, but you're not decreasing k, so it will give WA for for any 0 . Just do k -= wow3[i].ss while adding to the answer.

    The main problem is that you're counting up to 60 bits, but it can never actually make all 60 bits high, because k can be at most 1e18 — which can set at most 59 bits. Your logic is doing now — pre, so the delta of 2^60 can end up being less than k.

    so you code is adding 2^60 which is impossible to add

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

is there some way to make integers like 1 act like 1LL in C++ without explicit type casting? i have lost more time because of this overflowing during 1<<bit than i would like to admit

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

    I mean when you use the integer with some ll quantity during multiplication, addition and all, it behaves like a long long number, otherwise using 1LL is the only way, it doesn't take any time also.

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

I bricked B very hard, other than that very cool problemset

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

Nice contest.Fast Editorial

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

F is very cool!!!! D is good too

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

Great problemset!

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

in E, can someone please explain that what are the key differences when n,m are both even or 1 even 1 odd??

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

Why is $$$1 \leq n \leq 5000$$$ in problem C, why not higher constraints?

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

For A, it was crazy how you could just output all 1s and then all 0s as a binary number and that'd be a valid output cuz the number of 101s and 010s are gonna be 0 for both

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

How can example 2 in problem B be valid? since it's not permuting the first and last row such that every column will have 2 identical elements: e.g. two 1s in the first col and two 2s in the second col...etc

1 2 3 4 5
x x x x x
x x x x x
x x x x x
1 2 3 4 5
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone share your D2 code, please a readable code. Thanks a lot

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it
    Code
»
10 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

When I click on the implementation, it shows me some submission and when I click it it says, "You are not allowed to view submission" or smth like this, what do I do? Please help. Thanks :)

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

    is not you, if you want to check some implementation ask here in comments, or wait writers fix that

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

    All the code in Implementation seem to be linked to submissions in a private gym contest. So we just couldn't view any of them until someone fix it.

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

It's not really obvious to me why the solution of problem F works. Why is the idea of building the rooted trees equivalent to the original problem?

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

    Firstly, let's imagine that the vertices of the trees have their identities tied to the identities of elements on the array. Let the vertex corresponding to index $$$i$$$ have label $$$a_i$$$.

    We can show that the solution is correct by proving 2 things:

    (Necessity) The cyclic sequence of the trees remains invariant under the operation. This is because no vertices can ever move to a different parent nor can the order of them change underneath the same parent.

    (Sufficiency) This is the harder part — we want to show that $$$a$$$ can be transformed into any array that satisfies the same invariant. A nice trick to make this easier is to establish a "canonical" representation for the invariant, and proving that every array satisfying the invariant can be transformed into the canonical representation. Thankfully, there's a pretty natural one here: the pre-order traversals for all of the trees concatenated together in cyclic order, starting at an arbitrary tree; I'll call this $$$c$$$.

    Consider any array $$$a$$$ that doesn't equal $$$c$$$. First, let's rotate $$$a$$$ so that $$$a_1$$$ corresponds to the root of the starting tree in $$$c$$$. Since preorder traversals start at the root, it follows that all occurrences of $$$m$$$ are in the correct place. If something else doesn't match, consider the first $$$i$$$ such that $$$a_i \neq c_i$$$. Let $$$j \gt i$$$ be the index of the first occurrence of $$$c_i$$$ in $$$a$$$ after index $$$i$$$. We'd like to swap $$$a_j$$$ to the left until it reaches $$$i$$$, but might run into something with value $$$a_j + 1$$$ or $$$a_j - 1$$$.

    $$$a_j + 1$$$ is impossible — since the preorder traversal of the tree matched $$$a$$$ up until $$$i$$$, $$$a_j$$$'s parent must be in the correct place. Thus running into another $$$a_j + 1$$$ would mean that $$$a_j$$$ belonged to a different parent.

    $$$a_j - 1$$$ is also impossible — since we know there's no occurrence of $$$a_j$$$ in $$$a_i, a_{i + 1}, ..., a_{j - 1}$$$, it must be true that its parent lies within $$$a_1, a_2, ..., a_{i - 1}$$$ and thus it must've occurred earlier in the traversal.

    It follows that $$$a_j$$$ can be swapped to the left until it reaches $$$i$$$, and repeating this procedure would eventually yield $$$c$$$.

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

Why cant I open the code solution?

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

E could be solved easily by just sorting all possible pairs using this

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

can someone show the graph implementation for D2? the solution in the editorial isn't loading for me(permission error)

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

i tried to see all problem implementation but it shows this error "You are not allowed to view the requested page".How to fix?

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

You are not allowed to view the requested page

What happened?

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

Problem D2. Red Light, Green Light (Hard version) Giving wrong answer on test2. Can anyone explain the reason. Please.

#pragma GCC optimize("O3", "inline")
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
#define pb push_back

struct Compare{
    bool operator()(const pair<int, int>& a, const pair<int, int>& b) const {
        return a.second < b.second;
    }
};

struct Compare1{
    bool operator()(const ll a, const ll b) const {
        return a > b;
    }
};
bool compare(const vector<int>& a, const vector<int>& b) {
    return a[1] < b[1];
}

ll solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<int>> edges;
    vector<vector<pair<int, int>>> adj(n, vector<pair<int, int>>());
    for(int i = 0; i < m; i++) {
        int x, y, w;
        cin >> x >> y >> w;
        x--;
        y--;
        adj[x].pb({y, w});
        adj[y].pb({x, w});
        edges.pb({x, y, w});
    }
    priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> pq1;
    vector<ll> distArr(n, LONG_LONG_MAX);
    distArr[0] = 0;
    pq1.push({0, 0});
    while(!pq1.empty()){
        pair<int, int> delPair = pq1.top();
        pq1.pop();
        for(pair<int, int> x: adj[delPair.first]) {
            if(distArr[delPair.first] + x.second < distArr[x.first]){
                distArr[x.first] = distArr[delPair.first] + x.second;
                pq1.push({x.first, distArr[x.first]});
            }
        }
    }
    ll ans1 = distArr[n - 1];
    
    vector<ll> myArr1(m, LONG_LONG_MAX);
    for(int i = 0; i < m; i++) {
        if(distArr[edges[i][0]] != LONG_LONG_MAX) myArr1[i] = edges[i][2];
    }

    sort(myArr1.begin(), myArr1.end());

    ll ans = 0;
    for(int i = 0; i < k; i++) {
        if(myArr1[i] == LONG_LONG_MAX) break;
        ans += myArr1[i];
    }
    return min(ans, ans1);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--)
    {
        ll x = solve();
        cout << x << "\n";
    }
    return 0;
}
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I solve D2 in O(N) with offline the query, basically just go from largest starting point to smallest starting point and use hashmap to keep track the nearest light point that can be collided with current starting point. My submission: 324206877

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

can anyone explain the problem E , my idea was trying to use bfs from the center of grid, but it seem to be a wrong solution, pls help me, thanks in advance

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

i cant view the solution it says you are not allowed to view the requested page Can someone help?

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

324106193 Could someone please explain why this dp approach for C doesn't work?

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

why I click the implementation and it shows "You are not allowed to view the requested page"

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

Easy solution for 2118B

void solve() {
    int n; cin>>n;
    cout << n * 2 << endl;
    for (int i = 1; i <= n; ++i) {
        cout << i << " " << 1 << " " << n - i + 1 << endl;
        cout << i << " " << min(n, n - i + 1 + 1) << " " << n << endl;
    }
}
»
10 months ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

The proof of optimality for the greedy solution to problem C is quite interesting.

The editorial already mentions that there is no number more beautiful between $$$x$$$ and $$$y$$$. Let's define a move as a sequence of operations that increases the beauty of a number by $$$1$$$ (i.e., transforms $$$x$$$ into $$$y$$$). The cost of a move is the number of operations required to perform it.

Now, a natural question arises: how does greedily selecting the move with the minimum cost lead to a maximum total beauty? Let me explain this in two steps:

  1. Consider a variation of the classic 0/1 Knapsack Problem in which all items have the same value. In this setting, a greedy strategy of always selecting the lightest available item first is optimal. Now, at first glance, it might seem that this does not minimize the leftover capacity (i.e., the "gap") in the knapsack. However, the solution remains optimal in terms of total value.
    Suppose we construct an optimal subset by including a heavier item instead of a lighter one. If we then replace that heavier item with the lighter one:

    • The solution remains valid, because the total weight decreases and still fits within the knapsack's capacity.
    • The total value remains unchanged, since both items have the same value.
      So, any optimal solution using heavier items can be transformed into an equally optimal solution using lighter items. Therefore, greedily selecting the lightest items results in an optimal solution.
  2. In this problem, each move increases beauty by exactly $$$1$$$, and we are allowed to perform at most $$$k$$$ operations. Thus, every move has the same value (i.e., +1 increase in beauty), while the cost (number of operations) varies from move to move. This maps directly to the knapsack variation described above.
    There is just one difference. In our problem, not all moves are available from the beginning. As we perform moves, we discover new moves as the transformed number can now get a new transformation (ex., after transforming $$$45$$$ to $$$47$$$, it can then be transformed into $$$63$$$).
    Let’s compare what happens when we use our strategy in two scenarios: the actual one, where moves are discovered gradually, and an ideal one, where all moves are available from the start. The only potential concern is that we may miss a minimum-cost move in the actual scenario because it hasn’t been discovered yet. But in our setting, there is a guarantee that this will never happen.
    The cost of a move is $$$2^{\text{position of rightmost 0}}$$$. Once a move is used on an element, the position of the $$$\text{rightmost 0}$$$ must increase. So, the newly discovered move from that element always has a higher cost. At any point, the minimum-cost move among all currently available moves is guaranteed to have the globally minimum cost (as the unavailable future moves will have higher cost).
    Therefore, the greedy strategy of always applying the minimum-cost move maximizes the number of beauty-increasing moves that can be performed within the budget of $$$k$$$ operations. This proves the optimality of the greedy solution.

Note:
0/1 Knapsack Problem — Given a knapsack with capacity $$$c$$$ and $$$n$$$ items, where each item has a weight and value, the goal is to choose a subset of items such that the total value is maximized, maintaining the constraint that the total weight of items in the subset does not exceed $$$c$$$.

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

In 2118B - Make It Permutation, is the most optimal solution not 2*n -1 instead of 2*n? (Just curious)

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

    I think it is 2*n-3 which is coming like this for all rows except row 1,row n-1 and row n cost is 2 so 2*(n-3)=2*n-6 and for those 3 rows it is 1 per row ,so total 2*n-6+3=2*n-3

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

an 'interesting' way d1 could be 'ACed' is by running 2500000 sec on every query i knew this from my friend TranSiQuy who ACed this way

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

Hey, Can someone help me out ?

I am unable to see the solutions for this contest.

When I click on the Implementation code, it says "You are not allowed to view the requested page"

Pls help me out.

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

Can any explain the graph approach for D2.

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

    First observation you need is that if you hit a specified light from the left the next red light you hit will always be the same one, independent to which point in time you hit the red light (same goes for the right side). So if the next red light is predetermined you can construct the following graph: for node i you store which red light you will hit first if you hit a red light at node i and a) turn left, b) turn right. From this you can either dfs down this graph and store a query-s answer in each of the visited nodes, or find all nodes that go into a cycle.

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

Is 2118B - Make It Permutation a ad-hoc problem?

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

I don't understand why my submission of problem C is wrong[submission:324087417]. Can someone help me!

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

rejdioglava09 orz for being soooo skibidi sigma i got hot watching you send solution for problem b

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

by the way, I don't have access to the implementations of C and D. I assume it's also the case for others. please fix it when you get chance. thanks.

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

good contest !

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

You are not allowed to view the requested page. when i try to see solution how to fix

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

I thought the editorial for D2 was a little confusing. I couldn't quite understand what d[y]-y and (t-x) had to do with each other. The diagonal imagery helped though, and after a while it made sense to me. The idea became clear to me after drawing a small example on the graphic. If anyone else is still confused, this might help. It then becomes clear how (t-x) and (d[y]-y) relate and why we're interested in hashing them into the same bucket.

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

Maybe someone knows if it’s possible to compute F without constructing the trees?

I had the following idea: for each s in [2,m], consider an array where the ith value is the number of occurrences of s between the ith and (i+1)th occurrences of s−1. Let's call such arrays linkings. Then, the linkings of A and B should be rotations of each other, and we can detect this using FFT. Moreover, we can determine which shifts are viable: they will be of the form kd+r, where d is a divisor of the number of occurrences of s−1.

The problem is that we can’t rotate different linkings independently. Maybe someone has ideas on how to resolve this? Or are these trees essentially the only way?

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

    Let's transform the array such that $$$a_i \gt a_{i+1}-2$$$ for every $$$i$$$. You may notice that the cyclic shift of this transformed state is unique. From this, you can construct a solution: transform both $$$a$$$ and $$$b$$$ into this form and check whether they are cyclic shifts of each other. This can be done in $$$O(N)$$$: 324451173.

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

does anyone have the implementation of the second solution for D2??

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

I have solved D2 in O(n) using Unordered_map<ll,vector> and vector to find nextGreater and nextSmaller. Store the queries in array along with all positions and iterate backwards.

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

Dear Codeforces Team,

My submission (#324131839) for Problem 2118A in this contest has been flagged for similarity to other submissions. I want to clarify the following:

  1. I wrote the code entirely on my own during the contest, without viewing or copying anyone else’s solution.
  2. I did NOT publish or share my code on any public platforms (e.g., ideone.com, pastebin.com, Discord, etc.).
  3. I coded locally, and the solution was never accessible to others.

I have great respect for Codeforces’ rules and the spirit of fair competition. If this similarity resulted unintentionally (e.g., due to coincidental logic structure), I kindly request a reconsideration of my submission.

Thank you for your time and understanding. I will be more diligent in the future.

Sincerely,
Niloy_Chakraborty
Codeforces handle: Niloy_Chakraborty

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

Hi, I received a plagiarism warning regarding my submission [324124484] for Problem 2118C. I want to clarify that I did not intend to break any rules. I wrote the solution independently and did not share it with anyone. However, if it unintentionally became accessible possibly through a public IDE or an oversight on my part—I sincerely apologize. I respect the Codeforces platform and will be extra careful moving forward to ensure there’s no chance of unintentional leakage. I kindly request that this be taken into consideration. Thank you for your understanding.

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

    This won't be happening again from contest 1032 I Sincerely apologize For unknowingly violating the rules . I guarantee you this was not gonna happen from the contest number 1032. Thanks for understanding

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

About C: log10^18 is a constant, you don't need to write it in O-notation.

Sorry, i don't know how to use LaTeX in comments.

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

please someone tell me the issue with my code

https://mirror.codeforces.com/contest/2118/submission/327422523

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

The second problem has not answer code

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

the diagonal idea was amazing

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

I got an different approach for C. we can use vector <bitset<64>> to represent each number and the n just greedily iterate on each lsb which is 0. if we have enough operations then we flip the bit otherwise we exit the code as we have lesser than required operations.

finally use bitset.count() to get our answer

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

For problem E, I had really achieved a solution with penalties in every cell not more than 2!

Code Link

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

B can also be done with** 2n-3** operations.

Spoiler for B