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

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

Thanks everybody for participating in the round!

You are free to leave feedback in the comments as well!

What is your feedback about the contest?
Which problem did you like the most?
Which problem did you find least enjoyable?

A. Square?

Author: JahonaliX Preparation: khba

Solution
Code (Python)

B. Your name

Author: khba Preparation: khba

Solution
Code (C++)

C. Isamatdin and His Magic Wand!

Author: Isamatdin Preparation: Isamatdin

Solution
Code (C++)

D. Yet Another Array Problem

Author: Nasa Preparation: Muhammadali__ & Nasa

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

E. khba Loves to Sleep!

Author: Isamatdin Preparation: Isamatdin

Solution
Code (C++)

F. Tree, TREE!!!

Author: Nasa Preparation: Nasa

Hint 1
Hint 2
Hint 3
Solution
Code (C++)
Bonus
Hint for bonus 1
Solution for bonus
Code (C++) for bonus

G. Mukhammadali and the Smooth Array

Author: Muhammadali__ Preparation: Muhammadali__

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

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

Why did not you set $$$n \leq 2*10^5$$$ for task G?

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

What should Output for test case :

  • 1
  • 3 4 5
  • 2 4 3

for E. khba Loves to Sleep! My accepted code gives 0 1 2 3 but i think this should 0 1 2 5

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

AK the contest!, Thanks for this interesting contest

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

editorial for problem -G is pretty clever and simple!
thanks!

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

Figured a greedy approach for problem E choose the value with the maximum possible min distance from each $$$a_i$$$.

A simple proof to see it works is — say we have an optimal set $$$k$$$ which does not include point with the max distance say $$$k_m$$$, then we can simply replace any point in $$$k$$$ with $$$k_m$$$ and we still have an optimal set. This works for every possible stage.

Binary search gives the closest distance from a given position, we can use that as the cmp for a priority queue. Another greedy choice, rather than inserting every point in range [0,x], insert 0, x, and the midpoint(s) of $$$a_i$$$ and $$$a_{i-1}$$$ for $$$(1≤i≤n-1)$$$ (0-indexed) since one of those points have the max distance.

While we still have elements left to choose, pick the top priority say $$$c$$$, add to the result and then insert $$$c+1$$$ and $$$c-1$$$ as long as they are valid meaning in range [0,x] and not already chosen (my submission conveniently skipped this check, but still AC'd).

https://codeforces.me/contest/2167/submission/346322280

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

Why does the answer to E need to add n?

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

In the problem F, don't we have to consider the case when the number of nodes outside of node v + number of nodes in one of the subtree of node v >= k, then when the nodes in the remaining subtrees of v becomes a root, node v contributes to the answer, right? it is not covered in the editorial, or did I miss anything?

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

    After seeing your question, I had the same doubt. Later I realized that this answer is included in the calculation of v's child nodes. When sz[v] >= k, the contribution of v to the n-sz[v] nodes outside v's subtree is added. When n-sz[v] >= k, the contribution of f[v] to the sz[v] nodes within v's subtree is added. In other words, when traversing each v from 1 to n, the calculation includes v's contribution to nodes outside its subtree and f[v]'s contribution to nodes within its subtree, but the contribution of v itself is not accounted for in either v's calculation or its child nodes' calculations. This is why we need to add n to the ans when outputting the result.

    看到你的提问之后我也产生了相同的疑惑,后来我发现这种答案包含在v的子结点的计算中,sz[v]>=k时,将v对除v子树之外n-sz[v]个结点的贡献加入了,n-sz[v] >= k时,将f[v]对v子树中sz[v]个结点的贡献加入了,也就是说在从1到n遍历每一个v时,计算了v对v子树之外结点和f[v]对v子树中结点的贡献,而对v本身的贡献无论在v的计算中还是在v子结点的计算中都没有计入,这也是为什么要在输出时给ans加n

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

The prime enumeration in the solution for Problem D missed 47.

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

Fast and well-written editorial. F is such a good and basic problem of root-changing DP!

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

can anybody tell me why we are adding n to the answer in the problem F?

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

    That n accounts for making each of the nodes 1,2,...,n as the root. Since k is atmost n, the root r will be included (take one of the nodes as the root, and arbitrarily select rest of the k — 1 nodes).

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

    When sz[v] >= k, the contribution of v to the n-sz[v] nodes outside v's subtree is added. When n-sz[v] >= k, the contribution of f[v] to the sz[v] nodes within v's subtree is added. In other words, when traversing each v from 1 to n, the calculation includes v's contribution to nodes outside its subtree and f[v]'s contribution to nodes within its subtree, but the contribution of v itself is not accounted for in either v's calculation or its child nodes' calculations. This is why we need to add n to the ans when outputting the result.

    sz[v]>=k时,将v对除v子树之外n-sz[v]个结点的贡献加入了,n-sz[v] >= k时,将f[v]对v子树中sz[v]个结点的贡献加入了,也就是说在从1到n遍历每一个v时,计算了v对v子树之外结点和f[v]对v子树中结点的贡献,而对v本身的贡献无论在v的计算中还是在v子结点的计算中都没有计入,这也是为什么要在输出时给ans加n

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

What is F talking about? I read it once and once but havn't understand the explanation of test case yet.

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

Aside from the testing speed, everything else is good.

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

Hello can anyone help me regarding the problem G , i don't understand how this solution(editorial) guarantees correct ans is there any proof for eg what should be the ans for a={1,1,1,17,1,1,1},c={1,1,1,10000,1,1,1}

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

why problem A tagged "2-sat"?

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //this is the code

include <bits/stdc++.h>

using namespace std;

int main() { int t; cin >> t; while (t--) { int n, k; cin >> n; vector a(n); vector<pair<long long, int>> vpp(n); vector d(n);

for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    for (int i = 0; i < n; i++)
    {
        cin >> d[i];
    }

    long long sum = LLONG_MAX;
    long long check;
    long long e, t;

    for (int i = 0;i < n; i++)
    {
        check = 0;
        e = a[i];
        t = a[i];
        for (int j = i + 1; j < n; j++)
        {
            if (a[j] <e)
            {
                check = check + d[j];
            }
            if (a[j] > e)
            {
                e = a[j];
            }
        }
        for (int j = i - 1; j >= 0; j--)
        {
            if (a[j] >t)
            {
                check = check + d[j];
            }
            if (a[j] < t)
            {
                t = a[j];
            }
        }
        sum = min(sum, check);
    }

    cout << sum << endl;
}

} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Please tell me whats wrong in this solution for question G of the contest

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

F why ans+n ,i wonder why add n

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

I still can't understand the solution for problem E , help me coders.

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

    Instead of thinking about k first, think that for an assumed minimum distance m, is it possible to fit at least k teleports on the number line. If it is true, we search the greater search space for greater (more likeable) values of m, if not we go and choose the lesser search space.

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

In the editorial for F, It says when outside the subtree of i, if n- szi >= k, then we can choose any root in subtree of i and szi >= k hold, I agree with that. But what about the cases when n — szi <= k , if I choose root as someone in its subtree and after that the size of the subtree of i becomes >= k ? Why are we not taking that case

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

    "if I choose root as someone in its subtree and after that the size of the subtree of $$$i$$$ becomes $$$ \gt = k$$$" it will not, if I understood your problem corrently. I'm sorry if I misunderstood.

    Think about it. How will it change? If you root differently from current rooting by choosing a node from a certain path's, the previous root's subtree will be the other unexplored/branched-out portion. In other words, if a tree starts like

    .....1

    /....|....\

    2....3.....4

    then, any new root chosen from 2's branch will have a fixed size subtree in node 1, and that size is always sz(3) + sz(4) + 1 (the node 1 itself).

    Hope it helps. Again, not sure if this is what you were thinking about, but I tried.

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

      yes exactly so what if 1 + sz(3) + sz(4) becomes >= k, but the solution will does not account for this condition, if only says n — sz(1) >= k

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

        We actually need to check n - sz(2) >= k if we are talking about my example. It means that for node 1, if node count except 2's path (thus n - sz(2)) is bigger than k, then all nodes in that subtree will have current node (node 1) as their answer if we make them root. Repeat that for all the paths, and repeat all of it for all current nodes.

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

Problem F seems to be intuitively easier compared to problem G (because I didn't even think about reversing the DP as to finding max instead of plugging directly from the problem where I have to find min) but why is it the case that there seems to be much more people solving problem G instead of F?

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

G is easy with n <= 8000. imo n <= 1e5 should have been a better constraint, and more fitting for this position.

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

In problem E, we can do a different logic in the binary search.

It’s related to the union of the forbidden segments.

For each i, the forbidden segment is [a[i]−dif+1,a[i]+dif−1].

The union will be: for each left endpoint we take the maximum right endpoint, and then we create an array that doesn’t contain intersections between segments.

Then, we have forbidden segments [x1, x2], [x3, x4], [x5, x6],.. we take the teleports from the segments [x2+1,x3−1],[x4+1,x5−1]..

This is my submission 350253562

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

In the editorial of problem F, I think it should be better to state: if $$$n - sz_i \ge k$$$ holds, then $$$sz_{p_i} \ge k$$$ for roots in the subtree of $$$i$$$. (here $$$p_i$$$ is $$$i$$$'s parent in the rooted tree.)

i.e when $$$n - sz_i \lt k$$$ we might be missing contributions of $$$i$$$ but we are not missing contributions of $$$p_i$$$. So in the algorithm we are actually adding the parent's contributions.

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

For question G why this Top down approach fails ...irrespective of TLE or MLE...this fails because it gives wrong answer but how what is the flaw in this solution-->

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll compute(vector<ll> &dp, vector<int> &values, vector<int> &costs, int index, int pre_val)
{
    if (index < 0)
        return 0;
    if (values[index] == pre_val)
    {
        // we must incude it
        if (dp[index] != -1)
        {
            return dp[index];
        }
        else
        {
            return dp[index] = costs[index] + compute(dp, values, costs, index - 1, values[index]);
        }
    }
    if (values[index] < pre_val)
    {
        // wehave option to take it or leave it
        if (dp[index] != -1)
        {
            return max(dp[index], compute(dp, values, costs, index - 1, pre_val));
        }
        else
        {
            dp[index] = costs[index] + compute(dp, values, costs, index - 1, values[index]);
            return max(dp[index], compute(dp, values, costs, index - 1, pre_val));
        }
    }
    if (values[index] > pre_val)
    {

        return compute(dp, values, costs, index - 1, pre_val);
    }
}
int main()
{
    int t, n;
    cin >> t;
    while (t--)
    {

        cin >> n;
        vector<int> values;
        vector<int> costs;
        int a;
        ll max_keep{};
        for (int i = 0; i < n; i++)
        {
            cin >> a;
            values.push_back(a);
        }
        for (int i = 0; i < n; i++)
        {
            cin >> a;
            max_keep += a;
            costs.push_back(a);
        }
        vector<ll> dp(n, -1);
        max_keep -= compute(dp, values, costs, n - 1, 8000);
        cout << max_keep << endl;
    }

    return 0;
}

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

hi. how am i supposed to know if 53 will be the number upto which the product of primes will exceed 10^18? is this a regular pattern?.(ps. sorry if the question seems dumb, im a newbie)

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

In question D what must be the output for the array 6, 18, 30. I think it should be 4. But the according to solution it is 5. How? I think there is a glitch.

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

    $$$gcd(4,30)=2$$$

    $$$gcd(4,18)=2$$$

    $$$gcd(4,6)=2$$$

    Hope this helps!

    Additionally, the answer cannot be composite, as for any composite number, there exists a prime that is a better choice.

  • »
    »
    9 месяцев назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится
    1. question requires that atleast for one number in the array for which gcd(x, num) should be 1. [6, 18, 30] and x = 4 does not satisfy this.

    2. if there is atleast one odd number in the array, answer must be 2 as gcd(2, odd) = 1

    3. if every number in the array is even, then we must find the smallest odd number (to have gcd as 1). for every composite odd number, there is always a smaller prime number which also gives gcd as 1. so it comes down to finding the smallest prime number.

    4. how many prime numbers to check for ? primes number < 100 are enough because

    • if the number is missing a prime (<100), then that is the answer.
    • but if it is not missing any prime (2 * 3 * 5 .. 97), then number is way greater than 10^18, which is not possible because of the constraint.
    • till 53 is enough to check but instead of calculating the above value, we can look at numbers which contain 10, and there are more than 18 numbers in primes (<100) which have 10 in them, so multiplying them would give > 10^18.