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

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

We thank everyone who participated in the round.

1968A - Maximize?

Author: JuicyGrape

Hint 1
Hint 2
Hint 3
Solution
Implementation
Rate the problem

1968B - Prefiquence

Author: FBI

Hint 1
Solution
Implementation
Rate the problem

1968C - Assembly via Remainders

Author: JuicyGrape

Hint 1
Hint 2
Hint 3
Solution
Implementation
Rate the problem

1968D - Permutation Game

Author: FBI

Hint 1
Hint 2
Solution
Implementation
Rate the problem

1968E - Cells Arrangement

Author: JuicyGrape

Hint 1
Hint 2
Hint 3
Solution
Implementation
Rate the problem

1968F - Equal XOR Segments

Author: JuicyGrape

Hint 1
Hint 2
Hint 3
Hint 4
Solution
Implementation
Rate the problem

1968G1 - Division + LCP (easy version)

Author: JuicyGrape

Hint 1
Hint 2
Solution
Implementation
Rate the problem

1968G2 - Division + LCP (hard version)

Author: JuicyGrape

Hint 1
Hint 2
Solution
Implementation
Rate the problem
Разбор задач Codeforces Round 943 (Div. 3)
  • Проголосовать: нравится
  • +93
  • Проголосовать: не нравится

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

any good resources or blogs to explain Z-function?

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

I was thinking too complex at the problem D

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

Should Problem B be just a 2 pointer problem with a time complexity of O(min(a, b))?

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

There exists an $$$\mathcal{O}(n \log^2 n) $$$ solution with a very good constant factor for G2, reply to this if interested.

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

    I just thought of one too. Does it involve sorting and parallel binary search?

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

      You don't need any of that. You could simulate what you do in G1 with a set/segment tree by iterating over the lengths, and it works. The time complexity came from harmonic series and the log from DS.

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

      Not sure what “parallel binary search” means, but it is nested binary search. I don't sort anything.

      Basically, we still have our nice $$$z$$$ function, however, we will also store an RMQ which can get the maximum value of $$$z_i$$$ for all $$$i$$$ in a range $$$[l, r]$$$.

      Let's precompute the value $$$mx_i = \text{maximum number of disjoint substrings with length } i, \text{which are all equal to the prefix of length } i$$$

      If we want to check if $$$k$$$ disjoint substrings are possible with $$$\text{LCP}$$$ $$$l$$$, we just check if $$$mx_{l} \ge k$$$.

      Now the problem is the computation of $$$mx$$$.

      For length $$$i$$$, there can be at most $$$\frac{n}{i}$$$ disjoint substrings with that length. Let's use the fact that $$$\sum_{i=1}^{n}\frac{n}{i}=\mathcal{O}(n \log n)$$$.

      Using our RMQ, we can, instead of calculating $$$mx_i$$$ in linear time in the length, we can calculate it in $$$\mathcal{O}(mx_i \log n)$$$, simply binary search on the next $$$j$$$ such that $$$z_j \ge i$$$ (here, we binary search and use a sparse table to get this next $$$j$$$ in $$$\mathcal{O}(\log n)$$$, alternatively, you can walk on a segment tree in $$$\mathcal{O}(\log n)$$$).

      The complexity of this computation is $$$\sum_{i=1}^{n}mx_i\cdot \log n = \log n \sum_{i=1}^{n} mx_i \le \log n \sum_{i=1}^{n}\frac{n}{i}=\mathcal{O}(n \log^2 n)$$$

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

FOR G2: answer f(x) is decreasing function, so i just modified simple binary search solution for G1, by just changing the upperlimit as previous answer.
Code: 259245908
I am not able to prove this tho... please help me.

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

Thanks for your brilliant problems! Round was fun enough to spend full competition time just solving the problem.

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

Can someone please tell me what's the rating of problem D? I'm asking because the rating is not updated on the problem tag.

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

For B: Optimise Answer

void solve() {
    int n, m; cin >> n >> m;
    string a, b; cin >> a >> b;
    
    int j = 0;
    for(int i = 0; i < m; i++) {
        if(a[j] == b[i]) j++; 
    }

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

Made video editorials for C,D,E for whoever wants to refer

C : https://www.youtube.com/watch?v=oEx5rR4wAKw

D : https://www.youtube.com/watch?v=XBUN4LwolEM

E : https://www.youtube.com/watch?v=DObsk_Rlhg8

language => Hindi

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

In problem F, my submission 259182179 has been hacked. But I resubmit it and got AC... Could anyone tell me why???

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

G1/G2 can be solved without any fancy algorithms as in my rust solution in C++ too: 259314092

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

Is this solution for G2 hackable? Using two heurestics:

1) Memoising the value of cnt calculated in f(mid)

2) Since the answer always decreases on increasing k, keeping the value of hi from previous k(not setting it to n again).

Not able to proove it's time complexity.

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

    I have done the same thing, and I was able to prove the complexity to some extent. It's easy to see that my code is $$$\mathcal{O(n\cdot\text{number of distinct values of } mid})$$$.

    First let's assume $$$l=1$$$ and $$$r=n$$$, the binary search ranges are $$$[1,n],[1,\frac{n}{2}],\dots,[1,\frac{n}{n}]$$$.

    The main observation is this, the range $$$(\frac{n}{2},n]$$$ is exposed to $$$1$$$ binary search, the range $$$(\frac{n}{3},\frac{n}{2}]$$$ is exposed to $$$2$$$ binary searches. Similarly, the range $$$(\frac{n}{i},\frac{n}{i-1}]$$$ is exposed to $$$i-1$$$ binary searches.

    It is clear that with this, we have a very loose upper bound of $$$O(n\cdot\sum_{i=2}^{n}\min((i-1)\cdot\log(n), \frac{n}{i-1}-\frac{n}{i})$$$. However, as i said, this is very loose, as the sum $$$\sum_{i=2}^{n}(i-1)\cdot\log(n)$$$ is quite a bit more than $$$n\cdot\log(n)$$$ (the total number of checks).

    We can make this tighter.

    Let's consider the ranges $$$1$$$ by $$$1$$$, first we have the range $$$(\frac{n}{2}, n]$$$. We have $$$2$$$ cases, the first case is that $$$\log(n)$$$ is smaller than the range, in which case (for the sake of an upper bound), we should assume that we take all the $$$\log(n)$$$ checks from that range, becuase we will never see that range again, so it is best to take as much as we can from that range, hence decreasing the number of times the other ranges can be chosen by $$$\log(n)$$$.

    However, if the range length is less than $$$\log(n)$$$, then all the next ranges are also smaller than $$$\log(n)$$$. So in fact, a better upper bound is $$$\mathcal{O(n\cdot\sum_{i=2}^{n}\min(\log(n),\frac{n}{i-1}-\frac{n}{i}))}$$$, this value is $$$733800000 \approx 7\times 10^8$$$ for $$$n=2\cdot 10^5$$$.

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

      The point where the minimum changes from $$$\lg n$$$ to $$$n/(i-1)-n/i$$$ will be $$$i\approx i_0=\sqrt{n/\lg n}$$$, so this analysis gives a runtime bound proportional to around $$$n\cdot ((n/i_0)+(\lg n)\cdot i_0)=2 n \sqrt{n \lg n}$$$.

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

For G2 there is also solution using the problem:

-we can deactivate an element

-we need to find the closest active element to the right

That can be done using dsu

Then we'll increase lcp. When we increase it by one, we deactivate elements, so we're left only with elements with $$$z_i \ge curLCP$$$ where $$$z_i$$$ is z-function.

To find maximum $$$k$$$ that can achieve at least $$$curLCP$$$ we will start at the beginning and then jump to the next active element of $$$i + curLCP$$$. Since the answer for $$$curLCP$$$ is at most $$$\frac{n}{curLCP}$$$, $$$O(ans)$$$ solution works in $$$O(n log n)$$$, and dsu gives us either $$$\alpha(n)$$$ or $$$log n$$$

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

In G2, shouldn't the time complexity of the case when $$$k \gt \sqrt{n}$$$ be $$$\mathcal{O}(n \sqrt{n})$$$? (the editorial says it's $$$\mathcal{O}(n \sqrt{n} \log{n})$$$)

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

approx. rating of G2? 2100+ right?

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

For G2 I had a similar idea to use the fact that the answer only changes a few times and I calculated the answer for each segment with new n/l.

However, this got WA, not sure why, so I replaced it with an O(n*log^2(n)) (please someone correct me if I'm wrong).

You first binary search for the leftmost index with an answer less than the last one.

Here's the submission 259255876

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

Use hash to check whether two string equal again.

And be hacked again (((

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

    I used hashing too, but I wasn't hacked since I used mersenne twister to randomly select mod for hashing.

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

      Can you share your hashing template?

      • »
        »
        »
        »
        2 года назад, скрыть # ^ |
         
        Проголосовать: нравится +4 Проголосовать: не нравится
        mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
         
        struct Hash {
            int n;
            string s;
            const ll p = 31;
            ll mod = uniform_int_distribution<int>((int)(1e9+7), (int)(1e9+1e8))(rng);
            vector<ll> pw, hash;
         
            Hash(string _s) : s(_s) {
                n = s.size();
                pw.resize(n+1);
                hash.resize(n+1);
                pw[0] = 1;
                for(int i=1; i<=n; i++) pw[i] = (pw[i-1] * p) % mod;
                hash[0] = s[0] - 'a' + 1;
                for(int i=1; i<n; i++) hash[i] = (hash[i-1] + pw[i] * (s[i] - 'a' + 1)) % mod;
            }
         
            ll getHash(int l, int r) { return (hash[r] - (l ? hash[l-1] : 0) + mod) % mod; }
         
            bool equal(int l1, int r1, int l2, int r2) {
                if(r1 - l1 != r2 - l2) return 0;
                if(l1 > l2) swap(l1, l2), swap(r1, r2);
                return (pw[l2-l1] * getHash(l1, r1) % mod) == getHash(l2, r2);
            }
        
            bool isPal(int l, int r) {
                return equal(l, r, n-1-r, n-1-l);
            }
        };
        

        Here you go.

        Also be aware that the function isPal only works if the to the string is appended its reverse.

        For example, let's say we will call isPal for the string s = "cabad".

        To chech is substring from "aba" is palindrome, you have to initialize the hashing structure with the string "cabaddabac"

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

Why such tight constraints on G2, $$$n \leq 2\cdot 10^5$$$ when the intended time complexity is $$$O(n\sqrt{n}log(n))$$$??

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

Hey,

  1. G2 is solvable in O(NlogNlogN): checking the maximum number of intervals one can separate in such that the common prefix has length at least k is doable in O(N/k logN) if we store the valid indices with Z[i]>=k in a set.

  2. Can someone understand why the solution to F gives TLE if we use unordered_map<int,vector > instead of map<int,vector >? My solution using unordered_map was TLEd in the main tests, and the official solution has the same issue when we use unordered_map. This seems unintuitive as we never use the key ordering.

Thanks!

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

now I get it

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

Is there any tutorial for that Z function used in G1?

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

Can anyone refer me more problems like E?

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

why do we usually use a segment tree for finding xor on segments and not just how it was described in the problem f? or we just do this only when elements don't change?

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

I think there's an error in the editorial for problem F. It says "we may find the largest t <= r" but it should be t < r since if t == r then the segment [t+1, r] would not make sense. I tried a solution with this change in mind and it got AC. 259369531

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

Anyone has an idea why this one gets TLE https://codeforces.me/contest/1968/submission/259216131 ? it is exactly the same complexity as the editorial solution.

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

On problem F i am not convinced with the solution , can anyone provide with a better approach here , because i see the statement that

Observation: any division on more than k>3 segments can be reduced to at most 3 segments. Doesn't hold the biequivalence , that if the segment can be divided into atmost 3 segments then it can be divided into k segments

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

problem E as per the tutorial: Interesting fact, that in such way we generate all possible Manhattan distances. Odd distances are generated between cells from the main diagonal and (n−1,n). Even distances are generated between cells from the main diagonal and (n,n)

but I guess if total x manhattan distances exist then we only get x-1 , how to prove we can get max x-1 distances only ?

if I am wrong please correct me

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

In problem F, what is wrong in the following logic: (Here a represents the prefix xor array and mp is mp[xor] = vector)

while(q--){
   int l,r; cin>>l>>r;
   if(a[r] == a[l-1]){
        cout<<"YES"<<endl;continue;
    }

    int x = *lower_bound(mp[a[r]].begin(), mp[a[r]].end(), l);
    int y = *lower_bound(mp[0].begin(), mp[0].end(), x+1);


    if(y<r && y>x && x>=l)cout<<"YES"<<endl; else cout<<"NO"<<endl;
   }
  • »
    »
    2 года назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится
    1. You need to look for the sum a[l - 1], not 0.

    2. The result of the second call to lower_bound() may not be dereferenceable.

    3. You don't have to check whether $$$y \gt x$$$ or $$$x\ge l$$$, since these things are automatically true.

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

Why is O(nlogn) approach exceeding time limit in B question? Normally O(nlogn) works with n<2x10^5 and time limit = 2 sec.

(i tried to write a solution using upper bound 259169919)

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

are there any problems similar to F

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

Can someone please explain to me the solution for problem D cause, I can't understand the editorial clearly

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

    Let me explain my idea:

    Let $$$f_s(p)$$$ be the total score of a player when he starts at the position $$$s$$$, always moves from his current position until he reaches $$$p$$$, and after reaching $$$p$$$, always stays at his current position. Bodya and Sasha's maximum score will be $$$\max_{1 \le i \le n}{f_{P_B}(i)}$$$ and $$$\max_{1 \le i \le n}{f_{P_S}(i)}$$$ respectively.

    This claim is correct because, a player will always move from his current position only when there lies some better position with higher $$$a_x$$$, somewhere in the future along the path of his $$$k$$$ moves. Now we will prove why it is always better for him to move now instead of staying at the current position for some turns and then move.

    If his current position is $$$x$$$, and he decides to stay at $$$x$$$ for some (one or more) turns, and then move to $$$p_x$$$, there are the following three cases:

    1. $$$a_x \lt a_{p_x}$$$: It is obviously better to move to $$$p_x$$$ immediately and not waste turns at $$$x$$$ with lower score.
    2. $$$a_x = a_{p_x}$$$: Staying and moving doesn't really differ here. So there is no loss in moving.
    3. $$$a_x \gt a_{p_x}$$$: When he moves after some turns, he moves definitely in the hope of reaching some position $$$y$$$ from $$$p_x$$$ (through one or several moves in between) where $$$a_x \lt a_y$$$. So it is always better to spend these turns at the position $$$y$$$ than wasting them on $$$x$$$.

    So the optimal move for a player will be — some number of moves (possibly zero), followed by some number of stays (possibly zero), until he completes his $$$k$$$ turns. So we basically iterate over his final position to know the maximum possible score.

    For all $$$1 \le i \le n$$$, $$$f_s(i)$$$ can be calculated in $$$O(n)$$$ by keeping track of how many turns it takes to reach $$$i$$$ starting at $$$s$$$ and the total score obtained along the path.

    So overall running time of the solution is $$$O(n)$$$.

    You can check my submission for better understanding: 259185280.

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

About the problem F , I have a question. According to the formula $$$X ^ X ^ X = X$$$ , why reduce k to three , instead of further reduce one? Could help me ?

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

I implemented the solution described by the editorial for G2 during contest but got TLE...

https://codeforces.me/contest/1968/submission/259238179

I am posting this so late because I was trying to upsolve with a smarter solution, but couldn't come up with one. It's pretty frustrating that my approach was correct but failed because of the constant factor.

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

for f what is the different between "--lower_bound(id[a[l-1]].begin(),id[a[l-1]].end(),r)" and "lower_bound(id[a[l-1]].begin(),id[a[l-1]].end(),r — 1)"

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

Good tutorial, thanks! But I want to ask that In problem F, why we should do mp[0].push_back(0) ?

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

For G2: I would be glad if any one help understand why author's O(n√n log(n) ) solution is passing where n=2e5 and time limit 3s.

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

Can somebody help me with Problem D submission ? i seem stuck here. 260790899

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

Can someone please tell what mistake am I doing in G1 ?

https://codeforces.me/contest/1968/submission/260861542

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

G2 卡常 厳しすぎ!! (Constant factor for G2 too tight!)

I implemented the solution described in the editorial, with KMP instead of Z-function, but all my submissions ended up TLE41 (261016696) and I couldn't get the log^2 solution right.

By dividing the submission into $$$ \gt \sqrt{n}$$$ and $$$\le\sqrt{n}$$$ parts I found my solution would finish in 3.2 seconds and I tried my best to squeeze in but still couldn't.

Could anyone help me optimize the constant factor further, or is KMP unable to pass?

BTW I don't think a $$$2e5$$$ data range for a $$$O(n\sqrt{n}\log n)$$$ intended solution is quite appropriate.

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

In G2 I submitted 2 solutions one wit time complexity O(N * sqrt(N) * log(n)) and other with O(N * sqrt(N * log(N))). The former solution passed whereas the later gave TLE. Does anyone have any idea why this is happening?

Former solution: https://codeforces.me/contest/1968/submission/261653053

Latter solution: https://codeforces.me/contest/1968/submission/261652536

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

G2 can be easily solved with z-function in O(nlogn^2). We find the maximal k for each answer i in increasing order. Use ordered set to maintain list of all indices in z-function that are greater than or equal to i and binary search for each next index. This allows us to binary search for each answer. Code

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

Can someone explain condition to move for D?

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

    First of all it is not possible to simulate all the moves since the constraints on number of moves is large . since each position in array 'A is associated with another position we need to move at max , min (n,k) times then for each move we can calculate the max score possible for the each player and after comparing maximum score achievable by both we can get the answer 359327583

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

..