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

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

Внезапно, все идеи задач этого контеста принадлежат мне. И я очень сильно прошу прощения за отсутствие одного маленького теста в задаче B. Это было самой крупной ошибкой, которую я допустил во время подготовки этого раунда.

1324A - Yet Another Tetris Problem

Разбор
Решение

1324B - Yet Another Palindrome Problem

Разбор
Решение

1324C - Frog Jumps

Разбор
Решение

1324D - Pair of Topics

Разбор
Решение

1324E - Sleeping Schedule

Разбор
Решение

1324F - Maximum White Subtree

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

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

Can someone suggest some basic dp problems like E (same or little higher difficulty) please.

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

E: A different dynamic programming approach, with runtime O(n*h).

Let dp(i,j) be the maximum number of good sleeping times if Vova slept the i to n times with the starting time j. Then, the value dp(1,0) will be the answer. Initially, all dp(i,j) = 0.

Transitions (to be done from i = n to i = 1, with j being all possible values of time (0-h)):

Let t1 = (j + a_i) % h and t2 = (j + a_i - 1) % h

Then, dp(i,j) = max(good(t1) + dp(i+1, t1), good(t2) + dp(i+1, t2))

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

Simpler way to solve C is to calculate the longest contiguous sequence of L's and then output it plus one.

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

Alternate solution to task E:

Considering all times of the day modulo h, let dp[i][j] be the maximum number of good sleeping times assuming that Vova has slept the first i times and that the current time of day is j.

Also, let's maintain boolean array pos[i][j] such that pos[i][j] is true IFF it is possible to end up at time j after sleeping i times.

Now, transitions can be calculated by setting dp[i+1][(j+ai)%h] to max(dp[i+1][(j+ai)%h, dp[i][j] + ((j+ai)%h >= l && (j+ai)%h <= r ? 1 ; 0) and applying a similar transition for ai-1.

Now, the answer is just the max dp[i][j] such that i = n.

I noticed a comment denoting a similar solution although my submission was in-contest unlike the other comment.

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

    i thought exactly the same but mine seems doesn't work

    can you please help me

    my submission

    Thanks in advance

    EDIT:

    Now it is working but why do we need to maintain a boolean array of possibilites?

    Shouldn't it be handled automatically by setting to zero the dp matrix initially

    please explain.

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

      During the contest, my intuition was thinking that not all (i,j) pairs would be possible during DP, so I included the boolean matrix as an extra precaution.

      I knew that the extra space wouldn't really matter and played it safe. IDK if it makes a difference but I wasn't worried in-contest because I wanted to make sure I just got the task right.

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

Can anyone suggest where can we study and practice rerooting question like F. Thanks in advance : )

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

What was the case that wasn’t included for B? I got hacked and I just wanted to know what the case was.

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

@vovuh I tried to solve F using two DFS but kept failing test case 3 and I couldn't figure out why it fails on this test case. Can you help me point out my mistake? My submission is 73111564 and I've added comments to make it easy to read.

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

A can also be done by checking if difference of adjacent elements mod 2 equals 0 or not

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

Can somebody explain why the above solution for D works? Once we sort all Ci there is no track of their original indexes anymore.. so how is it correct?

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

When will the ratings get updated... Generally how much time it takes after the contest ends .?

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

Question D can be solved in O(n) after sorting also using two pointers, which seems a bit efficient.

Link to my solution

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

Can somebody explain palindrome problem? How does it check the palindrome by removing characters in between?

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

    the question says we need a palindromic sub sequence of length greater than or equal to 3, So the easiest palindrome that can be formed by sub seq is of length 3 which has 1st and 3rd elements same, the 2nd element won't matter . Simply search for 2 same elements in the array but they should not be consecutive (so that you can insert any 1 element between the 2 similar elements).

    If the array has 2 same elements that don't occur together then the answer is "YES".

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

The solution given by vovuh for E is giving WA on tc 2. Any suggestions on what could be wrong in it?

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

    I copied the code from local directory, not from the polygon, the local code contained one bug which was fixed on polygon. You can realize what is this bug yourself (there is some kind of hint in the tutorial). And now you can copy and submit the correct solution but I don't know why do you need it.

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

this contest be like:

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

Can anyone explain O(N) solution for problem B ??!

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

Perhaps I am very poor at parity-trick questions, but problem A is very non-intuitive to me, and I was very shocked to see the number of submissions, problems B,C,D were much more straightforward for me.

I still don't think I understand problem A fully.

The answer is "YES" only if all $$$a_i$$$ have the same parity (i.e. all $$$a_i$$$ are odd or all $$$a_i$$$ are even). That's because placing the block doesn't change the parity of the element and the −1 operation changes the parity of all elements in the array.

OK, but this can prove that answer is "NO" when parity of any two numbers is different. But how do I prove that it has to be "YES" when all numbers have same parity?

It's somehow very non-intuitive to me :(

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

    The way Tetris(the game) works is the bottom most row gets cleared if there is a block present in every column. Since the block we can add spans 2 rows and 1 column, the height increases by 2 when we add it to a column. If the parity of each column is same then you can add those blocks to make the height of each column equal to, say, x. Therefore, you can clear the bottom-most row x times to get a fully empty board. Lets say your numbers are 1 3 5 9 3. Then you can add some number of blocks to each of the columns to get 9. After doing that, you can clear the whole board. Hope this helps :)

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

    When parity of all numbers is same, in one step you can increment the minimum element by 2 and then decrease all elements by 1. So the overall effect is that minimum element got increased by 1 and rest all elements were decreased by 1. Now it's easy to see why after a finite number of steps all elements in the array will become equal.

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

    I don't know it will help you this way or not but here's how I looked at it. Consider the cases when when all the adjacent numbers differ by an even counts(which means all the numbers have the same parity). Like 3 5 7 5 3 1 or 2 4 2 16 4. The only motto of the question is to make all numbers equal(Obviously, after that you can turn them all to zero).You start by adding +2 to the minimum number possible and then decrease the numbers till at least one of the numbers reaches to zero. Let's say p q r s t (numbers differs by an even count) and let's say t is the smallest number , you add +2 to t and let's say (for simplicity) t+2 is again the smallest number in the array so what happens if you actually perform operation 2nd described in the question — you subtract (t+2) to all numbers because t+2 is still the smallest number (so it's the first number that will be turned to zero) so the numbers become p-(t+2) q-(t+2) r-(t+2) s-(t+2) 0. Now you add +2 to zero and repeat the process and now it's easy to see that you can turn down them to same number if and if only they have same parity.

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

    Thank you all for your attempts. I think I have a somewhat better understanding now.

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

The solution provided for E is not running. I think the issue is that we are running inner loop from 0 to n which includes the case of dp(i,j) where j>i which is invalid. Please correct if I am wrong.

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

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

I have an interesting approach to question D,Time complexity: O(n).

You can define c[i] = a[i] — b[i],then sort them,finally while(l<r){ if(c[r]+c[l] > 0){ ans+=r-l; r--; } else { l++; } I think many people will use it to pass;

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

Where can I practice tree dp problems like https://codeforces.me/contest/1324/problem/F. Thnaks!

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

I never saw that kind of lengthy system testing. Did it happen like this before?

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

After 100% it went to 91% .WTH

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

My F question uses two DFS, the complexity should be no problem, why is it TLE?73153261

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

I tried the same approach with question D as given in tutorial, still i am getting a wrong ans. Can someone please spot where i might have gone wrong? It goes wrong in test case 12. Thanks in advance.

/* arr[i]= a[i]-b[i]; ->as in the tutorial

only that arr is sorted in DESCENDING order

and i have used a 2-pointer technique */

static int findPairs (Long arr[] , int n )

{ 
        int l = 0, r = n-1; 

        int result = 0; 

        while (l < r) 
        {                              
            if (arr[l] + arr[r] > 0L) 
            { 
                result += (r - l); 
                l++; 
            } 
            else
                r--; 
        } 
        return result; 
    }
»
7 лет назад, скрыть # |
 
Проголосовать: нравится -18 Проголосовать: не нравится

I think problem E can be solved by greedy Did anyone solved it by greedy ? I have tried , but i failed

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

Can someone help me find the error in my solution of E https://codeforces.me/contest/1324/submission/73208155 It gives wrong answer verdict on test 40

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

Someone please explain me the rerooting technique used in problem F. I am unable to understand it's editorial. Please help.

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

someone plz drope solution for A by using eazy c++

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

Why is this code of 1324D — Pair of Topics getting TLE?

Idea of code in short :- BIT / Fenwick tree is implemented using unordered_map (C++) to query smaller values, of type (b[ j ] — a[ j ]), than (a[ i ] — b[ i ]) for all j < i.

Any suggestions to optimize this code using the same idea?

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

where does my code goes wrong for PROBLEM E link of solution:-

solution here...

.thank in advance:)

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

1 1 2 2 3 3 4 4 5 5 14 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 6 15 15

the checker is saying this is a palindrome. can anyone explain how? this is test 5,1st problem

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

Is there any other DP solving for E problem?(thanks in advance)

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

Can anyone explains why my solution for the Problem-F is giving TLE. According to me its complexiety should be O(2*(n+v)) in worst case?

vector mv[200005];

int a[200005];

int dfs(int x,int p)

{

int newcost=0;

for(int i=0;i<mv[x].size();i++)

if(mv[x][i].first!=p)

if(mv[x][i].second!=-2)

{

    if(mv[x][i].second>0)

    newcost+=mv[x][i].second;

}

else

{

    mv[x][i].second=dfs(mv[x][i].first,x);

    if(mv[x][i].second>0)

    newcost+=mv[x][i].second;

}

if(a[x])

newcost++;

else

newcost--;

return newcost;

}

void solve()

{

int n;

cin>>n;

int flag=0;
for(int i=1;i<=n;i++)
{
    cin>>a[i];
    if(a[i]==1)
    flag=1;
}

for(int i=0;i<n-1;i++)
{
    int u,v;
    cin>>u>>v;
    mv[u].push_back(mp(v,-2));
    mv[v].pb(mp(u,-2));


}

if(flag==0)
{
    for(int i=0;i<n;i++)
    cout<<"-1 ";
    return;
}

int i=1;
while(i<=n)
{
    if(mv[i].size()==1)
    break;
    i++;
}
mv[i][0].second=dfs(mv[i][0].first,i);
for(int i=1;i<=n;i++)
{
    int newcost=0;
    for(int j=0;j<mv[i].size();j++)
    {
        if(mv[i][j].second==-2)
        {
            mv[i][j].second=dfs(mv[i][j].first,i);

        }
        if(mv[i][j].second>0)
        newcost+=mv[i][j].second;

    }
    if(a[i])
    newcost++;
    else
    newcost--;
    cout<<newcost<<" ";
}

}

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

This gives TLE : * Used Arrays A,B,C and 2 loops to input A and B and form C * Sorted C * This finally used lowerbound thing https://pastebin.com/icGaeuH8

This doesn't give TLE : * Used vectors A,B,C and 3 loops to input these * Sorted *Lower bount thing https://pastebin.com/9fajyGEi

Can anyone help me out ?

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

Tutorial Question E :

loop 1: i=0;i<n;i++ loop 2: j=0;**j<=n**;j++

-> i is number of sleeps -> j is number of sleeps he goes one hour earlier

So j <= i for all j Thus, j<=i rather than j<=n.

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

can anybody explain to me that in problem d -c[i]+1 logic? I didn't get it

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

    I hope you got c[i] + c[j] > 0. Now for this inequality , either c[i] must be positive , or c[j] or both. So we consider c[i] to be positive for all cases. Now c[j] has to be greater then -c[i] atleast to be 0 or positive. However c[i]+c[j] > 0 and not equal to zero and thus c[j] >= -c[i]+1.

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

For problem D, I still can't understand why we can skip it when c[i]<=0? Can someone help me?

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

Question (B) says that we can solve it by $$$O(n^2)$$$ or by $$$O(n)$$$. I cannot understand the writer's explanation of how we will do it in $$$O(n)$$$. Can anyone please provide code for the same. Thank you.

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

In problem D, doesn't sorting the c[] array mean losing information about i < j?

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

iam not able to understand F problem please help

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

Also, what does this even mean? The subtree of the tree is the connected subgraph of the given tree.

I feel the english is bad and it should be: A subtree of the tree is a connected subgraph of the given tree.

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

can somebody explain why in problem F the dp[v] = a[v] + max(0,dp[to]) as in subtree all nodes are included so it should be dp[v] = a[v] + dp[to] why we are taking max over 0 . can somebody explain 1st testcase how for node 1 ans is 2.

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

In E can anyone please point the mistake in dp equation :

  • if(j>i) dp[i][j]=0;
  • if(j==0) dp[i][j]=dp[i-1][j]+is((sum[i]-j)%h,l,r);
  • else dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+is((sum[i]-j)%h,l,r);
  • Logic is that one can sleep j times in first i sleeps only if he either sleeps early j times in first i-1 sleeps and not sleeps earlier in ith sleep or if he sleeps early j-1 times in first i-1 sleeps and sleeps early this time.
  • where dp[i][j] : maximum number of good sleeping times in first i sleeps, in which j were slept earlier
  • sum[i] : sum of sleeping times till first i sleeps
  • is(s,l,r) : returns true if s belongs in range [l,r]
  • And final answer is maximum in array dp[n][1:n]
  • If the logic is correct kindly help me debug my submission 78897404
»
6 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Sorry to bother you vovuh, but it seems like problem B is broken. Validator is crashing and solutions are not being evaluated. "Denial of Judgment" is being shown on evaluator

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

Can anyone tell me why this submission is going wrong on TC 12 for problem D? Submission Link. Its very odd, I see that the Editorial solution isn't too far off from my method (It uses binsearch while I use a ptr method).

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

Can anyone explain me why he took max(0,dp[child]) everywhere?

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

    Assuming you're talking about problem F, we don't want to add $$$dp[child] \lt 0$$$ to $$$dp[parent]$$$ since we want to maximize $$$dp[parent]$$$.

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

      mphillotry A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T. But doesnt the condition max(0, dp[child]) violate the above standard definition? Please reply.

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

        The tree is unrooted therefore descendants don't make sense in this context. The editorial only fixes the tree on some root to help solving the problem.

        Perhaps it wasn't the best choice to call them subtrees as confusions like yours may result, but the problem statement does give its own definition of a "subtree": the subtree of the tree is the connected subgraph of the given tree.

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

Why does this solution for Problem F get timed out ..The Time complexity is O(n).. I guess its just traversing the tree 3 times... (https://codeforces.me/contest/1324/submission/86646387)

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

If someone comes across this, can you please tell why is this solution failing at case 50.

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

Для задачи "E. Режим сна" более лаконичным является решение с конца (вычитаем часы начиная с последнего элемента входного массива). Получается тоже ДП, но не требуется совершать проверки "переходов из недостижимых состояний".

По итогу, выводим в качестве ответа значение 0-элемента (То есть, сколько будет хороших раз, если начинать с 0-часа).

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

In problem B, it says an array A is a palindrome if A[i] = A[n — i — 1] for all values i from 1 — n. However it should be A[i] = A[n — i + 1].

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

can anyone pls tell where my code fails for {1324B — Yet Another Palindrome Problem}176153984. vovuh

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

Can someone clear me up in the E problem why 1 24 0 0 3

this gives ans answer as 0 even though 1st sleep can give an optimal sleep