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

Автор vovuh, история, 6 лет назад, По-русски

1015A - Points in Segments

Разбор
Решение (Vovuh, O(n + m))
Решение (Vovuh, O(n * m))

1015B - Obtaining the String

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

1015C - Songs Compression

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

1015D - Walking Between Houses

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

1015E1 - Stars Drawing (Easy Edition)

Разбор
Решение (MikeMirzayanov, O(n^3))

1015E2 - Stars Drawing (Hard Edition)

Разбор
Решение (Vovuh, O(n^2))

1015F - Bracket Substring

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

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

Thanks for the editorial.

I have one question about problem F dp recurrence, would the recurrence stated above be able to differentiate

(seq)seqseq

from

seq(seq)seq

given seq is a valid bracket sequence, and the substring you are looking for.

obviously in the two solutions the position, balance, and boolean parameter will be the same. Also the prefix of length K should be the same too.

So I am asking, how will this dp handle situation such as this?

UPD:

Ok so after some thinking I realized that the dp state does not have to have a unique sequence, the only thing that has to be unique is the value it returns given a set of parameters. So seq(seq)seq and (seq)seqseq should be able to "reach the end" equal amount of ways, but I'm no so sure they can be made an equal amount of ways.

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

    In my DP I do something special the first time the full pattern is encountered: I call another DP that doesn't track the pattern at all and simply counts the number of valid bracket sequences with remaining characters and balance number.

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

There is a problem that we can't hack the O(N^3) E2 solution. Because that our test cases is bigger than the limit->256 KB, but it can be a possible input.

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

    Use a generator instead.
    P.S. My solution is plain simple O(N3), and with N = M = 1000 test (all stars), it passes with 2430 ms. Fairly wide gap (for 3000 ms constraint), so I don't expect a lot of hacks with that test.

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

    You can submit the code that generates test case of any size you want. See "generated input" tab in a hack window.

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

    maybe it's O(n^2 sigma{i,j} min(i,j,n-i,m-j))

    i write a program like this

    #include "bits/stdc++.h"
    using namespace std;
    typedef long long ll;
    int main() {
        int ans = 0;
        for(int i = 1 ; i <= 1000 ; ++ i) {
            for(int j = 1 ; j <= 1000 ; ++ j) {
                ans += min(min(i, j), min(1000 - i, 1000 - j));
            }
        }
        printf("%d\n", ans);
    }
    

    it print "cnt = 166666500, time = 1.666665"

    and my program used 1.30s pass the all star without 4 corner stars data

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

Почему бы не хранить в 1015С вектор разностей a[i]-b[i] вместо вектора пар? Тогда будет легче сортировать

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

IN question E2 if it would have asked to find the minimum possible stars too and if many multiple answers exist print any. Then how could we solve it any idea anyone?

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

When will the rating be published ?

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

FOR problem 'F' , can anyone explain me in simple words(possibly with an example) the dp strategy? I mean dp(i,j,k,l)part. Not able to understand tutorial.

Also, not able to understand the recurrance part.

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

Спасибо за простое решение Dшки, смотря чужие решения, осознаёшь что эта задача была "напишите кучу If'ов".

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

For the first problem

Can someone help with these:

How is the prefix sum calculation approach relevant to this problem (I fail to see) Why take size of cnt as m+2 Like why do --cnt[r+1] in the first for loop and why cnt[i]+=cnt[i-1] in the second for loop

Thanks!

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

    This is the so-called "event scheduling" process. Basically each segment has a beginning and an ending index (l and r). If you were to add all points from l to r manually, you would get an O(nm) solution. However, notice that you can only add one to position l, and when you traverse the array once again, and use prefix sums, pref[i] will >= 1 if there is a point on position i. However, since there is no point in r+1, nor is there in any indices to come, we put -1 at that position so the prefix sum would become 0.

    Adding multiple segments allows the prefix sums to be equal to the number of segments covering point i at position pref[i]. Try some examples on paper for better understanding.
    Here is an example (l=1, r=2, l=0, r=5):
    The event schedule array looks like this:
    {1, 1, 0,  - 1, 0, 0,  - 1}
    And the pref array looks like this:
    {1, 2, 2, 1, 1, 1, 0}
    (the arrays are 0-indexed and pref[i] really shows the number of points on position i).

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

I found another way to do D which is also really simple 41070174

Did anyone else find another valid way?

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

    Actually, I did the same thing.

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

    Mine might be similar to yours, my distance was ceil(dist / k), and then I reduced my dist by that amount, and reduced k by 1. I can't prove why it's correct though.

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

For D, what I did was s = (s / k + 1) * (s % k) + (k — s % k) * (s / k) In other words took s / k + 1 steps back and forth s % k times and remaining steps were s / k long.

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

problem E — dp+difference array, that was a nice problem (Y)

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

For E I have a shameful solution in O(nmlog(nm)). I keep prefix sums for how many stars I've seen after me, but I don't keep track of contiguous subsequences or anything. Instead, for each point (i, j) as middle of star, I binary search for the maximum length of the star centered there by using prefix sums and checking that sum in all four directions is at least my binary search value (it obviously cannot be more). Then I use segment trees to mark that a point is included in some star. I store all my stars in a vector, but before printing them out I query for each point on the grid that is * I check if it is involved in a star (either by row or column) using segment tree point max query (I am so eager to add log factors). If all the *'s belong to stars I print out all the stars I found in the binary searches. This passes the big data in 1715 ms.

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

I am not able to understand the dp part in Problem F can anyone elaborate it more. I am also not able to understand the need of finding the first Len dp.

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

    The main idea is if you obtain some prefix of s and trying to add some character which is not equal to the next character of s, your target is to obtain the maximum prefix of s with the last character you add. For example, if s = ((()(())((), you obtained the first six characters ((()(( and you have to add the character (, you want to get the prefix of s of maximum length (because you want to obtain the string s as soon as possible, right?), your string will be looks like ((()((( and the maximum prefix of s corresponding to the suffix of this string will be (((. The first dp is needed to make transitions in the second dp faster.

    And the second dp just calculates the number of prefixes of regular bracket sequences with the last k characters equals to the prefix of s of length k.

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

      As it is given that s must be a substring, then we can calculate how many ')' or '(' are needed to make it a regular string. For example in '( ) ) ) ( )' , we need two '(' to the left of s ,hence now we have to place 2 (4-2=2) more brackets(after balancing s) , those two can be(bold brackets) : ( ) ( ( ( ) ) ) ( ) or ( ( ) ( ( ) ) ) ( )
      or ( ( ( ) ( ) ) ) ( ) or ( ( ( ) ) ) ( ) ( ) or ( ( ( ( ) ) ) ( ) ) so there are five places to put one pair in the string, now we can do that for any number of pairs, which is the catlan number, and we can put these in any gaps (except in s). Is this a correct approach?

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

        Well... I don't sure you can solve this problem this way, because it is too hard to consider all valid placements of brackets for a random bracket string exactly once. Because of this we do dynamic programming

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

          for each gap we can have dp state such that if there are n gaps and x is the number of pairs that has been used then we can calculate dp[n][x] from dp[n-1][x],dp[n-1][x-1],dp[n-1][x-2]... and multiplying their respective catlan numbers . to speed up this.

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

        tatya_bichu No it cannot be solved by this method, Initially i was also thinking on the same lines. But the problem is, we are trying to make the sequence before the given sequence regular and same for the sequence after the given sequence. I mean, if we create a sequence like seq1 given seq2 than seq1 will handle the closed brackets and be regular apart from this. and seq2 will handle open brackets and be regular apart from this. But It need not be the case.

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

      Thanks Vovuh. I understood the concept except the factor 'j' in dp(i,j,k,l).

      COuld you please explain what does 'balance' exactly mean?

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

        In some sense the balance means the number of the opening brackets ( which are weren't closed on the current prefix. I.e. for empty prefix the balance is zero always. If you will add the opening bracket, the balance will increase by one. If you will add the closing bracket, the balance will decrease by 1. But there is one more thing. In any moment of time the balance should be greater than or equal to zero. It is because you should handle the case when the number of closing brackets is getting greater than the number of opening brackets. Btw you can try to understand it by yourself with this definition of balance. I hope it helps!

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

Что означает амперсанд в квадратных скобках?

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

    Что все параметры в лямбда-функцию передаются по ссылке.

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

Problem E "If it is impossible to draw the given grid using stars only, print "-1"." So all dots case's answer is 0 instead of -1?

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

In problem E how the output is -1 for input 5 5 .*... **.. .... .*... .....

Someone, please explain.

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

I also have posted this in the Announcement:

Hello, in problem F, why does this solution not work :

read n

read sring

get delta of string : delta"(("=2 ; delta")"=-1 like if you have "(" add 1 and if you have ")"

substract 1

get the minimum over all deltas :

code :

int del=0;

int mi=del;

for(int j=0;j<sz;j++)

{

if(a[j]=='(')

    del++;

else

    del--;

mi=min(mi,del);

}

than just fix where you want to fit it in

like [ poz, poz+sz-1 ]

I am giving you the code

http://codeforces.me/contest/1015/submission/41118240

thanks in advance

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

Hi wanna ask how this code gets TLE? It is E2 solution with about lets say 10 nested loops which do about 10^6 operations ( each at most) , so how does not it pass in 3 seconds? Pretty clear code below: http://codeforces.me/contest/1015/submission/41120679 I'd be very thankful for any help.

Edit: Fixed. Endl operation took super long.

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

Why it 's wrong answer on test 26, i didn't know where is wrong? Please help me! Thanks! http://codeforces.me/contest/1015/submission/41127014

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

    It says it in the error message. You printed 1000000001, but there are only 1000000000 houses.

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

      I printed the answer by using freopen and Ctrl+F (find) 1000000001 but not exist in my answer.

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

      Thank you! I fixed my problem.

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

In the F tutorial: which equals to the suffix of the prefix of s of length len

I think this len should be i?

Sorry for poor English.

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

The std of E2 is TLE

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

In Stars Drawing (Hard Edition) Please explain why? Let's increase h[i,j-len] by one and decrease h[i,j+len] by one. Thanks

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

I am not getting the editorial for F.can somone explain me What is len[i][j]

Plzz explain!!

Let leni,j will denote the maximum length of the prefix of s which equals to the suffix of the prefix of s of length i with the additional character '(' if j=0 and ')' otherwise. In other words, leni,j

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

    Do you know how does Knuth-Morris-Pratt algorithm work (especially "Partial match" table) and why does it work? If not, start with that. It might not be easy, I know, but there are multiple resource on this in the Internet and I won't be able to explain it better than it was already done in some articles / videos. Afterwards this information should be pretty straightforward as it is the same concept.

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

In problem F you can eliminate the flag l by simply not changing the value of k once k hits |s|, this is how you would construct a Deterministic Finite Automaton which checks whether some string s appears at least once in the input string — you simply make the outgoing edges at vertex |s| go back to |s|. The code is also somewhat simpler.

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

Bad time limit of problem E2, my O(N^3) solution easily passed in TL.

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

Why is this code giving tle? My approach is same as the one mentioned in the tutorial!! https://codeforces.me/contest/1015/submission/71619845

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

    Mine also !! [https://codeforces.me/contest/1015/submission/74198722]

    Did you find the error?

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

can anyone explain what this lines does in the code

ifdef _DEBUG freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

endif

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

    It is for getting input from input.txt, which is a file that should be present in your current directory. You can paste the input there and your program will read input from this file. Same goes for the output.txt.

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

Help... Why this F solution is wrong : https://codeforces.me/contest/1015/submission/95085826

Idea : Let's say our final string would be A where at least 1 substring of S is present. Fix the starting position of the substring S, X = current starting position of S, Y = current end of S. In our dp, if pos < X || pos > y, that means we can choose this position to be either "(" or ")", so we call function for both of them. When pos >= X && pos <= Y, we don't have a choice, we have fixed that length for S, so we call function according to what we have in S. For each possible X (starting position can be 0,1,2...) do a new dp, and increase our answer.

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

@vovuh I am facing an issue with Problem F, my approach is quite same as in editorial. I made a dp[i[j][k][f]

where 'i' is the number of characters, I have placed till far and j shows the index of string s till which it is been place, k is the score of current bracket sequence "+1 for ( and -1 for )" and f shows whether string s is placed or not. So, my dp has following transitions: First I will put some ( and ) brackets and my j=0 and f=0 indicates that I haven't placed s till now. when f=1 it shows that currently I'm placing string s and my pointer j will increment in each step.

when j=len(s) f will become to 0 again

and i will not start placing s again if j=len(s)

in base case i will check whether j=len(s) and score=0

meanwhile if score<0 then i will return 0. Please look into it once. Thanks for your help. code for Problem_F

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

problem F can be solved by kmp automaton