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

Автор okwedook, 3 года назад, По-русски

1486A - Сдвигая стопки

Подсказка1
Подсказка2
Решение

Решение на C++: 107892022
Решение на Python: 107892053

1486B - Восточная выставка

Подсказка1
Подсказка2
Решение

Решение на C++: 107892065
Решение на Python: 107892085

1486C1 - Найти наибольшее (простая версия)

Подсказка1
Подсказка2
Решение

Решение на C++: 107892097
Решение на Python: 107892140

1486C2 - Найти наибольшее (сложная версия)

Спасибо Aleks5d за предложение решения данной подзадачи.

Подсказка1
Подсказка2
Подсказка3
Решение

Решение на C++: 107892122
Решение на Python: 107892144

1486D - Максимальная медиана

Подсказка1
Подсказка2
Подсказка3
Решение

Решение на C++: 107892153
Решение на Python: 107892163

1486E - Парный платёж

Подсказка1
Подсказка2
Подсказка3
Решение

Решение на C++: 107892178

1486F - Пары путей

Подсказка1
Подсказка2
Подсказка3
Решение

Решение на C++: 107892186

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

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

I'll add solution codes as soon as I can. If there are any mistakes, you can PM me.

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

C was nice

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

Amazing problems and solutions!!!!

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

can someone plz explain this to me for question B: Now to calculate the answer on a line we could use a known fact: point with the smallest summary distance is between left and right median.

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

    think about 4 points : (0,0) (0,1) (0,2) and (0,100). If you are fixing the median between (0,2) and (0,100) the first 3 point will have to move to the right(0,x+1) and that's bad for the solution.

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

    Just imagine: there are n points in line. You are on the leftmost point and walk to the rightmost point. When you begin to walk, the sum of all distance will get smaller until you arrive at the median —— or 2 medians (if n is even, there are 2 medians and you will get a same summary between them)

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

    It is because you want to minimize $$$C = |x-x_1| + .. + |x-x_n|$$$ where $$$x_1\leq x_2\leq ... \leq x_n$$$. You have $$$|x -x_i|+|x-x_{n-i+1}|\geq x_{n-i+1}-x_i$$$; and "=" happens when $$$x_i\leq x\leq x_{n-i+1}$$$. To make it valid for all $$$i$$$, in the case $$$n$$$ is odd, $$$x$$$ must take the value of $$$x_{n/2}$$$, otherwise, $$$x$$$ can take any value from $$$x_{n/2}$$$ to $$$x_{n/2+1}$$$ (the two medians).

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

    Another way to think of it: for any two given points, choosing any point along the line connecting them contributes the same summary distance, i.e. the distance between them. So taking all the points as a whole, you can ignore the left-most and right-most point. You now have a smaller set, and you can keep doing this, until you either have 2 points left (if initial set had an even number of points) or 1 point left. So if you have 2 points left, any point between them will contribute the minimum (the distance between them), as going outside of those 2 points will contribute more than the distance between them. And obviously if you only have 1 point left, that point will contribute 0, and anything else would contribute more.

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

Is there someone who tried to solve E as a dp[N][2] — minimum distance to reach a node where state 0 means using the second road now and and 1 for the using the first one? I WA o pretest 4,if you can give me your solution. My CODE

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

    I solved it using a similar approach. Here is my code. Hope it helps : )

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

      it's not, but thank you anyway :). I found my mistake. I didn't think about the case when we should use the same edge twice in a row like in this example :

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

Systests for E seem to be weak, many $$$O(n^2)$$$ solutions including mine (107846073) passed with it.

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

Some video solutions for A-E, and somehow not FST'ing E

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

Really liked problem c and great problem set!!

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

weak pretests in A, didn't include overflow case

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

Thanks for the fast editorials!

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

C was nice but it would have been better if on exceeding query limit it threw some error other than WA. I thought my code was incorrect but it was exceeding query limit.

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

    What else should it error as?

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

      Something like Query Limit Exceeded.

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

        In interective problems u can use assertion on number of queries to make sure whether it is wrong ans or query limit exceeded

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

          Could you please tell me something about how to use assertion to konw whether or not my submission is query limit exceeded.

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

        IMO this gives too much information, like in constructive problems if you print the wrong length you don't get a custom result.

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

        Maybe add if(totalQueriesMade>40) sleep(5) and purposely get TLE (assuming your code won't TLE otherwise). Hack-ish, admittedly.

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

WA ON TC2 in prpb A . Can anyone give me whats the mistake.

Spoiler

Thanks

UPDATE: ITS SORTED OUT NOW

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

Why is my solution not correct for A ? I can't think of a case where it may fail;-;...pls someone check 107870406 i wasted 2 hrs on this piece of sheet;-;

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

    You need to read the entire line, even if you find sum < 0.

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

    You break possibly before reading the input. I suggest read all the input before solving the testcase.

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

    ur code fails on 3 3 0 0 1 3 0 1 0 3 1 0 0 answer is NO , NO , NO I guess your code guess a different answer. Refer https://codeforces.com/problemset/submission/1486/107870392 the function solve2

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

      sorry I stopped taking input when i got the answer so it messed up further inputs...

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

    Hi,the logic is no8t making any sense to me. Consider this sequence 0 1 2 2 8 9 20. Do you think your solution will give correct output? Even though h-i is negative at i=3, still the sum becomes positive. But do you think in this case a solution is possible?

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

      the logic is correct, I am checking for every input...I broke when I found the ans...didnt take all the inputs, thats why it gave wrong ans

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

    I have the same mistake as you and wa on test2. Fortunately, I solved it.

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

okwedook Too many fake solution for E, suggest rejudge all submissions .

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

Just amazing contest,thanks to the autor! Waiting soon for ur next round.

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

I tried to solve D using ordered set, but I got WA on test case 7. I cant figure out why MY CODE

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

    you get the max median of subarray of length exactly k not at least k

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

      can you explain why length k is not optimum, i think length k should be optimum

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

        n = 3 k = 2

        5 2 5

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

        With array $$$[2, 1, 2]$$$ and $$$k = 2$$$, the only possible median for subarrays of length $$$2$$$ is $$$1$$$, but the best possible median is $$$2$$$ attained when we consider the entire array.

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

Can anyone please tell me why I got an fst on problem C ? WA_CODE

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

Such a good problem set, make me realize that I need to work hard. And that interactive questions, nice.

I will be thankful if someone recommends good and easy interactive questions, for beginners. Thanks in advance.

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

C was a great question, I'm still confused though on my find_left function.

at first, my mid calculations were the standard (lo + hi) / 2, but this causes infinity loop:

ll mid = lo + (hi - lo) / 2;

then I changed it to (lo + hi + 1) / 2, then got AC:

ll mid = lo + (hi - lo + 1) / 2;

full code:

ll find_left(ll secondMaxIdx)
{
	ll lo = 1, hi = secondMaxIdx - 1;
	while (lo < hi)
	{
		ll mid = lo + (hi - lo) / 2;
		if (ask(mid, secondMaxIdx) == secondMaxIdx)
		{
			lo = mid;
		}
		else
		{
			hi = mid - 1;
		}
	}
	return lo;
}

Can anyone with a better understanding of binary search explain why this works?

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

I solved E in another way(107874274). I'm not sure whether it is correct or not.

The naive solution is to use Dijkstra's algorithm and two loops to relax. Consider every vertex $$$u$$$. If it is used as middle vertex by some vertex $$$a$$$ and it is going to be used by vertex $$$b$$$.

According to Dijkstra's algorithm, ans[a] <= ans[b] must hold. If dis[a][u] <= dis[b][u], no vertex will be relaxed by $$$b$$$. So for every vertex, I record the minimum dis[a][u]. If it is equal or smaller than dis[b][u] then continue. Otherwise I just run the naive two loops.

Since every vertex will be used as middle vertex at most $$$maxw$$$ times, each relaxtion will call one pq.push. The time complexity may be $$$\mathcal{O}((N+M)logM + maxw\cdot (N+M)logM)$$$(I'm not sure about this)

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

    Can't we solve this for general weight. Means we can construct a new graph in which there is an edge between u and v iff there is exactly one vertex z between u and v in original graph s.t. u-z and v-z are edges in the original graph and weight of uv in new graph will be as defined in the problem. Now simply we should run Dijkstra on this new graph.
    What is wrong with this method? Can you please point it out.

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

E

"For a middle vertex we only...". what is a "middle vertex"?

"is the weight of the last edge...". What is the "last edge", last of what?

"Now for each starting edge..." What is "starting edge" here?

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

Is there anyone else who thanks Errichto whenever they see a binary search problem?

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

Can someone help me figure out where I am going wrong?

#include "bits/stdc++.h"
#define ll long long int
using namespace std;

const int mod = 1e9 + 7, mxN = 2e5 + 10;

int solve(int l, int r, int ind = -1)
{
  if(l >= r)
    return l;
  

  if(ind == -1)
  {
    cout << "? " << l << " " << r << endl;
    cin >> ind;
  }

  int mid = l + (r-l)/2;

  
  if(ind > mid)
  {
    int temp;
    cout << "? " << mid << " " << r << endl;
    cin >> temp;
    if(ind == temp)
      return solve(mid , r, temp);
    else
      return solve(l, mid - 1);
  }
  else
  {
    if(mid == l)
      return r;
    int temp;
    cout << "? " << l << " " << mid << endl;
    cin >> temp;
    if(ind == temp)
      return solve(l, mid, temp);
    else
      return solve(mid + 1, r);
  }
}

int main()
{
  ios::sync_with_stdio(0);
  cin.tie(NULL);

  int tt;
  // cin >> tt;
  tt = 1;
  for (int pp = 0; pp < tt; pp++)
  {
    int n;
    cin >> n;

    int ans = solve(1, n);

    cout << "! " << ans << endl;
  }

  return 0;
}

Thanks in advance!

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

    To debug such scenarios, you can stress test your solution as well.

    You can write a bruteforce for this problem very easily. e.g.

    1. Generate random permutation A.
    2. Take your query into a separate function. Answer it by using the array A. You can find second max in O(n) easily.
    3. In the end, match the maximum element position returned by your program with actual maximum element position.

    Keep doing this for many iterations till you find a counter case. This kind of stress testing strategy helps a lot in many problems. You can see my submission for some hints regarding implementing this bruteforce. The code is really messy and unpolished, but I have attached it for reference anyway.

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

okwedook test cases for E are weak. Many $$$O(N^2)$$$ have been passed. So please consider rejudging all submissions.

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

    Unfortunately we won't rejudge all the submissions Uphacks are already added to tests, so from this point all the solutions will be judged with updated tests Sorry for the inconvenience

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

I thought it has time complexity O(log n) but it's O(n).

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

    It seems that every interval like $$$[1,2],[3,4],\cdots,[n-1,n]$$$ will cost 1 query and the number of those intervals is $$$\Omega(n)$$$. In addition, there are other intervals that will cost 1 query.

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

How to solve D by two pointers?

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

I am getting runtime error on testcase 6 for the E problem , here is the link of my submission , can some one please check it!!, What I have done is kept n*51 states and edge of weight 0 between (u,0) & (v,w) , edge of weight (a+w)^2 between (u,a) & (v,w) , for the edge u,v with weight w given in question, I applied dijkstras on transformed graph..

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

In problem D, I know that we can use binary search as described in the editorial. But I noticed that if $$$x$$$ is too small, we cannot find a sufficient subarray. Consider the first example in the statement of list $$$[1, 2, 3, 2, 1], k = 3$$$. Assume that we want to check if it is obtainable the median of $$$1$$$ or not. But we cannot choose from $$$[1, 2, 3, 2, 1]$$$ a subarray of length at least $$$3$$$ that has the median is $$$1$$$.

How can we overcome this?

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

    The binary search asks the question: is there a subarray of some length with a median of at least 1?

    So we replace all elements that are 1 or more in the array with a 1, and those that are less than 1 with a -1, resulting in [1, 1, 1, 1, 1]. We can find a subarray of length at least 3 with the median 1, so it follows that the answer is 1 or more.

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

After reading the solution to the problem D — wow that is very smart. The binary search never ceases to amaze me. Thank you for the beautiful problem (and the solution)

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

Can anyone please explain the formula of problem F?

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

    I'm not sure what the official solution is, but I can try to explain my solution to F. Consider a vertex $$$u$$$, and say we want to count all pairs of paths that have $$$u$$$ as their unique common vertex. First arbitrarily root the tree at $$$1$$$. We break the paths into $$$4$$$ types:

    • up paths: paths that start at $$$u$$$ and go towards the root
    • through paths: paths that contain the parent of $$$u$$$, $$$u$$$, and another node in the subtree of $$$u$$$
    • down paths: paths that have $$$u$$$ as the LCA of the endpoints and are not strictly up or down the tree
    • vertex paths: paths that are from $$$u$$$ to $$$u$$$

    For a fixed $$$u$$$, we can take care of all pairs of paths that contains vertex paths easily, so we ignore those. We can pair any up path and down path. We may also pair through paths and down paths that do not overlap. We may also pair a down path with a down path if they do not overlap. We can easily count all these quantities using Heavy Light Decomposition and LCA.

    Submission link

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

About C: Actually we can do this with only 18 queries.

Here's my code: 107897123

I can't explain code clearly, it is copyed from my code in C1 and edited a little. (Code in C1: 107895318)

Maybe someone could explain how this worked or hack it?

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

Amazing solutions!!!Orz

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

It seems to me that D, E are too technical and without ideas (it was immediately clear to me from the statesment what to do), but they are still funny

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

https://codeforces.com/contest/1486/submission/107832284 can anyone point out why is this solution wrong, I ran query overall range and then ran two queries for f, mid and mid+1,l if I find the same index in either of the answers then I search in that range or If I don't find any matching index I go the half opposite to that previous queries

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

Thanks for the "Hint" part! Super helpful! :D

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

When will I become a CM? :-(

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

It's been a pain using the CF system tester to debug an interactive problem like C (even the asserts return nothing, just a Runtime Error). Here's a brief snippet in case anyone else would like to test this locally. It basically generates n different numbers (permutation from 1 to n) and shuffles them.

Instead of doing cout/cin operations, as you would do in the actual solution, here you could just call the ask() function and it would give you the second maximum for that range (each call takes O(n) time — so you would obviously time out if your overall solution is not some form of binary search / log(n))

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

Can someone please help me where my code is failing. TIA.

107911405

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

    Worst case needs O(n) queries. Try: [7 6 5 4 3 2 1 8]

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

      But I am always halving the search space? Can't seem to figure out the error in my logic.

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

        mid==l is possible, and when you call binarysearch(mid+1, r), you are decreasing the search space by size 1, not halving it.

        More importantly it sometimes gives wrong answer. Try 4 5 6 1 2 3

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

          Oh I got it. I made 2 variables mid and and middle and got confused between them :(

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

In the editorial solution of problem c2,in binary search l+1<r is used in while loop instead of l<r used usually.can someone explain the logic behind?

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

    Let's consider the case when smax<maxpos (the first while loop in the code). The loop invariant property is that the range [l,..,r] always contains both,2021-02-19 smax and maxpos. It is beneficial to break the loop just when l and r become adjacent (i.e. when r - l > 1 becomes false) because now the only way for the above property to hold is that l==smax and r==maxpos, and we can immediately output r (or r+1, in case of 1-based indexing).

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

      why is it like the range[l...r] will contain both smax and maxpos like in the example: 1 4 3 2 5 the loop will break at l=3 and r=4 whereas the index of smax is '1'(0 indexing).

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

I'm getting this error in C1 and C2 problem " wrong answer Integer parameter [name=query_l] equals to 100000, violates the range [1, 99999]" Can anybody help to fix that this is my code

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

Quality of editorials have increased a lot from past few months.

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

    Can you please explain problem E, not able to get what the editorial says.

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

If you use SPFA in E,you will be TLE.

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

Good problems, moderate difficulty, but weak tests of E.

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

Can someone please explain problem C2 didn't get the logic from the editorial. Thanks in advance :)

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

I can't get why my solution is giving me a wrong ans at test 7 of question D. My approach is a little different from the editorial. https://codeforces.com/contest/1486/submission/107932779

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

    You get the max median of subarray of size exactly k not at least k so you bug on 3 2 2 1 2 Your answer is 1 true answer is 2

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

In C2 solution and find_left binary search why do we return hi instead of lo?

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

    Cause it's the first element to get the needed answer as written in the editorial

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

      Can you please tell the logic behind using r-l>1 instead of usual l<r in binary search in the editorial solution?

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

        I use binary search with subsegment $$$[l, r)$$$, so it means I look at elements $$$l, l+1, \ldots r - 1$$$. This is called a half-interval (in russian at least). That's why $$$[l, l+1)$$$ contains only one element.

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

          Thanks! If i use l<r, the program stucks in infinite loop like in the sample test case 5 1 4 2 3 it goes like:


          5 ? 1 5 3 ? 1 3 3 ? 2 3 2 ? 1 3 3 ? 1 3 .....

          why so?

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

            Well, because l is always less than r. That's the beauty of half-interval.

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

              Can it be generalised that when we should use l<r and when l+1<r in binary search or is it just for this specific problem?

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

                I always write binary search this way. Just use half-intervals and you'll be happy. Consider it like this: l is minimal possible value and r is the first value, that is most definitely working out for you. You can rotate the problem (make segment $$$(l, r]$$$ and code wouldn't change at all (only checks and output).

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

                  ok,thanks a lot!

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

                  Thanks for your explanation, really appreciate it!

                  It would be really great, if you could elaborate on these points —

                  Consider it like this: l is minimal possible value

                  Minimal with respect to what condition?

                  and r is the first value, that is most definitely working out for you

                  You mean the first True value?

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

                  If the answer is in range $$$[1, n]$$$ you would use $$$[1, n + 1)$$$ or $$$(0, n]$$$. That easy, just add 1 (or -1) when needed and cover all the possible answers. Actually $$$(0, n +1)$$$ should work fine too.

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

          What are the initial conditions for l and r, in case I use your half-interval technique?

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

What does "wrong answer Integer parameter [name=query_r] equals to 49218, violates the range [50000, 100000]" mean? What am I missing? This is in testcase 5 problem C1

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

    Check whether l == r. The length of a range should be more than 1.

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

Can someone check my code for problem C. Still cant figure out the corner case I'm missing.

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

I can't explain me why my solution is wrong for problem A, can anyone help me? https://codeforces.com/contest/1486/submission/107785526

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

Can someone tell why it is giving wrong output 107936839

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

    If n = 5 and inputs are 2 1 1 1 1, then for loop breaks in the fourth iteration and the last one is acting as n for next test case.

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

      How did you debug so fast... I'm a beginner and often unable to find out mistake in my own code and often waste much time finding out mistakes, please tell how to do that

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

Why is this getting WA on testcase 3 ? can anyone help me. https://codeforces.com/contest/1486/submission/107964080

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

Can someone elaborate how you all are creating graph before applying Dijkstra? (problem E)

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

    I have considered $$$0$$$ based indexing of nodes to save memory.

    Since weights of all edges is between $$$1$$$ and $$$50$$$ . For each node say $$$nd$$$ , create $$$51$$$ nodes say $$$nd*51$$$,$$$nd*51+1$$$,$$$nd*51+2$$$,....$$$nd*51+50$$$.

    Now for each edge $$$x,y,w$$$ as input , make edges $$$(x,y,w)$$$ and $$$(y,x,w)$$$ as shown in following code since edges are bidirectional.

    add edges

    Let's understand how Dijkstra will work here with example :

    consider nodes $$$0,1,2$$$ . Suppose edge length between $$$0$$$ and $$$1$$$ is $$$e_0$$$ and edge length between $$$1$$$ to $$$2$$$ is $$$e_1$$$. Thus distance between $$$0$$$ and $$$2$$$ is $$$(e_0+e_1)*(e_0+e_1)$$$ by using definition in question.

    First we will insert $$$0$$$ to priority queue . Since $$$0$$$ is connected to $$$1*51+e_0$$$ with edge length $$$0$$$ , $$$(0,1*51+e_0)$$$ will be inserted in priority queue. Also $$$1*51+e_0$$$ is connected to $$$2*51$$$ with edge length $$$(e_0+e_1)*(e_0+e_1)$$$ , thus $$$((e_0+e_1)*(e_0+e_1),2*51)$$$ will be inserted to priority queue .Hence distance between $$$0$$$ and $$$2$$$ is $$$(e_0+e_1)*(e_0+e_1)$$$.

    Now try to scale above example to any number of vertices and it will be clear why it works.

    priority queue based implementation : 107975317 set based implementation : 107975265

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

In the editorial's code for C2, why is the binary search done over 0...n-1 and not 1...n, I've already noticed that the answer is being returned after incrementing it by one.

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

In problem D, I dont understand why the answer is monotonic. Suppose the array is [6, 1, 1, 1, 1, 7, 8], k = 3, the answer exist for x = 7(ie subarray [1, 7, 8]), hence according to the editorial the answer is atleast 7, this means answer for x = 6 should also exist, but there is no subarray of length >= 3 for which 6 is the median.
Did I miss something? Any help is appreciated.

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

    When your $$$check()$$$ function returns $$$true$$$ for some value $$$x$$$, it does not mean that there will be a subarray with median $$$x$$$, it means that the median is atleast $$$x$$$.

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

Except for the LCA part, I have had a nearly-linear implementation for task F.

My submission 108017714

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

    So you are strong enough:)
    Actually, I have written a pretty neat data structure to get RMQ in O(1) online (so it can handle LCA online of course).

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

      I didn't understand why you praised me until I actually read your editorial. :)

      I am not going to show off. However, I would like to add my little bit to nowadays's problem approaching style.

      In my opinion, the (nearly)-linear algorithm is the easiest to implement, without using any heavy data structure. Back in to the old days, the people who know HLD / LCT is the true master. XD

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

        Would you mind explaining how you obtained the triples (lca, subtree1, subtree2) in linear time (if you remember the problem by now and are willing lol)? I'm too lazy to read code, especially for a problem with such a long implementation.

        I did it for each vertex pair $$$(u, v)$$$ by computing the $$$(depth[u] - depth[lca(u, v)] - 1)$$$th ancestor of $$$u$$$, and the same for $$$v$$$. So I couldn't get rid of the log factor :(

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

          The problem is that given $$$u$$$ and $$$v$$$ where $$$v$$$ is an ancestor of $$$u$$$, we would like to know in which subtree of $$$v$$$ that $$$u$$$ lies.

          We can perform a DFS on the tree, and keep track of an array $$$\mathrm{current}[v]$$$, which is the current subtree of the vertex $$$v$$$ we are in. When reaching the vertex $$$u$$$, $$$\mathrm{current}[v]$$$ is what we need.

          This approach is conceptually simpler than what I had done in my code, and the only drawback is that we have to do DFS twice. One for LCA and one for the subtree query.

          My code is more complicated as I solved LCA and subtree query in one DFS as I remember.

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

            Ah, I understand, thank you so much! I suck at using the idea that you can maintain an array of size $$$O(n)$$$ during the dfs, it just makes my brain explode for some reason lol.

            A similar idea is that while visiting vertex $$$u$$$, maintain a stack $$$s$$$ consisting of the vertices along the path from the root to $$$u$$$ (in order). Then $$$current[v]$$$ is equivalent to $$$s[depth[v] + 1]$$$. I think the latter idea seems more intuitive to me, but maybe it's just because I've seen it before.

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

I am observing about a 2x difference between the runtimes of two solutions, by just changing the way I am indexing the array. I understand that this is because of different localities of reference between the solutions.

Yet, I am not able to figure out why one of these has a higher locality of reference. If someone can help me figure out the same, it will be greatly appreciated.

These are the two submissions: 108047029 108047388

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

I was trying to solve C2 but I am not quite understanding where the queries are running out. According to me, I am asking only one query before dividing the segment any further, so I think my queries shouldn't be more than 20.

My code: 108106042

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

In problem B, do we not need to consider unique points for x? suppose we have (0,0) (6,1) (8,2) (8,3) (8,4) and (10,4). Then vector for x will have <0,6,8,8,8,10> and the ans for this will be size = 6 x[3] — x[2] + 1 that is 1 but answer should be 3 (6,7,8)? pls help.

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

    Why do you think the answer should be 3? The answer is 1(only 8).

    1. for 6: the sum of distance is 16.

    2. for 7: the sum of distance is 14.

    3. for 8: the sum of distance is 12. Cleary, The minimal distance is for 8 only.

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

In E I combined all pairs of consecutive edges into a single edge and applied djikstra to the modified graph. It is giving WA on test 5. Please someone explain my mistake. Here is my solution 108800090

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

I am getting wrong answer on test case 16 in problem D and I am not able to find the error. Here 108604409 is my submission. If anyone can help..

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

Добрый день. Спасибо Вам большое! Наверное все же в D нужно стартовое r указать равное 2e5+1, иначе вылетит ошибка,например на таком тесте

5 2

1 7 8 1 1

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

    Внимательно прочитайте условие задачи. Данный тест некорректен.

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

I had been trying to solve problem E, and in failing to do so, tried to look for a solution in the editorial. However, I found the editorial for E a bit unclear, and both my brain cells struggled hard to comprehend it. Fortunately, I found kostia244's (sorry for the ping) solution in the standings page, which was quite clear and understandable to me, and was finally able to solve the problem after 1010 attempts. Hence, I decided to explain my (rather kostia244's) solution, just in case someone drops by here in future. So here it goes:
We maintain $$$dist[N][W]$$$, where $$$dist[i][j]$$$ stores the distance of node $$$i$$$ from node $$$1$$$ and the weight of the last visited edge is $$$j$$$. Initally $$$dist[i][j]=\infty$$$. To perform dijkstra on the graph, we maintain a heap, $$$q$$$ (or set, depends on your taste) which contains an item with three parameters:
1. the vertex, $$$u$$$
2. distance of the $$$u$$$ from the first vertex, $$$d$$$
3. weight of the edge through which we reached $$$u$$$, $$$w_{last}$$$
We relax the vertices in the following fashion:
If $$$w_{last} \ne 0$$$, it means we reached $$$u$$$ from another vertex $$$u_{prev}$$$, and the sum of the edge weight between $$$u$$$ and $$$u_{prev}$$$($$$w_{u_{prev}u}$$$) and the distance of $$$u_{prev}$$$ from node $$$1$$$, is stored in $$$w_{last}$$$. Now if we want to go to a new vertex $$$u_{next}$$$ from $$$u$$$, according to the problem, we can travel from $$$u_{prev}$$$ to $$$u_{next}$$$ with cost $$$(w_{u_{prev}u}+w_{uu_{next}})^2$$$. Hence, we update $$$dist[u_{next}][0]=(w_{u_{prev}u}+w_{uu_{next}})^2$$$, and push $$$u_{next}$$$ into the heap with no overhead cost, which means for $$$u_{next}$$$, $$$w_{last}=0$$$.
Otherwise, if $$$w_{last}=0$$$, it means we can reach $$$u$$$ from node $$$0$$$, with cost $$$dist[u][0]$$$. Hence, if we want to travel to an adjacent vertex $$$u_{next}$$$, we update $$$dist[u_{next}][w_{uu_{next}}]=dist[u][0]+w_{uu_{next}}$$$, and push $$$u_{next}$$$ into the heap with an overhead cost ($$$=w_{last}$$$) of $$$w_{uu_{next}}$$$, and we move on in life.
Now, the answer for $$$u$$$ is $$$dist[u][0]$$$, and we print it accordingly.
C++ implementation.