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

Автор DeadlyCritic, история, 4 года назад, По-английски

A. FashionabLee :

Invented by DeadlyCritic.

Brief Solution
Complete Proof
Python solution
C++ solution

B. AccurateLee :

Invented by DeadlyCritic.

Brief Solution
Complete Proof
Python solution
C++ solution

C. RationalLee :

Invented by DeadlyCritic and adedalic.

Brief Solution
Complete Proof
Python solution
C++ solution

D. TediousLee :

Invented by DeadlyCritic.

Brief Solution
Complete Proof
Python solution
C++ solution

Challenge : Try solving problem D for $$$n \le 10^{18}$$$. (no matrix-multiplication)

E. DeadLee :

Invented by DeadlyCritic.

Hints
Brief Solution
Complete Proof
Python solution
C++ solution

F. BareLee :

Invented by DeadlyCritic and AS.82.

Brief Solution
Complete Proof
Python solution
C++ solution
Разбор задач Codeforces Round 652 (Div. 2)
  • Проголосовать: нравится
  • +402
  • Проголосовать: не нравится

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

I appreciate your effort, but this contest was very mediocre. Problemsetting might not be the job for you guys. It was basically Geometryforces, and I'm not good at geometry. Please, no more geometry problems.

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

    If you know you're bad at geometry, study up on geometry and stop blaming the author for using too many geometry problems.

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

    I'm not good at CP. Please no more CP problems. Only 1+1 problems.

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

    It sounds like "I don't know geometry, dp, binary search so don't make contests with such problems". Moreover, the only task there with geometry is A...

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

    O looks like a circle. All statements have at least 1 occurrence of the letter o in them. That's the most geometryforces round I've ever seen!

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

faast tutorial..

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

Really enjoyed the problemset!

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

In pD: I saw some people use max even under mod 1e9 + 7. Why is that correct.

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

Can someone help in knowing why B was so hard for me? I didn't struggle on C as much as I did in B. Please help me realize what's wrong in my practice and thought process.

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

    You should first work out a sound algorithm for every question and then you can code! I hope you use pen and paper to help visualize stuff?

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

D can be solved with matrix exponential as well. And one can make the constraint to be n <= 1e9 to force using fast matrix power.

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

    I am unable to understand this point. Could you pls share some resource to digest your point

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

    how will you handle the case if i%3==0 then dp[i]++

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

    Is this method same as what is used for finding Fibonacci numbers ??

    I think it can be done only if we have the relation:

    dp[i] = dp[i-2]*2 + dp[i-1]

    But we also have additional constraint:

    for every i divisible by 3, dp[i]+=4

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

    The best I could get is a $$$10\times 10$$$ matrix (84825762), which might TLE on $$$n\leq 10^{9}$$$ and probably TLE on $$$n\leq 10^{18}$$$, with $$$10^4$$$ testcases. Can it be done with a smaller matrix?

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

      A $$$4 \times 4$$$ matrix should work, but matrices doesn't help with solving the challenge, the intended solution works in $$$O(1)$$$ for each test case. (considering $$$x^y$$$ to be calculated in $$$O(1)$$$)

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

      Can you do it with a 5x5? You need just a linear combination of dp[n-1], dp[n-2], n%3==0, n%3==1, and n%3==2, in order to determine dp[n], right?

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

    T =
    0 1 0 dp[i-1]
    2 1 0 dp[i]
    0 0 1 1

    T3 =
    0 1 0 dp[i-1]
    2 1 1 dp[i]
    0 0 1 1

    T3 * T * T

    Hope this is clear enough :)

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

      maxwill Using your relation, I came up with a formula for when n%3==0,1,2 respectively, but there seems some mistake which I'm not able to figure out.

      [T_(n-1)]     T^(2*(n-3)/3) * T3^((n-3)/3) * [0]
      [T_(n)  ]=                                   [1]
      [1      ]                                    [1]
      

      The above applies when n%3==0, and similarly I've worked out other two cases, but idk why this starts giving wrong output from 9 onwards. Is there some mistake in this relation? How do I correct it. I've spent hours.

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

        (ABC)^n != A^n B^n C^n. And your T is ambiguous.

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

          T_n is the nth term. T and T3 are the matrices {{0,1,0},{2,1,0},{0,0,1}} and {{0,1,0},{2,1,1},{0,0,1}} Final answer of any n will be n*4. I haven't made any mistake in calculating power of the matrix, I've checked that function. I don't know what am I missing x

          UPD: I was doing a blunder in multiplication. Therefore, changed the matrix. Now, rather than using multiple 3X3 matrices, I am using one 4X4 matrix and finally got AC. 84893815.

          I still wonder, how do we solve it using two 3X3 matrices.__

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

            Your program is doing TTT*...*T3T3T3*x
            While the answer should be T3TT*...*T3TT*x

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

    That is a force online solution, which would've make it a bit much more harder :)

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

      I don't think so. Whoever can come up the recurrence AND knows matrix exponentiation will definitely be able to implement it quite quickly. In fact, I tried the mat expo first, only to notice later that it wasn't necessary. A little harder, yes. But still, well within the range, I believe.

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

WOW!! the editorial came fast!!

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

a great contest!

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

what's wrong with my solution?

DIV 2/C

solution link : https://codeforces.me/contest/1369/submission/84811103

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

    Check output for:

    1

    5 2

    7 3 2 1 1

    3 2

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится
    You have to move your L variable to another place. 
    1
    5 2
    1 3 8 13 17
    2 3
    
    Must be 39, but your code give 34.
    
    The optimal stategy is to take r-border as 17 and 13. left is 8 in first case, 3 and 1 in second case. And do it by this way.
    Read editorial more carefully, there is an explaination.
    
»
4 года назад, # |
Rev. 2   Проголосовать: нравится +90 Проголосовать: не нравится

Lee

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

How the fuck is the editorial blog uploaded 5 hours ago when the contest started 2.5 hours ago !!?

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

Did you really expect the contestant to prove C during the contest, or just do a Proof By AC? I mean I got 3 WA trying different greedy algos without proving them and I knew that almost everyone submitted without being completely sure of the proof.

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

    Lol is there something to prove? It was obvious for me

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

    I did proof it in $$$2$$$ minutes, but my English sucks so It became so long.

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

    I think the proof is quite intuitive. The author's explanation is too wordy.

    Notice that

    • We can break down the problem into finding the $$$\sum_i MAX(friend_i) + \sum_i MIN(friend_i)$$$

    • For some large $$$a_i$$$, it is easy to use it to maximize the $$$\sum_i MAX(friend_i)$$$ by giving it to a person with no gifts. This suggests for each person's initial gift, we should be handing out one gifts to each person in decreasing order

    • In order to maximize $$$\sum_i MIN(friend_i)$$$ as well, we notice that $$$MIN(friend_i) == MAX(friend_i)$$$ if $$$w_i = 1$$$. So, we reorder our gifts so people with $$$w_i = 1$$$ get the largest gifts.

    • Because of the way the MIN function works, it' doesn't make sense to give someone many gifts with high $$$a_i$$$, then one gift with low $$$a_i$$$. Similar to last point, it's more efficient to give the better gifts to friends with smaller $$$w_i$$$. This way, we burn less of the good gifts on people. This suggests that we should use a greedy solution to fill the remaining $$$w_i$$$ for each friend and we should fill the friends with smaller $$$w_i$$$ first, with the largest unused $$$a_i$$$.

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

      This is a garbage proof.

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

        The purpose of a proof is to convince someone that a solution is correct.

        Can I spend 10 more minutes to come up with a more rigorous proof? Probably. Do I want to spend 10 more minutes to do it during a contest? Probably not.

        And since we're being evaluated on our ability to code and not on our ability to prove our solutions, using non-rigorous proofs is the correct way to go as long as the proofs can convince OURSELVES that a solution is correct.

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

          This is not an unrigorous proof because it is not anything resembling a proof.

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

          But don't reply now, I have got no patience.

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

          In almost every wrong greedy you can come up with something to convince yourself. The purpose of the proof is NOT to convince, the purpose of the proof is to give 100% confidence. Convince seems a weaker word to me, I am usually convinced by every other wrong solution.

          I don't feel good about your proof. For example, why try to maximize MAX for each friend first? Can increasing MINs at the cost of some MAXs be better? Of course, the answer is NO. Simply, because here, the algorithm is indeed correct. But your proof is a bit too handwavy to address many of the critical issues.

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

        Ok, how about this proof? First, you sort your integer Array from largest to smallest and start distributing to friends from the front. also you sort your friends from smallest to biggest by w.

        the first priority friend is the friend with w = 1. because if you give to that friend, the happiness is boosted twice. After giving the integers to that friends with w=1, the second priority is maxing the MAX of the leftover friends. that way, you dont waste your big numbers between MAX and MIN of the friends. you distribute your numbers one by one to your friends and max their MAXes.

        then you need to start wasting your numbers, the way to do it is starting with friends with small w so you dont waste as much numbers.

        end?

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

      The first bullet point in your proof was the main thing I needed to realize to solve this problem.

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

Stucked at B for a while? Thinking what should i remove 1 or 0 in the middle part? Can someone gimme idea?

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

    The key intuition here is that you cannot change the leading 0s or trailing 1s. Anything in between, however, can be converted into a 0 in some way, so you don't need to remove anything. All you have to do is keep the leading 0s and trailing 1s, and if elements remain in the middle, add a 0 in between.

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

    the 0 is added is the 0 right before the trailing 1s in original string

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

    There were just certain observations that

    1.You can't change leading zeros and trailing 1s
    2.Anything in between can be transformed to 1 or 0

    Like *****110010**** — ****10010**** — ****1010**** — ****110**** — ****10**** — ****1**** or — ****0****

    3.But since we need lexico wise smallest we'll take 0
    4. Hence our answer becomes (leading 0s) + 0 + (trailing 1s)
»
4 года назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

An ideal contest with ideal timing and ideal problems! Liked it!

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

Fastest system testing I have ever seen.

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

The problems were LoveLee!

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

Wow. What an editorial. I honestly felt like I made some problems harder for myself than they actually were LOL

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

I will add implementations soon.

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

unrated

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

O(N + M) solution for E. We need to make the observation that as long as the degree of a node is not greater than the value of the node, it is always optimal to remove it as late as possible. Then we can run a process similar to topological sort to check if we can remove all edges.

Link: https://codeforces.me/contest/1369/submission/84803734

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
 
#define ll long long
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define readl(_s) getline(cin, (_s));
#define boost() cin.tie(0); cin.sync_with_stdio(0)
 
const int MN = 200005;
 
int n, m, cnt[MN], freq[MN], vis[MN], rem[MN];
pii a[MN];
vector<pii> adj[MN];
vector<int> v;
 
int main() {
    boost();
 
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> cnt[i];
    for (int i = 1; i <= m; i++) {
        cin >> a[i].f >> a[i].s;
        freq[a[i].f]++; freq[a[i].s]++;
        adj[a[i].f].push_back({a[i].s, i});
        adj[a[i].s].push_back({a[i].f, i});
    }
    queue<int> q;
    //for (int i = 1; i <= n; i++) printf("%d ", freq[i]);
    for (int i = 1; i <= n; i++) if (freq[i] <= cnt[i]) q.push(i);
    while (q.size()) {
        int cur = q.front(); q.pop();
        vis[cur] = 1;
        for (pii nxt : adj[cur]) {
            if (!rem[nxt.s]) {
                rem[nxt.s] = 1, freq[nxt.f]--; v.push_back(nxt.s);
                if (freq[nxt.f] <= cnt[nxt.f] && !vis[nxt.f]) q.push(nxt.f);
            }
        }
    }
    int mis = 0;
    for (int i = 1; i <= n; i++) if (!vis[i]) mis++;
    if (mis) return 0 * printf("DEAD\n");
    printf("ALIVE\n");
    for (int i = v.size() - 1; i >= 0; i--) printf("%d ", v[i]);
 
    return 0;
}
  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    Nice. :)

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

    this is clean

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

    Thanks for the solution. I have a doubt though. instead of checking !rem[nxt.s] why can't we just use !vis[nxt.f]. I tried it didn't work but couldn't understand why.

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

      Hopefully you see why with this case:

      2 1
      2 2
      1 2
      

      Edit: Sorry, this case does not disprove your point due to the weird way I checked the vis array. However, you must realize that edges should still be removed even if both nodes it connects are visited :)

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

        I got the case See the 5th test case. The logic that we need not remove an edge if both nodes are visited is perfectly alright. But what is happening is if we check only that we will be repeating operations for the same edges because queue may contain multiple entries of the same node. suppose you have three nodes between 1 and 2. Then if you start from 1, you will push 2 into the queue 3 times. Now when you iterate for 2 if you don't check the edges then you will loop 3 times for each edge. Hence you get an error. Btw, thanks for looking into it.

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

          Precisely. The duplicate entries are caused by the abnormal placement of the vis[cur] = 1 line, which in turn causes the error you mentioned. Please see the more accurate implementation here, where the case I mentioned would effectively break if you checked for !vis[nxt.s] instead.

          I guess the lesson to be learned here is to always implement as precisely as possible, otherwise debugging may become a lot harder :D

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

Thanks for the great quality questions! I really enjoyed solving it :) The difficulty gradient was so good for me ;)

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

Extremely well written tutorial,super easy to get

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

Special thanks for hints in problem E, actualLee they are better than the complete solution.

»
4 года назад, # |
Rev. 2   Проголосовать: нравится -20 Проголосовать: не нравится
    while(t-- > 0) {
        int n = sc.nextInt();
        String s = sc.next();
        int left = 0;
        int right = n - 1;
        while(left < n) {
            if(s.charAt(left) == '0') {
                left++;
            }
            else {
                break;
            }
        }

        while(right >= 0) {
            if(s.charAt(right) == '1') {
                right--;
            }
            else {
                break;
            }
        }
        String result = "";

        for(int i = 0; i < left; i++) {
            result += "0";
        }
        if(right + 1 != left) {
            result += "0";
        }
        for(int i = 0; i < n - right - 1; i++) {
            result += "1";
        }
        System.out.println(result);
    }

B. TLE on test case 9. How to optimise it?

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

    Try using StringBuilder instead of += for making large strings.

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

    Also, use an object new BufferedReader(new InputStreamReader(System.in)) to read the input instead of Scanner if there's lots of it. Also, use new PrintWriter(System.out) if outputing a lot of data.

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

Can someone please tell me why am i getting WA on pretest 2 in C : Link to code

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

    Because it is not optimal solution to give backpacks in line. Give first k maximal elements to every friend and then try to maximize the minimum elements for every friend as explained in editorial.

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

nice contest.

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

You need to multiply (i % 3 == 0 ? 1 : 0) part in D's brief solution by 4

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

Can anybody explain how we form the graph in problem D? I read the problem statement multiple times but couldn't figure out how they made the Level 4 graph. :(

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится
    1. Add one child to nodes with 0 children
    2. Add 2 children to those nodes which already have one child
    3. And if the node has already more than 1 child, we don't need to add any more child to it.

    So,3rd level graph is given int the question

    Nodes with no children -> 3,5,4 Add 1 child to these(8,10,9 respectively) , Nodes with 1 child -> 2 Add 2 children to it(7,6) ,

    No need to add children to 1 as it already has more than 2 children

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

Man. Div. 2 A's are getting harder and harder these days. Most people will probably intuit the right answer, but I usually prove before submitting, and some recent Div. 2 A's have very non-trivial proofs.

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

    It has quite a straightforward proof. We know that in an n sided polygon, each internal angle is (n-2)*180/n. To get this value, we basically divided the polygon into triangles, and there n-2 triangles in a polygon, and every triangle has 180 degrees, so there are (n-2)*180 degrees. Now, let's choose a point and think about every triangle originating from that point. Then, every angle from that point will be equal to (n-2)*180/(n*(n-2)) => 180/n. Now, for one side to be parallel to x axis and one side to y axis, a right angled triangle needs to exist. This means that there has to exist a pair of points such that the angle between them is 45degrees. So, we can basically form an equation like this: (180/n)*x = 45, where x is the distance between these two points. The x and to be an integer, so solving for x we get => (1/4)*n. This means, that n has to be a multiple of for an integer solution to exists. I know, proof is very messy, but hope this makes sense!

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

I solved D using normal 2D dp,

dp[i][1] = dp[i-1][0] + dp[i-1][0] + dp[i-1][1] and

dp[i][0] = max(dp[i-1][0],dp[i-1][1]) + max(dp[i-1][0],dp[i-1][1]) + max(dp[i-2][0],dp[i-2][1])

I created a DS to store quotient and remainder to compare modulo values

My solution: Link

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

Wow, I made solution to problem D that is different from editorial https://codeforces.me/contest/1369/submission/84820772 I used cnt[i] — count of added vertices in i level, and lol[i] — answer for i level

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

Very Good Contest and Fast Editorial

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

Here's an easier analysis for A. The exterior angle (diagram below) of a regular polygon of n sides is 360/n
In order for one side to align to the vertical and another side to the horizontal, the sum of the exterior angles of some k consecutive sides must equal 90.

We must have
(360/n) * k = 90
k = 90*n/360 = n/4
So n must be a multiple of 4 for such an integer k to exist.

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

    I came up with the same proof, but it was harder for me to write it in English. Thanks for doing it instead of me. :D

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

      My pleasure. I wish coming up with solutions quickly was easier for me than writing proofs :D

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

https://codeforces.me/contest/1369/submission/84793979

could someone help me with this submission.dont know why is this wrong, i guess its the same approach as in editorial

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

Great contest with perfect Timing and as well as excellent problem set.

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

Is there a greedy solution for D.TediousLee

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

I have done the exact same thing , why am i getting wrong ans verdict on c qn https://codeforces.me/contest/1369/submission/84794143

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

Feeling Very Sad,this is the first time i have solved 3 problems in div 2. But could not submit C because of the power supply went off for about 30 minutes.

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

DeadlyCritic In problem E, the last sample test case:

INPUT:  
4 10   
2 4 1 4  
3 2  
4 2  
4 1  
3 1  
4 1  
1 3  
3 2  
2 1  
3 1  
2 4  
OUTPUT:  
DEAD  

Suppose, following is the ordering in which Lee's friends eat[u -> v means (u-th friend eats v-th food)]:

9 -> 3
6 -> 1
4 -> 1
8 -> 2
7 -> 2
1 -> 2
10 -> 2
5 -> 4
3 -> 4
2 -> 4

My answer:

ALIVE
9 6 4 8 7 1 10 5 3 2
Why is this ordering wrong?

Please help!

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

    when friend i goes to kitchen, he tries to eat x_i AND y_i, even the x_i is exists

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

      so, the 9-th friend eats not just 3, but 1 too

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

      Oh, Okay! So, if both favourite foods of the friend exist, he/she will eat both of them? You mean this, right?

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

        It was clearly explained in the statements =(.

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

          I am sorry but I thought the same too ... Except for that it was a great problemset, looking forward to more contests by you

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

            We were expecting some people to thing like this, so we spent quite a long time to write the statements as clear as possible, I'm sorry that it was not as good as I expected.

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

i really don't see how the proof for problem D says that we should use i % 3 and that magic while computing the answer?

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

    Let the answer for RDB of level $$$i$$$ be $$$dp_i$$$ . Also an RDB of level i is connected to two RDB of level $$$i-2$$$ and one RDB of level $$$i-1$$$.

    1. Note that it is beneficial to ignore the root vertex of RDB of level $$$i$$$ if $$$dp_{i-2}$$$ requires the root to be included. Because, at the cost of adding one claw of $$$i-1$$$, we'll lose two claws, one for each RDB of level $$$i-2$$$.

    2. For $$$dp_{i-1}$$$, it doesn't matter whether we include the root or not because either way no extra claw gets added or removed.

    3. Also if both $$$dp_{i-1}$$$ and $$$dp_{i-2}$$$ doesn't require their root to be included to get their respective values, we'll add root of RDB of level $$$i$$$.

    Point 3 gives an idea on whether to include root or not in point 2. That it is if including the root doesn't matter in getting $$$dp_i$$$, we'll always not include it. Because in doing so, we may get a better answer for $$$dp_{i+2}$$$. Say we'll include root for $$$i$$$, then we can't (or rather shouldn't) include root for $$$i+1$$$ and $$$i+2$$$, but this implies we'll include root for $$$i+3$$$. In the question, we have to include root for $$$i = 3$$$, so we have to include root for $$$i=6,9,12,..$$$ as well. That is where that $$$i$$$ % $$$3$$$ comes into play.

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

Is there any way to see full test case, my code failed system testing in B but I can't figure out on which test case: https://codeforces.me/contest/1369/submission/84767454

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

Why this doesn't work for D? I find for every level how many more(new) vertices with 3 children were added from previous level and then do dp[i]=dp[i]+dp[i-2]. Solution: https://codeforces.me/contest/1369/submission/84822790

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

    You are saying you count new vertices but you are doing dp[i][2] = dp[i-1][2]+dp[i-1][1]. So it is getting added cumulatively.Also for ith layer you need to do dpp[i] = dp[i][2]+dpp[i-3]. You can see this after drawing some trees.

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

      Yea dp is adding cumulatively but dpp stores the added vertices as they come only from dp[i-1][1].

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

https://codeforces.me/contest/1369/submission/84819647 Can anyone tell me whats wrong with my C solution. First I sorted the interger lee has in descending order.Then I sorted w array in ascending order. Then I took the case for when w[i]=1. I gave them maximum value.Then I disitributed interger to W[i] one by one. It will be a great help if anyone call tell me my mistake

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

What's wrong with my solution ? Question B ; My solution : https://codeforces.me/contest/1369/submission/84814815

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

Why there are not the codes for the solutions in the editorial?

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

In D can someone explain why is 1 added if i is divisible by 3

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

    Because that gives an AC !

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

    Because when i%3 u can use the top claw (1;2;3;4)

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

      Oh damn, can you also explain how did you reach to this in the contest? What was your approach to the question?

      Thank you!

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

        Suppose you used the root for some x. Now, x will be a child of both x+1, and x+2 so, in either of them, you can't use the top claw (because it's child is x and it's root has been used already). Now, for x+3, it's children will be x+1, and x+2 whose roots are free and you can use the top claw in this case. It's a period of 3, just check where it starts.

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

        Here's my thought process in this problem:

        If you look at the case n=4 closely, you'll see that the tree RDB(4) is actually composed of a root node linking to 2 instances of RDB(2) and an instance of RDB(3).

        Now, because of this, we might start to think that a[i]=a[i-1]+2*a[i-2]. This formula holds true for n=5, but doesn't for n=6 if you check by hand. Why is this? It turns out that, again, checking by hand, in the case n=6 we can use the top claw.

        Again, we think about when it is possible to use the top claw. Clearly, when n=4 and n=5 we couldn't use the top claw because we did that when n=3. However, when n=6, we can use the top claw as we did not when n=4 and n=5.

        We see that we can use the top claw in the case n when we didn't in the case n-1 and n-2. Seeing as 3 is the first n to allow this, we conclude that we can use the top claw if n is divisible by 3.

        Therefore we have the above formula: a[i]=a[i-1]+2*a[i-2]+4*(i%3==0).

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

question B sucked my whole contest.

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

hi guys try these video solutions ->

Problem A

Problem B

Problem C

Problem D Tried to explain graphically how DP is working :)

Hope it Helps.. :)

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

I accidentally used Ideone on public settings and someone copied and submitted the same code int this contest and now i received a system email regarding the penalty for this. Can someone guide me as to what should i do. This is the link of my submissions https://ideone.com/qDpu3b

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

Nice way of writing editorial...!!! DeadlyCritic

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

Implementations are out, sorry Python solution for D is missing yet.

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

    Just volunteering:)

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

For pB I tried to use a "more greedy" approach, which consisted on going backwards on the string and once I had a zero I would starting deleting ones until the last one. Then I looped again though the string, starting from the first One and deleting all zeros that followed, except for the last, which was kept and the One deleted. It ended up being pretty messy, but it worked, even tough I didn't had time to submit it during the contest because of some stupid bugs. Here is my solution, you won't have a good time looking at code(it is pretty messy as I said), but here it is: 84823891

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

https://codeforces.me/contest/1369/submission/84825830 is there anyone who can tell me why I am getting Memory limit exceeded on test 2

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

for problem D,someone please tell me why this dp relation is wrong,i made two cases if we color root and if not, v[i]=max(((2*v[i-2])+v[i-1]),((4*v[i-4])+4*v[i-3]+v[i-2]+4))

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

Even after giving so many contests, I can't solve div2 C problems. I can solve them in upsolving but I can't solve them during contests. Can anyone please help me to overcome my this problem, please even single advice will help me. Btw here is the link to my upsolved div 2 C problem. I am expecting help from the community. Help me.

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

What's wrong with my solution? 84826635

I use a 3d dp to keep a count of vertices with 0, 1 and 3(the outer ones, as they will only contribute to the next state) children. Then I say that:

dp[0][i] = (2*dp[1][i-1]%MOD+dp[0][i-1]%MOD)%MOD;
dp[1][i] = (dp[0][i-1])%MOD;
dp[3][i] = (dp[1][i-1])%MOD;

I fill the dp till i = 2000002 using the above relation. And finally output (dp[3][n]*4)%MOD

There is some mistake in this logic. But I'm not able to figure out where and how to fix it?

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

    you can't take all claw. In n'th level if you have total m claw then you can take less then m claw. Because some claw's overlap with another claw. see my solution.

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

      Can you elucidate it a bit, please? I don't understand what is the use of calculating dp[i][3], in your solution? And how exactly are you working out states in sol[]?

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

        okay.in my solution here,

        dp[i][0] is the number of nodes which has 0 child in i'th level, dp[i][1] is the number of nodes which has 1 child in i'th level, dp[i][3] is the number of nodes which has 3 child in i'th level, at the same time dp[i][3] denotes total number of claws.

        in sol[] array I calculate the solution for n'th level in sol[n].

        in my solution,

        sol[n]= sol[n-3] + number of new claw added in n'th level * 4 .

        ** claws which were added newly in n-1 and n-2 th claw are overlapped.So i didn't count them.

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

      Can you provide a test case where this method would fail..?

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

I can't understand problem F. Why in this sample case

2
1 9
4 5

The output is

0 0

?

I thought, these scenarios is possible:

  • 1 -> Lee 2 -> Ice Bear 4 -> Lee 8 -> Ice Bear 16 (Lose) -> Next Round 4 -> Ice Bear 5 -> Lee 6 or 10 (Lose)

  • 1 -> Lee 2 -> Ice Bear 4 -> Lee 8 -> Ice Bear 9 -> Lee 18 (Lose) -> Next Round 4 -> Lee 5 -> Ice Bear 6 or 10 (Lose)

Hence Lee can win and can lose too.

Where did I go wrong? Or maybe, what does it mean by "can be the winner independent of Ice Bear's moves or not" and "can be the loser"?

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

    Here "can be the winner" means that Lee has a strategy where he will definitely win and Ice Bear can't prevent this. Similarly for "can be the loser".

    Indeed, the problem statement is a bit ambiguous.

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

How to solve D for n <= 10^18 without matrix multiplication?

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

https://codeforces.me/contest/1369/submission/84784718 O(N + M) solution for E. It's basically finding an ordering such that if you use that order to direct the edges, out_degree[i] <= a[i]. So you can code it in almost the same way as you would code finding a topological ordering.

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

hello sir, the editorial is awesome and one more thing you provided all code in python too. Thanks a lot sir

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

E question can be solved using data structures

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

Editorial with complete proof is good. It helps me with my poor math......

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

I have a better solution for D(which is close to phrasing the question in another way),

you can think of nodes in 3 — levels

level1 — it is just one node without any children (when it moves to level 2 it gives rise to 1 another node)

level2 — it is node with one child (when it moves to level3 it gives rise to 2 other nodes)

level3 — this node will be our matter of interest which won't give rise to any other node but it signifies atleast one claw(4 nodes)

you can compute level1 ,level2 ,level3 nodes for height-MAX_n using simple dp

level3[i] = (level3[i-1]+level2[i-1])%MOD

level2[i] = level1[i-1]

level1[i] = (level1[i-1] + level2[i-1]*2)%MOD

now precompute all answers for 1 <= i <= 2* 10^6,

which is easy to compute using simple observation, for some i , ans[i] = (ans[i-3] + level3[i] — level3[i-1])%MOD

which means the, if you include claws which changed from level2 to level3 during transition of height i-1 to i , you cannot include nodes which became level3 at height i-1 but you can add nodes which became level3 at height i-2 but not at i-3 ...so on (which is just ans[i-3])

now output ans[height]*4

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

[video editorial for third question] — (https://www.youtube.com/watch?v=5V-RiprjfJQ)

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

In the announcement of the Contest, Author should have written in the name of Lee as its heading.Lol!

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

For question C, can anyone please tell me why this solution is giving wrong answer and why the other one is being accepted. They both are doing the exact same thing.

Wrong Solution — https://codeforces.me/contest/1369/submission/84832565 Right Solution — https://codeforces.me/contest/1369/submission/84832850

Thank You!!

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

Video Solutions for A B C Hope u guys like it :)

Problem A:

Problem B:

Problem C:

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

Nice explanation for solution B.

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

In problem E, I still can't figure out why the situation where si>wi for all i has no solution after I read Complete Solution, could someone explain it in more detail?

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

    The easiest way to look at it is that in this situation you won't be able to fix the last friend in the permutation. Suppose we fix some friend as the last one in the permutation with food choices as (x,y) , now there will be 0 x left and 0 y left when it is his/her turn because sx-1 times people have eaten X before him and as sx>wx , hence we will have 0 'x' left and same argument for 'y' , so there cannot exist any last friend which implies there is not solution.

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

@DeadlyCritic Apparently the naive n^2 AC's in java for B: 84798072

I guess the moral of the story is make n 3e5 in the future :(

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

    This is not n^2. It's linear.

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

      ArrayList.remove() is O(n) and that is called O(n) times, so it is n^2.

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

        Yes but it depends from which index we are removing. We say it's O(n) because we need to shift the elements after the index of removal. But here we can clearly see that this person is removing either the last element of the list or 2nd last. So in the worst case time taken will be O(2n) not O(n^2). :)

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

          That's not true. If the string is 100000... then they are removing the second element each time, so it is n^2. You see this by running it locally on a 1 followed by 10^6-1 0s and it will take forever.

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

            Yes you test case is right. It will give TLE. Sorry!. but this is true best complexity for removal is O(1) when we remove from the last index. You can see this

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

              Yeah, I know, I'm a java main. My point is that this code is the n^2 and AC's anyway even though it shouldn't because the max n is too small.

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

    Interesting, thanks for letting me know.

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

Could you please in Problem F, elaborate how did you conclude that

$$$w_{s,e} = w_{s,\frac{e}{4}}$$$
  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Because whoever wins that game gets to play in (e / 4, e / 2], thus wins.

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

For problem D, consider the following dp: let a[n] be the answer if we color the root, and b[n] be the answer if we do NOT color the root. They obey the recurrence given in the code below. Then the solution is max(a[n], b[n]). But this does not coincide with the results using the code from the editorial. What am I missing?

int N = 20;
vector<ll> a(N+1); //use root
vector<ll> b(N+1); //do not use root
vector<ll> c(N+1); //editorial
a[2] = 4; b[2] = 0; c[2] = 4;
for(int i=3; i<=N; i++){
    a[i] = 2*b[i-2] + b[i-1] + 4;
    b[i] = 2*a[i-2] + a[i-1];
    c[i] = 2*c[i-2] + c[i-1] + (i%3==2) * 4;
    cout << i << " " << max(a[i], b[i]) << " " << a[i] << " " << b[i] << " " << c[i] << endl;
}
  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Because you are thinking $$$unrooted_n = rooted_{n-1} + 2*rooted_{n-2}$$$. This is not correct. $$$unrooted_{n-2}$$$ might be better than $$$rooted_{n-2}$$$. In other words, for $$$unrooted_n$$$, you have the capability to take the children rooted, but it is not mandatory. You are making it mandatory. This wrongful enforcing causes the problem.

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

Can anyone explain the reason behind adding (i%3==0) in problem D?
The explanation seemed a bit less explanatory! (and without any pictures)
I almost figured out the solution during the contest except this part :\

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

Can anyone please explain me the approach of problem D? Thanks :)

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

    If you start constructing the tree, you will find a pattern.
    The tree of level "i" will have a subtree of level "i-1" in the middle and 2 subtrees of level "i-2" on the 2 sides. The structure will be exactly the same as obtained for levels "i-1" and "i-2".
    Thus, dp[i] = dp[i-1] + 2*dp[i-2] + (i%3==0)
    The last part comes from the fact that after every 3 steps, the root claw becomes available to use.
    Credits for the explanation of the last step: anshumankr001

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

Small improvement of tutorial's C code. If you want to sort array in descending order, instead of sort and reverse (also you could write std::reverse), you can use sort(v.rbegin(), v.rend()) for vectors or sort(a, a+n, greater) for arrays

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

I solved problem D by keeping count of nodes with 0,1,3 children. The claws (nodes with 3 children) at level n-3 do not overlap with the bottom most claws at level n and can simply be added to the answer of level n . The bottom most claws will contribute 4 yellow nodes each. If we keep track of how many claws there are at level n we can find our answer.

For every n counts are updated as -

cnt_0[n] = cnt_0[n-1] + 2 * cnt_1[n-1]
cnt_1[n] = cnt_0[n-1]
cnt_3[n] = cnt_1[n-1]

ans[n] = cnt_3[n] * 4 + ans[n-3]

84837417

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

Four methods for Problem D:

Method 0. using dp as the Editorial given: $$$a_i = a_{i-1} + 2 \cdot a_{i-2} + (i \% 3 == 0?4:0)$$$ (use $$$a_i$$$ instead of $$$dp_i$$$ for short) Code: 84808226

there is a typing mistake in Brief Solution of D: $$$(i \% 3 == 0?4:0)$$$ instead of $$$(i \% 3 == 0?1:0)$$$ DeadlyCritic

Expand the above recurrence formula we have

$$$ \begin{pmatrix} a_{3n+2} \\ a_{3n+1} \\ a_{3n} \\ 1 \end{pmatrix} = \begin{pmatrix} 5& 6& 0& 12 \\ 3& 2& 0& 4 \\ 1& 2& 0& 4 \\ 0& 0& 0& 1 \end{pmatrix} \begin{pmatrix} a_{3n-1} \\ a_{3n-2} \\ a_{3n-3} \\ 1 \end{pmatrix} $$$

with $$$a_0 = a_1 = a_2 = 0$$$, and $$$A$$$ indicate the coefficient matrix.

Methods below are $$$O(\log n)$$$ time complex since size of $$$A$$$ is a constant.

Method 1. using fast matrix power we can get $$$a_{3 \lfloor \frac{n}{3} \rfloor + 2}, a_{3 \lfloor \frac{n}{3} \rfloor + 1},a_{3 \lfloor \frac{n}{3} \rfloor}$$$, and $$$a_{3 \lfloor \frac{n}{3} \rfloor + n \% 3}$$$ is the answer

Method 2. It is well known that If you know the characteristic polynomial of matrix, then you can use polynomial multiplication instead of matrix product to get $$$(a_{3n+2},a_{3n+1},a_{3n},1)^T$$$ which is faster that Method 1, especially when the size of $$$A$$$ becomes bigger. Best method in general as I know (can't use NFT, since neither size of A is big nor 1,000,000,007 is NFT-friendly.)

Method 3. Note that the special $$$A$$$ can be diagonalized, thus there exist an invetible matrix $$$X$$$ such that

$$$ X^{-1} AX = diag(8,1,0,-1) $$$

thus we have

$$$ X^{-1} \begin{pmatrix} a_{3n+2} \\ a_{3n+1} \\ a_{3n} \\ 1 \end{pmatrix} = (X^{-1}AX) X^{-1} \begin{pmatrix} a_{3n-1} \\ a_{3n-2} \\ a_{3n-3} \\ 1 \end{pmatrix} = (X^{-1}AX)^n \; X^{-1} \begin{pmatrix} a_{2} \\ a_{1} \\ a_{0} \\ 1 \end{pmatrix} $$$

We can calculate $$$X$$$ above by hand or using following SageMath Code:

A = matrix([[5,6,0,12],[3,2,0,4],[1,2,0,4],[0,0,0,1]])
A.eigenvalues()
X = A.eigenmatrix_right()[1]

Actually we can calculate $$$A^n$$$ with only 3-lines SageMath Code:

n = var('n')
A = matrix([[5,6,0,12],[3,2,0,4],[1,2,0,4],[0,0,0,1]])
A^n

and the last column of $$$A^n$$$ is

$$$ \begin{pmatrix} a_{3n+2} \\ a_{3n+1} \\ a_{3n} \\ 1 \end{pmatrix} = \begin{pmatrix} \frac{32}{21} \cdot 8^{n} - \frac{2}{3} \, \left(-1\right)^{n} - \frac{6}{7} \\ \frac{16}{21} \cdot 8^{n} + \frac{2}{3} \, \left(-1\right)^{n} - \frac{10}{7} \\ \frac{8}{21} \cdot 8^{n} - \frac{2}{3} \, \left(-1\right)^{n} + \frac{2}{7} \\ 1 \end{pmatrix} = \frac{1}{21} \begin{pmatrix} 2^{3n+5} - 14(-1)^{3n+2} \\ 2^{3n+4} - 14(-1)^{3n+1} \\ 2^{3n+3} - 14(-1)^{3n} \\ 0 \end{pmatrix} + \begin{pmatrix} -\frac{6}{7} \\ -\frac{10}{7} \\ \frac{2}{7} \\ 1 \end{pmatrix} $$$

Code: 84843466

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

    By using another approach based on the ideas in this blog, thinking of the +4 as impulses in the recurrence f(x+2) = f(x+1) + 2*f(x) we can end in a nicer formula using geometric sums: $$$\frac{4(\frac {1 - 8^{floor(N/3)}} {1-8} \cdot 2^{N mod 3 + 1} - \frac {1 - (-1)^{floor(N/3)}} {1-(-1)} \cdot (-1)^{N mod 3 + 1})}{3}$$$

    Code

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

    jianglyFans How did you come up with this matrix. can you please elaborate the steps. If the (i % 3 == 0) condition is not present than it is easy because we can have a 2*2 matrix. But when this condition comes into play how to construct that matrix? I struggle coming up with matrices for linear recurrence, is there a good resource to learn from?

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится
      • $$$a_{3n} = a_{3n-1} + 2 a_{3n-2} + 4$$$
      • $$$a_{3n+1} = a_{3n} + 2 a_{3n-1} = 3 a_{3n-1} + 2 a_{3n-2} +4$$$
      • $$$a_{3n+2} = a_{3n+1} + 2 a_{3n} = 5 a_{3n-1} + 6 a_{3n-2} + 12$$$

      and then write in matrix form

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

Well done! nice problemsetting.

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

Мой русский разбор на E (может кому-то будет интересно) https://telegra.ph/Razbor-zadachi-E652-Div2-06-23

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

Video Tutorial for Problem D Link

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

Problem E...If every time one friend can choose one favorite food to eat.How can solve it?

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

    That becomes a max flow problem. Source -> virtual vertex for edge -> actual vertices -> sink, edges in (vertices -> sink) have capacity a[i], there's a valid answer iff max flow is M.

    Though it would be interesting if someone found a faster solution, since if a[i] = 1 we can solve this variation of bipartite matching using a dsu.

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

      Oh,i know.Thanks.

      But if we need to find the order,is this problem solveable?

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

        In your version of the problem, the order doesn't matter. To find which food each one eats just check which edge in (virtual vertex for edge -> actual vertices) edges.

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

      I am a little bit confused about the flow graph.

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

        Yes, your graph is the reverse of mine so it looks correct. I didn't remember the story in the problem so "edge" is person and "vertex" is food.

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

Not sure if it's commented before, problem E can be solved in O(n+m) by bfs. The key is thinking the process in reversed order, though I came up with it after the contest :(. 84843536

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

Hi! Can somebody explain how to connect all of the individual answers in problem F?

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

    If I can choose to play firstly or secondly in one round,we consider the next round:

    • if there exists one wining strategy,now I can win the game directly;

    • otherwise I can leave the situation to the other player and I can win also.

    Obviously I can also choose to lose the game.

    If I can only play firstly or secondly,now I should update the f/s in one round and go on.

    Sorry for my bad English:)

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

Can problem E be solved using multiple instance bipartite maximum matching? If anyone solved it using this way please share.

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

2 2 1 1 1 2 1 2 Why does this sample have no solution for problem E?

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

Can anybody explain me why am i getting run time error in my solution for problem C — https://codeforces.me/contest/1369/submission/84814630

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

    Yes! In your comp function, change a>=b to a>b. I tried your code with this modification and it is working fine. A better alternative is to use sort(a, a+n, greater<int>()). You won't have to write comp function again.

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

      Yes it helped and solution got accepted.I wish I could have noticed it during the contest. Thank you so much. Although I wonder why was I getting run time error because of this. I mean how does it matter.

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

        Even I have no clue. I don't see any reason for different outputs. Can somebody please explain?

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

          C++ sort requires the comparison function to have strict weak ordering (https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings) — if comp(a,b) is true then it means a has to be placed before b. If it finds a situation where comp(a,b) is true and comp(b,a) is true it throws an exception as it can't find a valid way to handle that.

          If your comparison function does return equal values as true then it will often work for some values as not every values is compared.

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

            Can you give an example? I tried with an array {1,2,2,3,4,5,6,6,7} and it didn't throw an exception.

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

              It looks like I was slightly misremembering and there isn't an exception. It's just undefined behaviour and different compilers handle it differently. On the versions I've got locally the code below triggers an invalid comparator assert on MSVC and segmentation faults on g++ / clang++.

              #include <iostream>
              #include <algorithm>
              #include <vector>
              bool comp(long long int a, long long int b) { return a >= b; }
              int main() {
              
                std::vector<long long int> ll(32);
                std::sort(ll.begin(), ll.end(), comp);
                for (auto &i : ll) {
                  std::cerr << i << " ";
                }
                std::cerr << std::endl;
              }
              
»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Hi guys try this video solution of Problem D Here I have tried to Explain how DP is actually being applied graphically to this .. Hope it Help :)

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

problem D: I am not clear why will be add 1 when i%3==0 . Can anyone pls help.

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

pls can someone tell me the editorial of accuratelee in a easy way i m not able to understand it

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

Question no. 1

int main() {

ll t;cin>>t;
while(t--)
{
       ll n;cin>>n;
       double x=(1.0*360)/(n*1.0);
       double y=(1.0*90)/(x*1.0);

       ll z=y;
       if(abs(z-y)< double(0.00000000001))
       {
         cout<<"YES"<<"\n";
       }
        else
       cout<<"NO"<<"\n";
}

} **** Can some explain where I m getting wrong?

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

    Each exterior angle of a polygon is 360/n, where n is number of sides. Thus (360/n)*x = 90 must be satisfied, which gives the relation x=n/4. Hence if n is divisible by 4 answer will be YES else NO

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

    ll z = y rounds y down (at least for positive y), so if it's close to an integer, but due to rounding errors is slightly below, then z and y will differ roughly by 1 To check if a floating point is close to integer you can use abs(floor(x) — x) <= some epsilon (but of course you don't need it in this problem)

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

About D's analysis, I think the natural way is to enum the root's color and get $$$dp_i = max(dp_{i-1}+2*dp_{i-2},dp_{i-2}+4*dp_{i-3}+4*dp_{i-4}+4)$$$, so how to come up with the tutorial naturally?

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

In C can't we just sort in ascending order then give one max element and rest min elements to every friend(except those which need only one element in which case we give him just one max element)? What am i missing?

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

Pfff... lots of useless comments, newbies and pupils have made this comment section shit.

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

    You are only 108 rating above the upper bound of pupil so don't consider yourself to be isolated from them, You are also among them in some manner

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

      But atleast i give my effort, some guys are asking questions without even reading the editorial and also there are video tutorials available to these problems, why not just watch those videos before posting something like ""Can anyone explain problem A in simple word please?""

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

    Shut up u lil piece of shit

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

Кто-то может хотя бы вкратце объяснить Д но на русском пожалуйста?

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

Hi, Problem D, the expression (i % 3 == 2) is used in tutorial to add 4 new nodes to the answer. But how can we know this is the levels that should included in optimal solution? Could be another remainder, not? For example, (i % 3 == 1), or (i%3==0). Thank you.

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

    It is possible to add one more claw containing root if an only if for $$$i - 1$$$ and $$$i - 2$$$ it is not possible.

    Now we only need to directly check, that for $$$1$$$ and $$$2$$$ it's impossible and then use basic mathematical induction.

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

Brief Solution is really a good idea.

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

don't try to be an extra-ordinary by posting video solution of problems when solution is already posted by others(seniors)

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

If anyone need Detail Explanation(not Video Tutorial) For D Here

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

Your explanation is absolutely amazing.

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

Can someone please explain, how to solve E using graphs?

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

In ques E editorial Saying for all i, si>wi will lead to lee death is invalid test case- 3 3 1 1 1 1 3 1 2 2 3 here 1st friend can get 1st dish , 2nd can get 2nd and 3rd can get 3rd . But according to editorial lee will be dead. can anyone clarify this? thanks in advance

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

DeadlyCritic, is the given python solution for problem D correct ? the max of mods is being taken rather than the values..

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

https://codeforces.me/contest/1369/submission/85417639 could someone tell me what is wrong in the code I am not able to figure it out why it is coming that on test 7 alive is not printed

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

O(n) solution for 1369E — DeadLee 85817429

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

DIV2 C was a good greedy

My Solution :

We sort a in descending order and w in ascending order. The logic is we have to maximize Max+Min for every friend,so we first give the maximum elements for each friend.Now every friend got one integer and that is his maximum integer.We will remove all these integers from array "a".Now we have to give each person a minimum integer.

We have to maximize the minimum integer given to each friend.Now say a friend requires y more integers,and the minimum is say x,then there should be atleast y-1 integers greater than or equal to x in a.So we given each friend the yth greatest available integer.

The reason for sorting w is, say one friend requires y elements and another requires z elements, we give the yth greatest integer for the first friend and y+zth greatest for second friend, a[y]+a[y+z],say y>z,then sum will be a[z]+a[z+y],the farther the integer is lesser it will be,so we have to satisfy the friend with lesser w.

My solution : 87000341

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

DeadlyCritic In problem E , you have told that if $$$s_i<=w_i$$$ , then we need to keep that person in end , but what to do in other case ? We can't simply print that person , order does matter , so how to determine that order.For example in sample input 4 , there are more than 1 people for whom $$$s_i>w_i$$$ holds for both plates they want , so what to do in that case ?

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

    Are you asking this : "how to find a good order for foods"?, so we use a priority queue, and we check if the food with minimum $$$w[i]-s[i]$$$ is fine.

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

Here's an alternate solution for problem D: ` Let us define $$$dp_1[i]$$$ = maximum possible nodes we can colour yellow for an RBD of level i if the root node must also be coloured yellow and $$$dp_2[i]$$$ = maximum possible nodes we can colour yellow for an RBD of level $$$i$$$ if the root node must not be coloured yellow. Then, after making the observation that an RBD of level $$$i$$$ consists of a node connected to the roots of two RBD's of level $$$i - 2$$$ and one RBD of level $$$i - 1$$$, we get the following recursion relations : $$$dp_1[i] = 2dp_2[i - 2] + dp_2[i - 1]$$$ and $$$dp_2[i] = 2max(dp_1[i - 2], dp_2[i - 2]) + max(dp_1[i - 1], dp_2[i - 1])$$$. Then, the answer to each query is simply $$$max(dp_1[n], dp_2[n])$$$.

Here's my submission: 93394017

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

    lets say x = 1e9 + 8 , y = 1e9 + 6 then when you compare x%MOD and y%MOD (where MOD == 1e9+7) . it will give wrong ans , how come you are still getting correct answer ??

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

Know this is late, but I was able to get the dp transition for problem D by simplifying the graph. I realized that we only care about the claws, so I replaced the graph with a more compressed graph where each node is a claw, and an edge connects 2 nodes iff the claws shared an adjacent edge in the original graph. Then it's a lot easier to get the transition and find the pattern. I filled out 3 whole pages for this problem XD.

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

best editorial i have ever seen . i appreaciate for ur efforts. Thanks a lot