Codeforces и Polygon могут быть недоступны в период с 6 декабря, 22:00 (МСК) по 7 декабря, 00:00 (МСК) в связи с проведением технических работ. ×

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

Автор BledDest, 28 часов назад, перевод, По-русски

2042A - Жадный Монокарп

Идея: BledDest

Разбор
Решение (Neon)

2042B - Игра с цветными шариками

Идея: BledDest

Разбор
Решение (BledDest)

2042C - Спортивная рыбалка

Идея: BledDest

Разбор
Решение (Neon)

2042D - Рекомендации

Идея: adedalic

Разбор
Решение (adedalic)

2042E - Пары вершин

Идея: BledDest

Разбор
Решение (awoo)

2042F - Два подотрезка

Идея: BledDest

Разбор
Решение (Neon)
  • Проголосовать: нравится
  • +46
  • Проголосовать: не нравится

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

did anybody solved problem C using greedy or binary search only if yes then please provide the code..

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

    .

  • »
    »
    4 часа назад, # ^ |
      Проголосовать: нравится +11 Проголосовать: не нравится

    why is that you only search for those 2 solutions? are you scared of learning a new thing, and a new approach to problems? accept that some problems are weird and require unique solutions. this question cant be binary searched because m+1 does not always yield a better answer than m, and m does not always yield a better answer than m+1. also it cant be solved greedily as you have to consider all suffix differences. stop forcing algorithms just because you're lazy to learn more of them

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

      Thanks for the reply, I coded a greedy solution for the problem but was unsure that i made all observations correctly , just wanted to make sure where i was lacking on greedy solution, but trust me even though I am not good at making mathematical observation yet I enjoy them a lot.. Since you are an expert can you please have a look at my profile and tell where I am going wrong ,I have solved over 400 problems but Still didnt made to pupil even though i am practising questions tougher than my level? I would be grateful to you!

»
24 часа назад, # |
  Проголосовать: нравится -19 Проголосовать: не нравится

Look at what this idiot did.

Instead of using set upper_bound and lower_bound functions, this guy wrote a whole specialized segment tree. https://codeforces.me/contest/2042/submission/294446705 And it PASSED with only a single penalty.

Guess this is what happens when you solve too many range query problems.

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

    what's the issue, the segtree most likely does have smaller constants, it's not a totally unreasonable decision if you know I think (also idk who asked)

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

      that is me. I am the idiot.

      I wrote the segtree because I forgot that lower_bound and upper_bound exist.

      Solved too many segtree problems on cses.

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

.

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

In problem C tutorial, shouldn't it be $$$0⋅s_{a_1}+(1-0)⋅s_{a_2}+(2-1)⋅s_{a_3}+⋯+s_{a_m}$$$ the resulting sum?

  • »
    »
    18 часов назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    I'd like to add that..

    notice you only pick indexes to start a new group where str[i]=='1' and that group can go from i to the end or you can fraction that group into small groups, but for every index you pick to start a new group, the resultant score (diff bob and alice from i to end) or s[i] is positive cuz even if you divide a group into small groups at index i, you will keep the score of s[i]. Example:

    $$$n=9, k = 5$$$

    011100101

    Array $$$s$$$ will be $$${_, 2, 1, 0, -1, 0, 1, 0, 1}$$$, but you are only interested on making groups at indexes where s[i] have positive values. Now you can make a group at index where $$$s[i] = 2$$$ cuz it's the greatest in $$$s$$$. so you make a group between $$$i-1$$$ and $$$i$$$ where $$$i = 1$$$ and we get two points of score, but even if you don't keep the last group you made $$$[i:n)$$$, you'll still keep those two points: now you have groups [0:0] and [1:9). As we still have values on $$$s$$$, let's take the next greatest. Now you have groups [0:0], [1:1], [2:9), cuz in group [2:9) you have +1 of score, and you are still keeping the previous +2 points, even if you divided the [1:9) group.

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

    For problem C, can't we use a prefix array to achieve the same result? Let aj denotes where the jth segment ends.

    Then, the sum can be expressed as: 0*pa1 + 1*(pa2 — pa1) + 2*(pa3 — pa2) + .... + (m-1)*(pam — pam-1)

    Simplifying, this becomes: -(pa1 + pa2 + ... + pam-1) + (m-1)*pam However, I am unable to eliminate the last m in the equation above. Can someone help?

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

Waiting for alternative way of problem D solution (Not using trees... ordered set in C++ or SortedList in python).

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

Could someone explain the solution of problem C, or someone give a different approach to problem C

»
14 часов назад, # |
  Проголосовать: нравится -6 Проголосовать: не нравится
»
12 часов назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For Problem-C, you should use int64.

»
11 часов назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

Problem E can be solved in $$$\Theta(n)$$$ without requiring an $$$O(1)$$$ LCA computation. Instead, we precompute two properties for each subtree: (1) whether it contains all $$$n$$$ distinct values, and (2) whether it contains duplicate values. Using these properties, we can decide whether to select a vertex directly in order.

To facilitate the precomputation, we use the DFS order to represent each subtree as a range and then translate the constraints for a subtree into constraints for its corresponding range. Both properties can be checked in $$$\Theta(n)$$$ using a simple two-pointer technique on the range.

294738728

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

i tried c using binary search, but couldn't figure out the mistake.. :(

https://shorturl.at/043Sb

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

For problem C, can't we use a prefix array to achieve the same result? Let aj denotes where the jth segment ends.

Then, the sum can be expressed as: 0*pa1 + 1*(pa2 — pa1) + 2*(pa3 — pa2) + .... + (m-1)*(pam — pam-1)

Simplifying, this becomes: -(pa1 + pa2 + ... + pam-1) + (m-1)*pam However, I am unable to eliminate the last m in the equation above. Can someone help?

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

For Problem D: What is the reason of the tle ?

void solve(){
    int n;
    cin>>n;

    vector<array<int, 2>> v(n);
    for(auto &curr: v)
        for(auto &it: curr)
            cin>>it;

    vector<array<int, 3>> a(n);
    map<array<int, 2>, int> m;
    for(int i=0; i<n; ++i)
        a[i][0] = v[i][0], a[i][1] = v[i][1], a[i][2] = i, ++m[v[i]];

    vector<int> ans(n);
    sort(a.begin(), a.end(), [&](const array<int, 3> &a1, const array<int, 3> &a2){
        if(a1[0] < a2[0]) return true;
        if(a1[0] > a2[0]) return false;
        return a1[1] >= a2[1];
    });

    set<int> s;
    s.insert(a[0][1]);
    for(int i=1; i<n; ++i){
        int currS = a[i][0], currE = a[i][1], idx = a[i][2];

        auto it = s.lower_bound(currE);
        if(it == s.end()){
            s.insert(currE);
            continue;
        }

        ans[idx] += (*it - currE);
        s.insert(currE);
    }

    sort(a.begin(), a.end(), [&](const array<int, 3> &a1, const array<int, 3> &a2){
        if(a1[1] < a2[1]) return false;
        if(a1[1] > a2[1]) return true;
        return a1[0] <= a2[0];
    });

    s.clear();
    s.insert(a[0][0]);
    for(int i=1; i<n; ++i){
        int currS = a[i][0], currE = a[i][1], idx = a[i][2];

        auto it = s.upper_bound(currS);
        if(it == s.begin()){
            s.insert(currS);
            continue;
        }
        --it;

        ans[idx] += (currS - *it);
        s.insert(currS);
    }

    for(int i=0; i<n; ++i)
        if(m[v[i]] > 1)
            ans[i] = 0;

    for(int i=0; i<n; ++i)
        cout<<ans[i]<<"\n";
}