KAN's blog

By KAN, 7 years ago, translation, In English

I'm sorry for the delay, it took some time for me to translate analysis to English.

Tutorial is loading...

Problem author: KAN, preparation: KAN.

Tutorial is loading...

Problem author: KAP, preparation: KAP.

Tutorial is loading...

Problem author: KAP, preparation: demon1999.

Tutorial is loading...

Problem author: ZhNV, preparation: ZhNV.

Tutorial is loading...

Problem author: SYury, preparation: kuzmichev_dima.

Tutorial is loading...

Problem author: KAP, preparation: KAN.

  • Vote: I like it
  • +21
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

After waiting for a long time,the tutorial is finally ready.

»
7 years ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

C was quite hard to understand, time zones are really confusing mind :D

thanks for cool task )

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can someone please explain it again, thanks.

    • »
      »
      »
      7 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Let's find out which timezones will participate when the local time in TZ#1 is 1 hour. s =minimum time of start f =maximum time of finish => f-1=maximum time of start.

      If time is 1 in first time zone then in the second time zone is two, in the third is 3...

      Okey, let's make it on a sample:

      5

      1 1 1 1 1

      2 4

      Which time zones will participate? TZ#2 and TZ#3. TZ#4 won't participate because their local time will be 4. So the contest will end at 5, which is not good for us.

      Now let's find out: "How many people will be in?" Answer is obvious a[2]+a[3], which is a[s]+a[s+1]...+a[f-1].

      Now let's find which timezones will participate on h=1...n; (h=local time at TZ#1)

      Let's make it again on sample:

      5

      1 1 1 1 1

      2 4

      h=1: a[2]+a[3]

      h=2: a[1]+a[2]

      h=3: a[5]+a[1]

      h=4: a[4]+a[5]

      h=5: a[3]+a[4]

      More formally for (h=1 to n ) {

      beg=s-(h+1)

      if (beg<1) beg=n+beg;

      end=f-h;

      if (end<1) end=n+end;

      curans=sum(beg->end)

      if (curans>ans)

      {

      ans=curans;

      time=h; }

      } This is an O(n*(f-s)) solution. Because each time we calculate sum from the beginning. But we don't need to calculate each that way. We can save previous sum. And the current sum will be:

      cursum=(previoussum+a[begin])-a[end%n+1];

      Complexity O(n)

»
7 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Using lemmas 1 and 3 from E editorial it is easy to prove that we can use ternary search as alternative way to find optimal prefix for each query with type = 2.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can you please explain the ternary search idea ? At first glance on this problem I thought a ternary search idea but while implementing it, I found some difficulties. Then I solved it using binary search. Thanks in advance.

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Well, let store all x in x[i], and while adding i element we will calculate prefix sum for first i elemets (sum[i] = sum[i - 1] + x[i]).

      Then, it is obvious that we should always use x[1] and x[cnt] element (where cnt — how many elements we have at the moment). So, we take x[cnt] and then search best answer choosing what prefix should we use from [1;cnt - 1].

      Define get(i) as function returns max(i) - mean(i) (what we want to maximize). max(i) = x[cnt] for any i, mean(i) = (sum[i] + x[cnt]) / (i + 1).

      Suppose we have best answer with i = p. Then, get(i - 1) ≤ get(i) for any i in range [1;p], and get(i) ≥ get(i + 1) for any i in range [p;cnt - 1].

      We can use it to search p in [1;cnt - 1] with ternary search (and answer for query will be get(p)): http://codeforces.me/contest/939/submission/35440173

»
7 years ago, # |
Rev. 7   Vote: I like it +19 Vote: I do not like it

I would like to share What I learnt from question E.

Solution with multiSet, O(n*log n) in worst case scenario, uses long double , gets TLE , 3 seconds

Solution with Queue, O(n) in worst case scenario, uses long double, gets TLE , 3 seconds

Solution with Queue, O(n) in worst case , uses Long long instead Long double, Gets Accepted , 608 mili seconds

Solution with multiset, O(n*log n) in worst case, uses long long instead of long double , gets Accepted , 654 mili seconds

Conclusion :

1) O(N) solution with 'long double' data type is slower than O(N*log N) solution with 'long long' data type. type. (still runtime depends on how many operations we are doing in each cycle)

2)Question E is pretty straight forward, There was no need for Binary Search or Ternary Search.

3)Always prefer to use "long long" or "int" , instead of "double" or "long double". Because simple operations like Addition or subtraction becomes very slow on these data types.

GL & HF :) .

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I can't understand problem C, In example 1 it is said that contest should start at 3 hours at time zone 1 that would be 1 hour in time zone 2 and 2 hour in time zone 3. But As stated by problem time at ith time zone should be i if time at 1 is 1 hour. So the time at second time zone should be 4 hours and third 5 hours.

  • »
    »
    7 years ago, # ^ |
    Rev. 5   Vote: I like it 0 Vote: I do not like it

    hmm, you should imagine that like in real life, except it only has n hours ( not 24 hours) and doesn't have 0 hours. Here is an example how that goes in time zone 1 if we have n=4: TZ1: 1 2 3 4 | 1 2 3 4 | 1 2.. and so on. Meanwhile, in TZ2: 2 3 4 | 1 2 3 4 | 1.. and so on . Hope you can understand, my English and explanations is not good at all =))) .

»
7 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Shouldn't in Task E, Lemma 3 should say f(i) is non-increasing?

»
7 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Anyone please explain or share the solution idea of problem F — Cutlet. I think another editorial is needed for understanding the given editorial for this problem.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    I'm not sure what the editorial is saying either, but let me explain my solution.

    Let the two sides of the cutlet be A and B. Consider the case where we have two availability intervals [li, ri] and [lj, rj] with i < j. Let's say we enter interval i with side A, flip to B during interval i, stay at side B until interval j, and flip to A during interval j. If we only consider these two flips, then the amount of time we had side B would be in the interval [lj - ri, rj - li]. If we have two more intervals x,y where i < j < x < y, we can perform the same flip as above between i and j with x and y as well (ie. A -> flip at x -> B -> flip at y -> A). In fact there are no conflicts regardless of how we flip things at i, j, x, and y as long as they are of the form I described earlier.

    Let's build a graph as follows. Each interval becomes a vertex. Let i < j, we connect interval i to j with a directed edge of length 1 and value [lj - ri, rj - li]. Connect the source to all intervals with value [li, ri], and connect all intervals to the sink with value [2n - ri, 2n - li]. These edges represent the time the cutlet spends at side B. We also have to add the side A edges, which are just duplicates of these with value [0,0]. Value here means the time the cutlet spends on side B.

    Now, taking an edge from i to j means we do the flips A -> flip at i -> B -> flip at j -> A. This gives some time in the value of the edge. The value of a path whose edges have values [l1, r1], [l2, r2], ... is simply [l1 + l2 + ..., r1 + r2 + ...].

    IF we add the restriction that we can only have The solution to the problem is now finding the shortest path from the source to the sink whose value contains n, taking alternating side A and side B edges (ie. we can have the cutlet on side B for time n).

    To deal with the case where we might have to flip multiple times in one interval, first we note that we want to flip at most twice in a given interval. So we add side B edges from i to i with value [0, ri - li]. This doesn't cause a problem because we must take alternating side A and B edges. However, this only considers cases where we do A -> flip at i -> B -> flip at i -> A, but this is sufficient because the B -> flip at i -> A -> flip at i -> B case cancels out with this one.

    To find the shortest alternating path, you can use Dijkstra with an extra flag that says whether to take a side A edge or a side B edge. Also store the value for each visit. Whenever we reach the sink, check to see if the value contains n, if it does, then we found the answer, otherwise, skip this path and keep looking.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve D if we can apply spells only on first string?

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Applying spells only on first string is equivalent to applying spells on either strings since spells are bidirectional in this case.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Problem D. can be done using DSU. iterating from left to right (from i=0 to n-1), if s[i] and t[i] are of same set, then its useless to add them in the answer, since there already exists a path between them in that set, and that is s[i] to leader, and leader to t[i] (and that had already been counted earlier). But if both have different leaders/sets, then both sets have to be merged, increasing the final answer by 1, also save the leader of those two sets as a spell. See my submission.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve A ? I realy dont understand

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    We need to check whether a love triangle exists or not. To do so, it is sufficient to check for all i that whether the ith plane is a part of a love triangle or not. Let us say that the ith plane is A, f[A]=B and f[B]=C. if A is part of love triangle, then f[C] should be equal to A. Also f[C] = f[f[B]] = f[f[f[A]]] which should be equal to A, thus it is sufficient to check for all i that whether f[f[f[i]]]==i or not.