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

Автор cry, 22 месяца назад, По-английски

1998A - Find K Distinct Points with Fixed Center

Problem Credits: sum
Analysis: cry

Solution
Code (C++)

1998B - Minimize Equal Sum Subarrays

Problem Credits: satyam343
Analysis: cry

Solution
Code (C++)

1998C - Perform Operations to Maximize Score

Problem Credits: cry, satyam343
Analysis: sum, satyam343, Dominater069

Hint 1
Hint 2
Hint 3
Solution
Code (C++)

1998D - Determine Winning Islands in Race

Problem Credits: cry
Analysis: cry

Solution
Code (C++)

1998E1 - Eliminating Balls With Merging (Easy Version)

Problem Credits: cry, sum, satyam343
Analysis: sum

Solution 1
Code 1 (C++)
Solution 2
Code 2 (C++)

1998E2 - Eliminating Balls With Merging (Hard Version)

Problem Credits: cry, sum, satyam343
Analysis: sum

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

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

first, I feel like B is a problem you can really get stuck on if you don't guess it

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

Bonus: E2 can be solved in $$$O(n)$$$ time. 275655689

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

Can someone estimate an upper bound on no of states in these solutions for E1 and E2? Or hack otherwise? 275608667 275619707

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

Thanks for making me break the record of my worst rank ever:)

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

The two codes of problem E1 are same,cry please fix it,thx.

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

B can be solved using one cyclic shift

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

For D I have a better solution which works in O(n + m).

vector<int> adj[N], radj[N];
 
void solve() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) adj[i].clear(), radj[i].clear();
    for (int i = 1; i < n; i++) adj[i].push_back(i + 1), radj[i + 1].push_back(i);
    for (int i = 1; i <= m; i++) {
        cin >> u >> v;
        if (u > v) swap(u, v);
        adj[u].push_back(v);
        radj[v].push_back(u);
    }
    vector<int> mpjt(n + 1);
    int mj = 0;
 
    for (int s = 0; s < n - 1; s++) {
        for (int z : radj[s]) mpjt[s] = max(mpjt[s], mpjt[z] + s - z - 1);
        for (int z : adj[s]) mj = max(mj, mpjt[s] + z - s - 1);
        if (mj > s) cout << 0;
        else cout << 1;
    }
    // cout << mpjt;
}

275623093

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

    what does the vector mpjt contain?

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

      I have seen the race as the cow Bessie having a head start of s units, so to beat Bessie we must be able to get ahead of Bessie using the edges which originate upto point s — 1. mjpt vector is used to store the maximum "Jump" we can get when we land at any x. "Jump" for each move Elsie takes is equal to (lenght of jump — 1) (as we take 1 unit time to jump which Bessie also gets so in short we gained a distance of Lenght — 1), mj variable stores the maximum possible "Jump" (or distance gain we can get using edges originating upto s — 1). If we can get a net jump of more than s, we Elsie wins else Bessie wins.

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

C you can do O(nlogn) time instead of O(nlogMAX) binary search time by using two pointers, faster than binary search. First, only two strategies can be optimal; increase the maximum of the array a as much as possible, or increase the median of the array a as much as possible. The editorial goes into the reasoning behind this in more detail.

First strategy is easy to compute by just finding the biggest array element where the corresponding b_i = 1, then increasing it by k.

Second strategy, first sort the array, then use two pointers where right pointer is pointing to the right of the median, and the left pointer is pointing to the first i to the left of (or equal to) the median where b_i = 1.

Clearly the right pointer forms a "wall" that we cannot overcome until we increment the median to its value, and if this "wall" is not incrementable (b_i = 0), you can only get past it by incrementing values to the left of the median, so you can recruit extra help from the left pointer to shift the wall over to the left. At each iteration we keep track of the "width"/frequency of the median (because we will have to increment a lot of elements at once to increase the median sometimes), and we increment the median to the value of the "wall" (right pointer), then move the wall to the right one element, and keep on iterating until we either reach the end of the array (just increment the current median as much as possible) or we run out of operations.

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

I see that a lot of you have got FST on A by fixing the first $$$k-1$$$ points in some way and balancing with the last one, my sincere condolences. However I also have to tell you that you could immediately upgrade that to an (almost) unhackable solution.

Instead of choosing the first $$$k-1$$$ deterministically, choose them randomly in the range of $$$[-R,R]\times [-R,R]$$$. $$$R$$$ should not matter as long as there is no overflow, though the collision probability should be $$$O(k/R)$$$ if my proofs are correct. Now choose the last point as the trivial one that makes the center same as the given one.

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

This blog was created 7weeks ago?????

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

Attached code for C doesn't pass samples?

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

Editorial code for Problem C doesn't work for this valid test case

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

You can also do E2 using the same DnC approach in the solution 1 to E1

my sub

it boils down to finding dpl and dpr, where dpl is the point starting which a ball can consume all other blobs and it only works till dpr. To calculate dpl, you need to find the leftmost index upto which the ball must consume other ones such that you can consume the left blockade(so dpl must be atleast this index). Also, if you can consume the right blockade, you can set dpl to dpl of the right blockade

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

In C: " Observe that it is optimal to only consider removing the largest element with bi=0", did you mean guess?

Please give proofs...

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

    I also was not able to observe that and solved a harder problem, used prefix sum and ordered multiset. 275621322

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

    Denote $$$a_m$$$ as the max element, $$$a_o$$$ to be the median of array after removing $$$a_m$$$ and $$$a_i$$$ to be the alternative element we want to consider to remove instead of $$$a_m$$$ for a better solution. If one of them is changeable then the case becomes choose the max changeable element and put all increments there. So we only consider all of them to be unchangeable below:

    if $$$a_i \gt a_o$$$, then swapping the choice won't impact the median but we pay $$$a_m-a_i$$$ for the change

    if $$$a_i=a_o$$$, then swapping the choice would increase the median by at most $$$a_m-a_i$$$, we pay $$$a_m-a_i$$$ for the swap as well, so the result won't be better

    if $$$a_i \lt a_o$$$, the only hope for getting a better result is after swapping, $$$a_m$$$ replaced $$$a_o$$$ to be the median, and it must increase more than $$$a_m-a_i$$$ we pay. The increase would be $$$a_m-a_o$$$ and we know $$$a_i \lt a_o$$$, so $$$a_m-a_o \lt a_m-a_i$$$, so the result cannot be better as well

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

    totally changed the editorial, i hope its fine now :)

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

      Thus, we are in 2 cases now, either increase max, or increase median of the rest. We will solve the problem considering both cases separately.

      As mentioned in this editorial We need to handle the cases separately .

      Whyyy Separately I mean Why there is no optimality if we are trying to maximize and last element together

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

        Claim 2 : Either we will use all k operations on the element which eventually becomes max element in our array, or we will use all operations trying to improve med(cn) and keep max element constant.

        this means that either max remains constant or median of the others remains constant.....which are 2 separate cases

        when max remains constant, we want to maximise median of others and vice versa

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

C is much harder than D to me, spent twice as much time solving it

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

Observe that it is optimal to only consider removing the largest element with bi=0 (in fact if the last element has bn=1 then we don't even need to consider it given that increasing an is the same or better than increasing the median). This leads to an O(nlogmaxai) solution.

any proof for this ?

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

Hello, for E1, I have another solution,

we know the brute force solution is for every index i, check if it can remain till the end, we can do this by simple greedy, this becomes O(n^2)

Now, there are 2 observations :-

1) suppose, for an index i, we can use it to remove i-1, and i-1 can potentially stay till the end, then i can also stay till the end, the same is true for i+1

2) suppose, we scan the array in both directions for a certain index i, and we found out that it cannot potentially stay till the end, then, all the indexes that i can remove, will also not stay till the end

ik that the explanation is not that good, but idk how to explain this more precisely, so forgive me for that

my code : https://mirror.codeforces.com/contest/1998/submission/275623982

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

nice contest. it'd be great if authors add hints before solutions in their editorials

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

For A , why can't we have coordinates are (-1, -1), ... (-k/2, -k/2), (1,1), .... (k/2, k/2) and (0, 0)(if k is even) and (xc*k, yc*k) ? It was failing on pretest2.

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

"(In fact we can observe that we only have to consider the rightmost one, though this observation is not necessary.)"

It isn't clear that you mean the rightmost in the sorted array.

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

In C, how is the answer for this testcase 29
1
6 2
3 11 12 14 14 15
1 0 0 1 0 0
UPD: Sorry, saw the wrong testcase

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

Thanks for making me break the record of my worst rank ever:)

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

I have a confusing way to solve E1. I could even not calculate its time complexity.

275618236

Could anyone help me to prove its correctness or hack it?

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

How to even analyze in B ?I mean I kept thinking about half an hour without reaching any conclusion.

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

satyam343, Dominater069, sum In problem C editorial it is mentioned that when applying all k operations on a_n after sorting and taking median of the remaining n — 1 elements the ans will be maximum as compared to other cases when taking a_i where 1 <= i <= n — 1.

Then do we need this code for checking all scenarios? Instead why can't we just find value for a_n + med(remaining n — 1 elements)?

Let me know if I am misunderstanding something.


// case 1 : increment max for (int i = 0; i < n; i++) if (a[i].second == 1){ // find med(c_i) int mc; if (i < n / 2) mc = a[n / 2].first; else mc = a[(n &mdash; 2) / 2].first; ans = max(ans, 0LL + a[i].first + k + mc); }
»
20 месяцев назад, скрыть # |
Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

I was having confusion that why I am choosing max element when bi = 0, since we have two option for max_element like a_lmax or a_rmax for array { a_lmax, a_med1, a_med2, a_rmax} --> choosing a_lmax will give greater median than a_rmax

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

I misread the problem C, instead of calculating $$$max(a_i + median(c_i))$$$, I thought that you need to calculate $$$sum(a_i + median(c_i))$$$ (so you need to only calculate $$$sum(c_i)$$$), any ideas how to solve it?

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

I had the goofiest, most overcomplicated solution to C.

275828049

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

for the second solution of E1

what if a[l] = a[r] = false

and with the new sum : x = P[r — 1] — P[l] && x >= next greater && prev greater

cant this be the maximum ?" ( after adding x + a[l] we will have a new maxi we didnt check before ) that can leads to possible answer ?

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

Problem B was so easy, can't believe I didn't get it T_T

Just solved A, got -71 delta

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

Hmm, it seems that problem E1 has a randomized solution as well. Let's find $$$l_i$$$, $$$r_i$$$ for each $$$i$$$ where $$$[l_i, r_i]$$$ is the maximal segment we can merge without discarding ball $$$i$$$. Then it is clear that if ball $$$i$$$ can subsume ball $$$j$$$ then $$$l_i \le l_j \le r_j \le r_i$$$ holds.

Now simply run the trivial algorithm for all $$$i$$$ in random order: try to add the elements from both sides to the current segment. When adding a new element in such a way, immediately apply the optimization from above ($$$l_i = \min(l_i, l_j)$$$, $$$r_i = \max(r_i, r_j)$$$).

Now, how many times would we add an element $$$j$$$ for different $$$i$$$ (e.g. to the right side of the segment). Suppose it happened for $$$i_1, \dots, i_k$$$ (in order). Then it's clear that $$$i_1 \lt \dots \lt i_k$$$ (otherwise we would jump over $$$j$$$ thanks to the optimization). But the length of the longest increasing sequence is expected to be $$$O(n^{1/2})$$$ (actually it shouldn't be much harder to derandomize such a solution).

276336073

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

I think in problem D we can add edge instead of erase it.Below is my submission,time complexity is O(n) 276931199

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

预防B题诈骗,人人有责

Preventing fraud in question B is everyone's responsibility

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

E2 is a nice problem,double experience https://www.luogu.com.cn/problem/P9530

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

In C this my idea was that basically, all the elements from median of the original array to the max element of the original array would have a chance for affecting the maximum value, and further I thought of two cases where if A’s B[i]==1. then we would simply add k to A[i] and use the original median of the n-1 elements, else if A’s B[i]==0 then we would have to find the rightmost idx to the left of the median where B[i]==1 then we can check if adding k to it affects the median, if it does then the new median would be this A[i]+k, and then we can add it to the A[i] from right and if no such index is found then we can just add A[i]+old median , but what am I missing here. Link to solution: https://pastebin.com/ux1dK3fw