Блог пользователя GlowCheese

Автор GlowCheese, 2 года назад, По-английски

1993A - Question Marks

Hint
Solution
Code (python)

1993B - Parity and Sum

Hint 1
Hint 2
Solution
Code (python)

1993C - Light Switches

Hint 1
Hint 2
Solution
Code (C++)

1993D - Med-imize

Hint 1
Hint 2
Solution
Code (C++)

1993E - Xor-Grid Problem

Hint 1
Hint 2
Solution
Code (C++)

1993F1 - Dyn-scripted Robot (Easy Version)

Hint 1
Hint 2
General idea
Solution
Code (C++)

1993F2 - Dyn-scripted Robot (Hard Version)

Solution
Code (C++)
Разбор задач Codeforces Round 963 (Div. 2)
  • Проголосовать: нравится
  • +218
  • Проголосовать: не нравится

»
2 года назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

Wow really fast editorial thank you

»
2 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

D is cool

»
2 года назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

thanks for fast editorial

»
2 года назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

can someone explain C

  • »
    »
    2 года назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +12 Проголосовать: не нравится

    Realize that all the chips are activated after the max(a_i). Let's call the interval corresponding to this max time as interval I, and the chip corresponding to it as chip i. So, since the entire pattern of the intervals being on/off is periodic, we know that there is nothing special (i.e. different) that happens later (after interval I). So, we know that, if the intervals all overlap at some point, it will happen in this first occurrence of chip i being on, over interval I. So, we can just maintain the intersection of the ON intervals (within I) across all the chips, using a high and low. If we end the intersection with a valid interval, the answer is the earliest time in that interval, otherwise the answer is no.

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +19 Проголосовать: не нравится

It's so refreshing to see all the participants solving the contest I'd tested.

Thank you GlowCheese for inviting me to become a tester.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Thanks for the Editorial The problems are really fun :D

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can we use binary search for problem C ? :)

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem C was really funny.. thx :)

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -28 Проголосовать: не нравится

Really great problems with nice observations, why is the editorial getting downvotes ? Is it because you guys are mad not able to solve the problems ?

»
2 года назад, скрыть # |
 
Проголосовать: нравится +20 Проголосовать: не нравится

I enjoyed solving D and E very much :) which is quite rare for a div2 round

Thanks a lot

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Wow fast Editorial

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

wish i solved D, but really fun contest nonetheless!

»
2 года назад, скрыть # |
 
Проголосовать: нравится -56 Проголосовать: не нравится

algorithm contest -> ❌ math contest -> ✅

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Ah, I should've been able to do B. Don't know where my code went wrong. Anyway, Thanks for the editorial.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Nice Solutions.

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +9 Проголосовать: не нравится

The solution for F1 (and F2) is really elegant and tricky! I enjoyed (attempting to) solve the problem very much. Hope to see more of such problems.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

Editorial is high quality. Nice problems too, been a while since I've seen a moving interval one (C).

»
2 года назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +1 Проголосовать: не нравится

274419542 why i didnt get TL for C task? Its O(n*k)

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don’t understand why binary searching works on D They aren’t even checking whether the bs element is present in array or not?

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please explain in problem D if I am doing binary search on median and getting that number "mid" should be in middle, how am I even able to check by dp if I can remove segments accordingly, please explain the check function in simpler terms. Thanks in advance!

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    mid should not be in middle, it should be less than or equal to median. Means we are binary searching on the predicate: f(mid) -> is mid lying to the left of median? and we can do that by counting elements greater than mid, if they are greater than half the size of array, then it will return true.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

in C, I did linear search in the range max(array)to max(array)+k and got the answer. the check function i used for every numbers in the range is- ~~~~~ function<bool(int)>check=[&](int t){ for(int i=0;i<n;i++){ if((t-v[i])<0||((t-v[i])/k)%2!=0) return false; } return true; }; ~~~~~ can someone explain how these range working, also i think the complexity is O(N*k)??

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

E is really a good problem

»
2 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Why, in problem C, the solution with time complexity O(nk) not giving TLE?

I first submitted O(nk) solution (274384802).

Then later, submitted O(n) solution (274394074).

After the system testing, I again submitted my O(nk) solution just to check if it is passing or not, and it got passed. I think the test cases were quite weak for Problem C

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

B- Parity and Sum

can someone please help me understand why my solution was WA on test case 2

C++ solution:

#include<bits/stdc++.h>
using namespace std;
#define ll long long

// Code By VibhuGodson
int main ()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll t=1;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        vector<ll>evn;
        ll mxod=-1;
        ll x;
        for(ll i=0;i<n;i++){
            cin>>x;
            if(x%2) mxod=max(mxod,x);
            else evn.push_back(x);
        }
        sort(evn.begin(),evn.end());
        ll ans=0;
        if(evn.size()==0 or evn.size()==n){
            cout<<ans<<endl;
            continue;
        }
        for(auto&i:evn){
            if(i>mxod){
                mxod=2*i+mxod;
                ans+=2;
            }else{
                mxod = i+mxod;
                ans++;
            }
        }
        // cout<<"---------------";
        cout<<ans<<endl;
    }
    return 0;
}

I think the intution is correct, I'm doing the same same thing as per the solution

  • »
    »
    2 года назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    It's was my first try too. The problem in this solution is that sometimes choose the biggest even number from evn, it's better than a smaller one. Because if you take $$$a \gt b$$$, which is at the end of evn, then you increase mxod and now mxod = mxod + max_elemen(evn), so now mxod is in 100% of cases is greater than any evn and you don't need anymore ans += 2

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What is the dp state in the editorial of problem D?

»
2 года назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

As usual, thanks to the authors for this round ! Here is my advice about the problems:

A
B
C
D
E
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In Problem D they did not specify that sum of k is less than 5e5, so after I got pretests accepted with O((n + k) * log(1e9)) per testcase solution, I assumed that it whould fail the system test because of the Time Limit. I fixed and resubmited the solution with O(n * log(1e9)) time complexity and it passed the system tests, but it seems like there was not any test case in the system tests that results in a TLE. So if anybody wants to get free hacks for problem D, they can easily construct a testcase where t = 1e4 and n = 1, k = 5e5 for each test case.

»
2 года назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

For D. My solution https://codeforces.me/contest/1993/submission/274431244 is giving RTE on the following test case:

1
1 500000
3169420

But when I am running it locally it is working just fine. Can anyone please help me?

»
2 года назад, скрыть # |
← Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

I solved C, in a slightly different way

1) If the answer exists it's always going to be from (max(a), max(a)+k-1). (Observation from provided test cases).

2) so we add (2*k*r(some integer) to each element a[i] in place, such that a[i]+2k is just greater than max(a).(2k because in every 2k intervals the light will be on)

3) we need to find two things if(max(a)-min(a)>=k) the answer will be -1, else the answer is max(a).

Solution here: https://codeforces.me/contest/1993/submission/274401978

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Correct me, if I'm wrong, but shouldn't we choose both n+1th row and m+1th column at the same time to correctly calculate R and C?

I mean, e.g. when calculating C, we need at the same time remove one row (to look for the best solution on n remaining rows) and remove one column (because otherwise distance between rows cannot be calculated correctly). Hence complexity should be somewhat $$$O((n+1)(m+1) \cdot ((m+1)^22^{m+1} + (n+1)^22^{n+1}))$$$ or $$$O(n^42^n)$$$

»
2 года назад, скрыть # |
← Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

I think the editorial for problem B is for another version of the problem where we can make operations on numbers having the same parity ? You could fix it by just removing the part talking about that x)

Btw it's quite cool that the editorial was writen that much in advance!

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Guys i have a doubt

https://codeforces.me/contest/1993/submission/274430880

Im getting wrong answer on the above submission

but my other solution (https://codeforces.me/contest/1993/submission/274433114) is getting accepted why? i understand the logic of the 2nd one but whats wrong with my first submission?

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Consider test case with n = 3, a = [1, 2, 6]. Your algorithm would first set maxodd to be 1, then it would use 2 operations to turn 1 -> 3 and 2 -> 5, then 2 more operations to turn 5 -> 11 and 6 -> 17. But in an optimal answer you never need to use 2 operations on an even number more than once, so the actual answer is 3 and not 4.

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    If maxodd is less than v[i], you should add the largest even number to maxodd, instead of v[i]. Adding v[i] may lead to extra operations.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

There's typo in editorial of D, it should be: $$$b[i]=1$$$ if $$$a[i]≥x$$$ right?

»
2 года назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится

Funny thing is: there is a recent video with 3blue1brown which mentioned general idea from F (they called it quite popular but i'm not really into geometry so idk)

With that video in mind it was much easier to think up the solution

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

In problem B you must take numbers such that have distinct parities. So you can remove even + even & odd + odd options from editorial. EDIT: I'm a bit late to tell it first.

I think
»
2 года назад, скрыть # |
 
Проголосовать: нравится -11 Проголосовать: не нравится

GlowCheese

1) why the fuck is not k <= n in problem D, it makes no sense

2) why do tests not have k = max, and 1e4 tests???? How do you miss that in the tests. My solution uses an array of k size and passeed the initial tests https://codeforces.me/contest/1993/submission/274400595

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem D might be my favourite problem ever.

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone help me figure out what the time complexity is for my solution to D?

I used recursion with memoization to solve for the answer. Let the initial size of the array be N. I start with the full array, and using recursion I get the answer for every possible array of size N-K, and so on. I store the maximum answer. I keep doing that until the size of the array is smaller than or equal to K. Then I find the median of the array. I have an map which stores the indices that I have removed.

I'm thinking the time complexity is O(N^2log(n)). My reasoning is that there are ~ N^2 subbarays of different sizes, and the sort takes log(N) time. Is that correct.

This is my code: https://codeforces.me/contest/1993/submission/274435465

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

B Video Editorial : https://youtu.be/xTb58OEZnJM

C Video Editorial : https://youtu.be/6xtmLO43mD4

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem D was cool.

Approached the problems in a million ways. But didn't think in this way.

Was thinking about BS + some sort of greedy

Also thought about some DP

But nice technique of reducing this problem only to a simple dp sum problem.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in D it should be x<=a[i],right?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Was C that easy? I am unable to find any clue to solve even after seeing the comments and editorial

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hello

Can someone help me with my solution for C? I'm getting wrong answer on test 2 on the 2003rd test, so there's an off by one error somewhere. Here's the link to my submission: https://codeforces.me/contest/1993/submission/274445861

I take the time of the light being installed as lowerbound and that time + k as the upperbound, and I set the lowerbound in an array to be +1 and the upperbound as -1 However if the lowerbound to upperbound contains 2k in the middle, then it loops around after the mod, so i set the lowerbound to +1 and the 0th index to +1 and the new upperbound to -1 to account for this loop around Then I add in the array as I go until I find the first index that has the value n (meaning all lights are on at this moment) and at the end I check if index 0 was an answer, then I shift the answer to put it after max(a)

»
2 года назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

why is this code for problem C not giving correct output on codeforces compiler? Link to Submission

while same code gives correct output on my local vscode and also on codechef online c++ compiler

for the 1st test case it is giving correct output as given in the problem

My Code
»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I solved C in an another way. As the answer should be at least the maximum number of the array a and every 2k minutes the condition of the light in the apartment is repeated, I decided to build a segment tree, where in each segment of time I save one of three numbers:

0 — means during this segment of time there's no time when all of the lights in the apartment are on

1 — means during the whole segment of time all of the lights are on

2 — means during a part of the segment of time all of the lights are on

The information in the segment tree is updated by running through the array and performing some mathematical operations. Including that the total time which we are looking through is 2k, the time complexity of the algorithm is O((n + k) * logk).

Code: 274409062

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In C we also could have taken everything mod 2k and added 1 everywhere light is on and subtracted 1 everywhere light is off using prefix sum array . Now we had to check at which indices mod 2k val is equal to n means all the ranges intersect.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Thank you for the fast editorial!

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone help me point the mistake in my B solution ?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I think there's an easier solution for problem D. Instead of trying to leave as many large elements as possible, we try to leave as few small elements as possible. to prevent the resulting sequence becoming empty, we define dp[i][j] (0 <= i <= n, j = 0 or 1) as the least number of alive elements less than x. refer to my code for dp transition. https://codeforces.me/contest/1993/submission/274378351

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain the dp in problem D in more detail?

What does the state represent?

Why are the transitions the way they are?

Thanks in advance.

  • »
    »
    2 года назад, скрыть # ^ |
    ← Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится

    I have an alternate DP definition.

    A subsequence is called good if the gap between each consecutive elements is divisble by $$$k$$$.

    Define $$$dp[len]$$$ to be the maximum sum of a good subsequence of length $$$len$$$. Suppose you append an element $$$a[i]$$$ to the array. How do you refresh the DP? The only subsequence that we haven't considered are the ones ending at $$$i$$$. If the subsequence ends at $$$i$$$, there are exactly $$$i$$$ elements to the left. Hence, $$$i\%k$$$ elements would remain after the operation. Therefore, if a subsequence ends at index $$$i$$$, it has to have length equal to $$$len = 1 + (i\%k)$$$. Therefore, we only need to refresh $$$dp[len]$$$ from $$$dp[len - 1]$$$.

    The answer would be $$$dp[r]$$$ where $$$r$$$ is the number of elements that you want to retain at the end.

    vector<int> dp(k + 1, -inf);
    // dp[len] is the maximum sum of a good subsequences of length len.
    dp[0] = 0;
    
    for (int i = 0; i < n; i++) {
        // If the subsequence ends at i, then its length is predetermined.
        // There are i elements to the left. From these elements, you
        // need to remove blocks of size k.
        int prev_cnt = i;
        int len = 1 + prev_cnt % k;
        dp[len] = max(dp[len], dp[len - 1] + a[i]);
    }
    
    return dp[req_len];
    

    Full Code

    CC: ducdung2k5 Sveshnikov

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Excellent editorials for D and F1, learnt a lot

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Seems a typo in solution for D:

$$$b[i]=1$$$ if $$$x\ge a[i]$$$, otherwise $$$b[i]=−1$$$.

if $$$sum(b) \gt 0$$$, then the condition $$$x\le med(a)$$$ is satisfied.

Should it be $$$b[i]=1$$$ if $$$x\le a[i]$$$?

»
2 года назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

A solution to F2 withou CRT:

  • First, calculate the mininum $$$i_x,i_y$$$ such that $$$i_xx_n \equiv -x_j \pmod {2W},i_yy_n \equiv -y_j \pmod {2H}$$$. We could use exgcd to solve this.
  • Second, calculate the mininum $$$p_x,p_y$$$ such that $$$p_xx_n \equiv 0 \pmod {2W},p_yy_n \equiv 0 \pmod {2H}$$$.
  • At last ,the problem is calculate how many $$$x \in [0,k-1]$$$ satisfiying $$$x = r_xp_x + i_x = r_yp_y + i_y$$$ ($$$r_x,r_y$$$ is any non-negative integers). This is the same as CF710D, we can solve it using exgcd too.

Thus this problem is solved in $$$\mathcal O(n \log V)$$$.

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +6 Проголосовать: не нравится

It would be better if it had been guaranteed that $$$k \leq n$$$ in D.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

https://codeforces.me/contest/1993/submission/274528860

The tc of above solution is 0(n*log(1e9)) but it is giving TLE for Question-D.Please help!

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain, why in D, we are having to separately consider the case when I % k == 0,

This is my code as I don't understand where it goes wrong:

Code:
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Very educational round. Problem D and Problem F1 are interesting. Tricks used in Problem D amazed me. Apart from that, the editorial explains clearly.

»
2 года назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Small typo: in the editorial for F1,

"Let's call $$$t_k=(x_k,y_k)$$$ how much the robot moves (in direction) after executing the first $$$i$$$ commands of the script."

should be

"Let's call $$$t_k=(x_k,y_k)$$$ how much the robot moves (in direction) after executing the first $$$k$$$ commands of the script."

Doesn't impact the solution that much though. Pretty elegant and cool idea, worthy of an ARC or even AGC.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem D is very impressive, It's nature is very interesting, I need more like this.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hello! Can someone please help me understand how the check function in D is monotonic? I get that it will be monotonic when not deleting segments, but I'm not exactly sure how it would be monotonic when we consider the fact that we are deleting segments.

Thanks for everything, really appreciated :)

  • »
    »
    2 года назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    When we check if a $$$x$$$ is less than or equal $$$med(a)$$$ after deleting segments, we want $$$sum(b)$$$ to be as large as possible. Note that $$$b[i] = 1$$$ only if $$$a[i] \ge x$$$; that means if $$$x_1 \gt x_2$$$ and $$$b[x_1] = 1$$$, then $$$b[x_2] = 1$$$ also, which leads to $$$sum(b)_{x_1} \ge sum(b)_{x_2}$$$. The smaller $$$x$$$ gives the larger $$$sum(b)$$$ so yes, the check function is monotonic!

    • »
      »
      »
      2 года назад, скрыть # ^ |
      ← Rev. 2  
      Проголосовать: нравится 0 Проголосовать: не нравится

      Thanks for your answer! I am beginning to understand now! But still I'm unsure as to how you can be sure that at the end, hi will be the number such that sum(hi) is precisely 1 (if (n-1)mod k + 1 is odd) and 2 (if (n-1)mod k + 1 is even), because ultimately hi should be the highest possible median.

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        That is how I usually implement binary search. Remember in the code I wrote:

        if (check(mid)) {
            lo = mid + 1;
        }
        

        Which means mid can be the median we're finding but later we set lo = mid + 1. In the end, lo <= hi is no longer true, which means hi = lo - 1 = (med(a) + 1) - 1 = med(a). That's it ;)

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone tell me how to solve problem E in O(n^3 2^n)

enumerate the deleted line and row: O(n^2)

let f[i][j] be the answer with bitmask i, last choose is j, dp in O(n^2 2^n)

total: O(n^4 2^n)

how can I do it faster?

my submission:https://codeforces.me/contest/1993/submission/274615702

  • »
    »
    2 года назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Make a matrix such that matrix[x][y] = the answer if you don't use the xth row and yth column. Final answer is obviously the minimum element in this matrix.

    Calculate the optimal sum between horizontally adjacent elements for each x and y. You do this by looping over which row you don't use, then do bitmask dp on the columns. This is n^2 * 2^n for each row, so n^3 * 2^n total. After calculating the dp (dp[mask][last] = minimum horizontal differences such that you use the columns in mask and the last column is last), you update the matrix like this: let's say you are currently calculating the answer such that you don't use row rno. matrix[rno][c] += min(dp[mask with all ones except for c][last]) over all values of last.

    Then repeat for vertically adjacent elements. Thus you optimize from n^4 * 2^n to 2 * n^3 * 2^n.

    274629969 Hope this helps

»
2 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

The solutions are excellent!!!!!!!

»
2 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

E is really beautiful

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in $$$D$$$ it was a bit unclear to me how this $$$dp[i]$$$ ensures that the answer will be calculated only among all possible subsequences of length $$$(i - 1)$$$ $$$ mod $$$ $$$k + 1$$$.

since i didn't find any mention of this, that's how i explained it to myself:

when $$$1 \lt = i \lt = k$$$ we can't delete anything so $$$dp[i]$$$ is the answer among all possible subsequences of length $$$i$$$ (which is just a prefix of length $$$i$$$)

when $$$i = k + 1$$$ we now that we must delete segment of length $$$k$$$ to get length $$$1$$$ and $$$dp[i] = max(dp[1], b[i])$$$. thus we ensure that now length is $$$1$$$ and we kind of returning to case $$$1 \lt = i \lt = k$$$ because if $$$i = k + 2$$$ length will be $$$2$$$, when $$$i = k + 3$$$ length will be $$$3$$$ and so on

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

It seems like the official code for Problem D can be hacked.

Specifically, let's consider the following input:

1
5 4
100 5 5 5 100

In the above example, the length of the original sequence is $$$5$$$ and the $$$k$$$ is $$$4$$$. Since the length of the operated sequence should be at least $$$k+1=5$$$, thus we should not perform any operation and the final sequence should be as same as the original one, which is $$$[100, 5, 5, 5, 100]$$$.

Obviously, the median of such a sequence is $$$5$$$, but the official solution of Problem D will output $$$100$$$, which is wrong.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

E is really magic

»
18 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Just solved D in a cool way not mentioned in editorial, let me explain it here, I find it easier in some sense.

Let's do binary search over answer and then build $$$b_i := a_i \ge x$$$. We know how much cuts we have to do (something like $$$B := \lfloor\frac{n-1}{k}\rfloor$$$, and we know how much elements at the end we will have ($$$N := \frac{n - B \cdot k}{2}$$$). Out of N, we should have more than half equal to one, so it's ($$$A := \frac{N}{2} + 1$$$) 1's that we kept until the end.

Now, instead of noticing the fact on indices mod k for 1's (I haven't while solving), let's build the initial array, reversing the operations. In fact we can do it left to right, saying that we either add a single element that lives until the end or we take a segment of $$$k$$$. We can notice that it doesn't matter in which order we destroy such segments so this decomposition always works.

So, we have to construct the array, having A ones and B segments. Supported operations are "append a segment" and "append current element and process if it's 1". I don't know how to solve it directly but the trick is to do $$$dp_{a,b}$$$ that denotes what's the minimal current index such as we already took $$$a$$$ 1's and $$$b$$$ segments. It has $$$O(1)$$$ transitions if you do $$$O(n)$$$ precalc. The issue is the number of states: but one can notice that we need $$$dp_{A, B}$$$ and $$$A = O(k)$$$ and $$$B = O(\frac{n}{k})$$$, so total number of states is linear.

I think this is a cool approach, i liked it very much. 313844486

»
6 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится