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

Автор Edvard, 10 лет назад, По-русски

Привет, Codeforces!

22 августа 2016 года в 17:00 MSK состоится шестнадцатый учебный раунд Educational Codeforces Round 16 для участников из первого и второго дивизионов.

Это будет последний учебный раунд поготовленный мной. Как я уже ранее писал я начал работать в замечательной компании AIM Tech и теперь у меня стало заметно меньше времени на подготовку раундов. Не хочется из-за этого терять качество и интересность раундов, поэтому вскоре вы узнаете кто будет продолжать подготовку раундов.

Комплект задач был частично предложен участниками сообщества. Задачу C предложил Resul Hangeldiyev Resul. Задача E — это очередная задача предложенная Zi Song Yeoh zscoder. Задача F была предложена пользователем Александром Кульковым adamant. Оставшиеся задачи некоторое время крутились в моей голове и я наконец решил их дать на раунд (они являются достаточно стандартными, но важно уметь их хорошо решать).

Задачи для вас подготовил я (Эдвард Давтян). Спасибо Татьяне Семёновой Tatiana_S за проверку английских текстов условий. Большое спасибо Ивану Поповичу NVAL за вычитку и тестирование задач A-E и Александру Кулькову adamant за вычитку и тестирование задачи F.

На раунде вам по традиции будет предложено шесть задач. Надеюсь они вам понравятся! Думаю комплект задач получился чуть сложнее, чем обычно, но уверен это вас не остановит :-)

Good luck and have fun!

UPD 1: Завершено тестирование на взломах. Спасибо за участие!

UPD 2: Опубликован разбор задач.

  • Проголосовать: нравится
  • +249
  • Проголосовать: не нравится

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

Your rounds are always interesting, We'll miss these rounds :D

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

Good luck at AIM Tech!

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

what a sticker !!!!!!!!!!!!!!!!!!!!!!

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

I love the ECR,because it's unrated,so I can enjoy the competition's process rather than the result!

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

моё увожение

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

Rated?

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

Guys , is AIMTech round rated ?

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

Is this rated?

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

This does not stimulate without rating

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

Wish good luck to all :)

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

Hacking friends code is the funniest part :D

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

Why late ?

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

10 min late!!!!!!!!!!

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

Codeforces: " Contest has started. Proceed? OK? ".

Me: " OK! ".

*Page Refreshes"

I travel back 10 minutes

Codeforces: " 10 minutes remaining ".

and people said time travel is not possible :)

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

Now that the contest has been delayed there's a even shorter gap between this and csacademy's round...

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

wish there be no long queue...

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

Спасибо авторам за качественные и интересные задачи!

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

Didn't understand Problem B at all, can someone explain what was supposed to be done?

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

Awesome, it could be great Div2 round!

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

Did someone pass E using segment tree?

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

Please someone explain us the "E" problem!!

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

Is there no open hacking this time?

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

I have no clue how people managed to come up with logic of building odd matrix without googling the solution,this algorithm is way confusing on first look.

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

Statement for B problem asked for a a which may or may not exist in the array. The question statement was ambiguous. Many coders faced the difficulty that they understood that it may any integral point and they were giving the geometrical median, whereas the question was asking for any x in the array. Please make clear statements.

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

I asked in problem B if the point X is one of the input points but even the answer didn't show that it must be one of the input points

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

Can we solve F with trie data structure?

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

Скажите, пожалуйста, можно ли было сдать F Ахо-Корасиком? Я сдавал несколько раз, но стабильно получал WA7, и до сих пор не понимаю, в чем ошибка.

http://mirror.codeforces.com/contest/710/submission/20056721

UPD. В чем ошибка понял. Но как исправить не очень представляю.

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

looking at tourist's solutions makes me feel so stupid

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

can anyone hack this solution, as the value for accumulating sum is taken as int. http://mirror.codeforces.com/contest/710/submission/20059546

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

    I think it can't be hacked. Because count1 will always equal to count2, so it doesn't matter whether they overflow or not. And the code will always output coords[N/2 — 1] if N is even, and that's the correct answer.

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

Was it possible to solve problem D using ternary search?

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

Tests of problem C are all possible inputs! and someone are trying to hack !!!

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

problem B ) please guys now i understand the optimal solution is to get a point from the input to be the optimal .ok ! but i don't know why the element in the middle of the array is the answer not the middle of the all distance ? proof please!

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

    You could also try out all the points and pick the best one.

    http://mirror.codeforces.com/contest/710/submission/20051324

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

      i did saw it before and i liked it but i want to understand why the middle element ? i can do ternary too can't i ?

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

        For example if you have n = 7 and the following points

        1 3 5 7 9 11 13

        Let's say we choose the middle point ( 7 ). If we try to move it to the left we'll end up adding 2 ( 7 — 5 ) to the total distance 4 times while decreasing 2 from the total sum 3 times which means that the total distance will be greater than the distance we would've had by choosing the middle element.

        Same goes for moving the element to the right.

        EDIT: If you meant why the middle element and not the middle of all the distances, that's because you need to choose an element from the array.

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

          why the middle element and not the middle of all the distances?

          that was my question ..but thinking in it i think selecting a point from the array will be better

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

            Suppose you have two points, you can easily see that while you are in between them the sum of distance is constant and the smallest and the leftmost point is the point on the left(and an integer as it is an input point), but if you go right of the right, sum of distance increases and so is if you go left of the left. Now consider 3 points, for the extreme points the answer lies between them, now forget them and consider the middle point, for a single point the answer lies on the point itself because deviating from it will increase the value of distance from it to a nonzero value and since it is in between other two points it is the answer. Next, consider 4 points for the extreme two points least sum points lies between them and for the middle two between them and points between middle two also lies in the middle of extreme so the answer is left of the two middle points as it is the leftmost satisfying above conditions. And so on...

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

hi there , i think there is a problem in your judge , during the contest i submited the true answer , but i received the wrong answer , i checked that answer and there is no wrong thing in my output , here is my code :

http://mirror.codeforces.com/contest/710/submission/20061137

any ideas ?

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

What is the idea behind problem F? A dynamic Aho-Corasick?

I'm struggling with this! :-(

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

    My approach is SQRT decomposition. Lets have a constant B equal to about 2000.

    If a query of 3rd type has length less than B we can simply check all substrings and get the answer. The complexity is , where L is the length of the text.

    If a query of 3rd type has length greater than B we can make an O(N) or O(L) approach, because there are at most such queries. We can build a tree of the suffix links (if we use suffix automaton) or failure links (if we use Aho Corasick). The edges are of form "link[u] -> u". Then to for every such query we will build such a tree. Now for every string in the set D, we update its end state's subtree with 1. Now to get the answer to the current string, we go through all of its prefix states and query them (vertex query). So the problem becomes subtree update and vertex query. The whole query can be done in time.

    The overall complexity is . We want to have almost the same complexity for the 2 types of queries of 3rd type. Thats why we pick B about 2000.

    I solved the problem with a generalized suffix automaton during the contest but it can be easily replaced with an Aho Corasick. Link to submission: 20062687.

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

      If a query of 3rd type has length greater than B,for each query,you will build a tree,what's the complexity about making a tree,is the total length of strings in suffix automaton? And there are L/B queries,how to calculate complexity?

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

        If the query is of 3rd type and it's heavy (has length  ≥ B) we build the tree on all string in the automaton. We can easily build it in O(L). There are at most heavy queries because every heavy query has length  ≥ B. This means that to solve all heavy queries we have complexity of .

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

    Another interesting way to solve it is to keep logM automata. When adding a string, you create an automaton of size 1, consisting of that string. While you have two automata of the same size, merge them, obtaining one of double the size. The merge can be done using the typical Aho Corasick build procedure. To handle the erase operation, we can keep another logM automata, composed of the erased strings. We will handle these the same way we did for the above. Therefore, the answer of a query will be query(all) - query(erased). This achieves a complexity of O(sumlengths * logM). This idea was discussed here. 20053668 implements it, to some extent.

    If you prefer not to use SAM or Aho Corasick, you can insert the strings of length < SQRT into a trie, and for each of the rest you build the KMP automaton. This method achieves O(sumlengths * sqrt(M)). You can check out 20058436 for more details.

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

      Wow! The first solution is really cool! Thanks for explaining it :)

      By the way there is another possible solution with hashes. We can see that there are different lengths of strings. That means that we can store all hashes for a given length in a hashtable for the corresponding length. This way we can simply create a solution with hashes.

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

        But there may be more than sqrt(L) hash values, so the time complexity may be more than O(Lsqrt(L)) I guess?

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

          There can be at most O(sqrt(L)) distinct lengths because the sum of the lengths doesnt exceed L, so in the worst case, which is 1 + 2 + .. + k = l equivalent to k * (k + 1) / 2 = l, k is of order O(sqrt(L)). To answer a query we will traverse all the O(sqrt(L)) hash tables, which we can query in O(1), along with some partial substring hashes on our query string.

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

          Oh I understand now. Thanks for the explanation.

          But I think this method might not be fast enough. My submission get TLE.(20105952) Maybe if the time limit is 10s then I can pass?

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

            You can pass it, if you are careful when implementing it. Try to get rid of unnecessary modulo operations, instead of keeping an unordered_map of unordered_maps you can keep a set or an unordered_set of fixed size, and one set to keep track of the distinct lengths.

            L.E.: 20106642 is my accepted code, following this idea.

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

I'm a rank beginner. Can somebody shed some light on the arithmetic progression problem? I brute forced it and it gave me several TLEs. I have a hard time figuring out just by looking at other people's submissions.

I'd appreciate it if someone could point me to the correct number theory algorithm or lemma. Thanks.

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

I think E can also be solved using bfs with priority_queue. Did anybody try it?

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

    I've already done it in C++ and got TLE.

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

    I treat this problem as the shortest path problem. At first I got TLE when x is small and y is large, that's because the distances of the nodes are updated quite frequently. Then I set the initial distance of the i-th node as i*x, so the distances won't be updated too frequently. Then I got AC.

    You can also see my submission for details 20059443

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

    It can be solved with Dijkstra after a few observations.

    • First of all, make a list of relevant states. N is relevant. Then, if x is relevant, x - 1 and x + 1 are relevant if x is odd, and x / 2 is relevant if x is even.
    • If we're gonna do many add operations in a row, it makes sense to do it only before our first copy operation, otherwise we could just replace two of them for only one before the last copy operation. So we set D[i] = X * i for all relevant positions x.

    Once we're done, we push all relevant states with their initial distances to the Dijkstra's queue and run it. It runs instantly. See here -> 20087826

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

Fuck Editor

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

For the problem B

a same position could have more than one points?

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

Where can I find the editorial please?

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

Did anybody solve problem D? Can anybody give me an approach for this problem?

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

    We can assume that b1 <= b2 (otherwise let's just swap(a1, a2) and swap(b1, b2))

    Sequence 1: b1, b1+a1, b1+2*a1, b1+3*a1, ...

    Sequence 2: b2, b2+a2, b2+2*a2, b2+3*a2, ...

    1) Let's find minimum K>=0 such that b2+K*a2 belongs to the sequence 1.

    b2+K*a2-b1 must be divisible by a1. It's enough since b2+K*a2-b1 >= 0.

    K*a2 = b1-b2 (modulo a1)

    Let's denote GCD(a1,a2) = a_gcd

    1.1) If (b1 — b2) is not divisible by a_gcd, there is no solution to this. The sequences do not have any common numbers, thus the answer to the problem is 0.

    1.2) If (b1 — b2) is divisible by a_gcd:

    K*(a2/a_gcd) = (b1-b2)/a_gcd (modulo a1/a_gcd)

    We can use any algorithm to find "modular multiplicative inverse": https://en.wikipedia.org/wiki/Modular_multiplicative_inverse

    2) The first common element of Sequence 1 and Sequence 2 is A = b2+K*a2

    Next elements will be A+LCM(a1,a2), A+2*LCM(a1,a2), ...

    If you know A and LCM(a1,a2), it's easy to find how many of these numbers belong to interval [L, R]

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

    I solve it with 2 brute force solutions.

    0) if a1 < a2 swap a1/a2 and b1/b2

    Brute force #1: a1 > 1000 iterate first sequence and check if point in second.

    Brute force #2 a1 <= 1000, if b1 < b2, required increase b1 to be >= b2 Scan b1, b1+1, b1+2*a1, ... b1+1000*a1 to find a first common point, if not exists answer will be 0 As we know first common point and also need know a step it will be lcm(a1,a2) Now we have one progression and need know count of points in interval [l,r] it possible done in O(1).

    That's all.

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

Problem D, test 84: 1643097 -1745072283 4818 -1699409409 1391770030 14101637

How about constraint L <= R?

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

IT WILL BE NOT EASY! BUT I WISH EVERYBODY GOODLUCK!

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

I implemented aho corasick algorithm on problem F and I got idleness limit exceeded on test 1, I wonder what is the cause? It's the first time I encounter this problem so I don't have an idea where the problem lies. =/

[submission:http://mirror.codeforces.com/contest/710/submission/20107516]

UPD: Didn't noticed the solution is required to be online. =P"

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

When will the editorial be posted?

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

Будет ли разбор?

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

Here is my solution to Problem D,may be easier to understand.

Some restriction like:

If we combine these,we can get some scale like L1 <  = k' <  = R1

Then,we need k' be integer which means a1 * k' - a2 * l' = b2 - b1 has integer solutions and . This can be solved used exgcd.

If u are still confused,here is my code.

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

This round is important and must have an English Editorial.
Codeforces is a good international site.
Please don't limit us by only releasing in Russian. I kindly request you to release an English version.

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

Who will continue to prepare next educational rounds?

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

When will the next educational round start? It is a good idea to have a educational round on every Sunday.