yse's blog

By yse, 5 months ago, In English

Thanks for participating in the contest. I hope you enjoyed the problems :)

Rating Predictions
Rate the contest!
Rate the difficulty!

A — Koshary

Hint
Solution
Code (C++)
Rate the problem!

B — Party Monster

Hint
Solution
Code (C++)
Rate the problem!

C — Snowfall

Hint 1
Hint 2
Solution
Code (C++)
Rate the problem!

D — Palindromex

Hint 1
Hint 2
Solution
Code (C++)
Bonus
Rate the problem!

E — It All Went Sideways

Hint 1
Hint 2
Solution
Code (C++)
Rate the problem!

F — It Just Keeps Going Sideways

Thanks to chromate00 for suggesting a modification to problem E, which resulted in this problem.

Hint 1
Hint 2
Hint 3
Solution
Code (C++)
Rate the problem!

G — Drowning

Hint 1
Hint 2
Hint 3
Solution
Code (C++, using ordered set)
Code (C++, using Fenwick Tree and coordinate compression)
Rate the problem!

H — Fallen Leaves

Hint 1
Hint 2
Hint 3
Solution
Code (C++)
Rate the problem!
  • Vote: I like it
  • +84
  • Vote: I do not like it

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

how 11 days ago?

»
4 months ago, hide # |
 
Vote: I like it +7 Vote: I do not like it

The example note in C made me question reality for a moment...

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Fast editorial

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

Mann ..Stack saved me at the last minute in E

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

Anyone managed to solve H with some greedy simulation ?

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it +8 Vote: I do not like it

    I dont know can it be called greedy. I just did simple dp, like we are not deleting leaves, and rerooted it.

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

      I mostly meant simulating the process moving the leaves up by one and when they meet adding the pair, the problem is that sometimes its not optimal to remove a pair since one of the leaves might be the one we should eventually leave off.

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

        oh yeah! This is literally what I did. Addiotoinally if we leave the longest leaf without pair in our dp, it will work. We could delete not the optimal one, to avoid it, we just check for every root (reroot) (sorry for late answering)

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Thanks for the contest , I just participated out of speed-running first few problems.

Spoiler

btw C is a nice problem.

I looked other problems , They look interesting too.

Congrats on this successful contest .

»
4 months ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

wtf we have nearly identical solution even the naming convention for C this is my submission holy shit i hope i don't get banned lol.

373106639

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

    my sol almost exactly same bruh

    • »
      »
      »
      4 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it
      #include <bits/stdc++.h>
      using namespace std;
      
      #define endl '\n'
      #define all(x) (x).begin(), (x).end()
      
      using ll = int64_t;
      
      void test_case() {
          int n; cin >> n;
          vector<ll> a, b, c, d;
          vector<ll> ans(n);
          for (int i = 0; i < n; i++) {
              ll x; cin >> x;
              if (x % 6 == 0)
                  a.push_back(x);
              else if (x % 2 == 0)
                  b.push_back(x);
              else if (x % 3 == 0)
                  c.push_back(x);
              else
                  d.push_back(x);
          }    
          for (ll x : a)
              cout << x << ' ';
          for (ll x : b)
              cout << x << ' ';
          for (ll x : d)
              cout << x << ' ';
          for (ll x : c)
              cout << x << ' ';
          cout << endl; 
      }
      
      int main() {
          ios::sync_with_stdio(false);
          cin.tie(nullptr);
          int T; cin >> T;
          for (int i = 0; i < T; i++)
              test_case();
      }
      
    • »
      »
      »
      4 months ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      i hope we don't get reported then bruv!!

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

    ah hell nah

    My submission

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    dont be worry, as you can see its not a particularly difficult problem, so its just normal for ideas or coding styles to overlap (at least there many people do in that way).

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

Thanks for the insanely fast editorial, and for the good problems you have put up in the contest!

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

I'm genuinely impressed with problem G, how can one come up with that idea?

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

glad to know that there's solution without using Fenwick Tree/Segment Tree in problem F.

»
4 months ago, hide # |
Rev. 2  
Vote: I like it +2 Vote: I do not like it

who used complex Manacher algorithm for D or just dumb me :D

»
4 months ago, hide # |
Rev. 5  
Vote: I like it +11 Vote: I do not like it

Answer to the $$${Bonus}$$$ $$${Problem}$$$ $$${D:}$$$

My approach uses greedily finding the longest palindromic subsegments, but the concept of this approach works for the idea discussed in the Editorial as well.

Since there are $$${exactly}$$$ $$${2}$$$ occurrences of each element in the array, therefore the number of pairs which are equal and of the form $$$a_{i}=a_{j}$$$ is only $$${n}$$$.

Also, since the $$${positions}$$$ of each element is $$${fixed}$$$, therefore, for each $$${center}$$$, if you find palindrome with that center, there are $$${unique}$$$ set of palindromic pairs.

If we iterate on the array, and at the current index we try to find the palindromic pair of this element. If the palindromic pair is at an index $$${j \geq i+1}$$$, then we check for each pair $$${(i, j)}$$$ $$${,}$$$ $$${(i+1, j-1)}$$$ $$${,}$$$ $$${(i+2, j-2)}$$$ $$${,}$$$ $$${....}$$$ $$${,}$$$ $$${(i+k, j-m)}$$$, where $$${i+k \leq j-m}$$$. While checking we will also mark them as visited in a visited array.

If at any point we get to know that this selected subsegment is not palindromic we simply just increment $$${i}$$$ to the index where the palindromic condition first failed, i.e., $$${i=x}$$$, where $$${a[x] \neq a[y]}$$$ for some indices $$${x}$$$ and $$${y}$$$ during the checking of palindromic condition. After incrementing, check for other possible palindromic subsegments by repeating the process.

But, if we find that the selected subsegment was a palindrome, then we iterate on the visited array from $$${0}$$$ to $$${n}$$$, and store the maximum $$${mex}$$$ found yet. Then we increment the $$${i}$$$ to $$${j+1}$$$, where $$${j}$$$ was the index of the palindromic pair of the element at index $$${i}$$$.

We increment $$${i}$$$ to $$${j+1}$$$ because we have found the longest palindromic subsegments greedily, and since there are $$${exactly}$$$ $$${2}$$$ occurrences for each element in the array, therefore $$${any}$$$ subsegment of the palindromic subsegment $$${(i, j)}$$$ cannot be present in any other palindromic subsegment.

Hence, we only are iterating on the array just $$${once}$$$ and we visit each and every element just $$${once}$$$. Therefore, only $$${O(n)}$$$ operations are performed in greedily finding the longest palindromic subsegment.

Now, talking about the finding of $$${mex}$$$. Since $$${0}$$$ can be present in $$${at}$$$ $$${most}$$$ $$${2}$$$ palindromic subsegments, therefore, for all other palindromic subsegments the $$${mex}$$$ can be found in just $$${one}$$$ operation $$${( mex}$$$ would be zero in that case $$${)}$$$.

$$${At}$$$ $$${most}$$$ $$${2}$$$ subsegments will take $$${O(n)}$$$ time to find their $$${mex}$$$, therefore in total it takes $$${O(2*n-2+2*n)}$$$ $$${=}$$$ $$${O(n)}$$$.

Therefore, the total number of operations turn out to be $$${O(n) + O(n)}$$$ $$${=}$$$ $$${O(n)}$$$.

My submission uses the same idea.

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

can fentree be used in E like in F

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Problem F was a modification/extension of problem E. This is the first time I have seen this sort of "multi part" problem in a codeforces round despite it being quite common in national/international Olympiads. Personally, I find these sorts of problems quite enjoyable. Kudos to the problem authors.

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

for the bonus of problem D

I exactly solved the problem like that but for the proof my intuition told me this is correct <3

»
4 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

..

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

    It was a big mistake to tell people to check your solution; they hacked you And tbh, I wanted to hack you but wasn't fast enough

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

for the bonus of problem D

This fits the time limit because i got Accepted for same idea

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Instant tutorial, the contest was great, and the author is Egyptian!!!

I can't ask for a better thing, thank you, yse

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

Why is it not necessary to have at least one of $$$a_l$$$ or $$$a_r$$$ to be positive in a good subarray? (Problem G)

»
4 months ago, hide # |
Rev. 2  
Vote: I like it +9 Vote: I do not like it

instead of making suffix min array in problem E, we can simply track the max gain by maintaining two state variables...

My Code
»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

hmm... what if we want to maximize the number of subarrays that are divisible by 6 in problem C? And what if the number by which the product of the array numbers should be divided is arbitrary? It sounds interesting. Thanks for the contest.

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

    interesting

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

    To maximize the number of subarrays containing at least one occurrence of $$$x$$$, you can formulate the problem as follows:

    Let the number of occurrences of $$$x$$$ be $$$k$$$. These $$$k$$$ elements divide the array into $$$k+1$$$ segments of lengths $$$x_1, x_2, \dots, x_{k+1}$$$. The number of subarrays that include at least one $$$x$$$ is:

    $$$\sum_{1 \le i \lt j \le k+1} x_i x_j$$$

    This is a known optimization problem where we use the identity:

    $$$\left( \sum_{i=1}^{k+1} x_i \right)^2 = \sum_{i=1}^{k+1} x_i^2 + 2 \sum_{1 \le i \lt j \le k+1} x_i x_j$$$

    Since the total sum of elements $\sum_{i=1}^{k+1} x_i = n-k$ is constant, maximizing the sum of pairwise products $$$\sum_{1 \le i \lt j \le k+1} x_i x_j$$$ is equivalent to minimizing the sum of squares $$$\sum_{i=1}^{k+1} x_i^2$$$ which is minimized when the elements are distributed as evenly as possible ($$$x1 \approx x2 \approx x3 \approx \dots \approx x_{k+1}$$$).

    As for the divisors of $$$x$$$, i believe grouping them together in contiguous blocks to act as new singular elements is optimal though i couldn't prove it.

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

I realised the solution for E in the last 10 minutes but ran out of time :( Solved it 15 minutes later though :P

This was a fun first contest

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

contest was fun . feels like a div 3 contest. C was interesting for me. couldnt solve d and e but i saw e problem earlier contest. overall the contest was fun. i hope everyone was satisfied with contest

»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
Proof of Bonus D
»
4 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

In F I used difference array to track the sum of all position which contains height x, and after it I check how many blocks finally will be in right side, we can easily find their positional sum. and the answer without removing any block will be sum of all height's answer which is their final positional sum — initial sum.

void solve() {
	ll n;
	cin >> n;
	vector<ll>a(n);
	for (auto &it : a)cin >> it;

	vector<ll>b(n + 2);
	ordered_set st;
	for (int i = 0; i < n; i++) {
		b[1] += i + 1;
		st.insert(a[i]);
		b[a[i] + 1] -= i + 1;
	}

	for (int i = 1; i <= n + 1; i++) {
		b[i] += b[i - 1];
	}
	vector<ll>c(n, 1e8);
	ll mn = 1e8;
	for (int i = n - 1; i >= 0; i--) {
		mn = min(mn, a[i]);
		c[i] = mn;
	}

	ll sum = 0;
	for (int i = 1; i <= n; i++) {
		ll t = st.order_of_key(i - 1);

		ll tot = n * (n + 1) / 2;

		ll r = n - t;

		ll prev_sum = r * (r + 1) / 2;

		sum += tot - prev_sum - b[i];

	}

	ll mx = sum;

	for (int i = n - 1; i >= 0; i--) {
		ll t = st.order_of_key(a[i] - 1);

		mx = max(mx, sum + t - (n - i));

	}

	cout << mx << endl;

}
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

for the problem G isn't it just counting inversion with few more conditions, so can't it just be done using merge sort yse ?

»
4 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

I have Done D using Brute

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Suffix minimum idea in E is actually neat !

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

I can't code, can't find a job, can't have GF, what I can do god, Please save me.

Started CP at the age of 24 being a unemployed, who is aiming to become CMaster at CF.

Fingers crossed :X)

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

My apparch for F (Segment Trees)

Used a lazy segment tree to track, for each element, how much distance it can move based on how many columns to the right support its height.

While iterating right → left, updated this tree to maintain:

for each height h → count of future columns with ≥ h

and used it to compute total movement (ans).

Used a second segment tree to maintain frequency of heights (prefix counts).

This second tree is used to simulate removing one element, by counting:

how many elements (heights ≥ a[i]) gain +1 movement

(inversion-like counting).

Final answer:

base movement + best gain from removing one index

code in java:- https://codeforces.me/contest/2227/submission/373194545

»
4 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

D can be solved using binary search. We can find if ans is atleast x+1.in O(n)

If ans is atleast x+1 , means all ele 0 to x are present , thus get both the positions of x. Now 2 possiblities , Both x are present in our palindrome or Only one.

For both x case , we have both position of x , hence find its centre and expand palindrome as much as possible and check if all elements from 0 to x are present. For single x , do same , now centre is positon of x itself. (Check for Both positions of x)

Link -(https://codeforces.me/contest/2227/submission/373144878)

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

H is great, the edge-perspective idea is interesting.

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

I think my solution is the same as Bonus Problem D: 373133653, instead of brute-forcing all over the center, i did brute-force from the boundary.

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

    Since each element appears exactly twice. Each value can only be in at most 3 different situations (they can be the matching pairs, or they can be the center). If they are the matching pairs, they can only be visited exactly one times. If they are the center, they can be visited by other element at most 2 times. This gives us O(N) solution.

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

Can anyone explain the solution to the Bonus question of D ?

»
4 months ago, hide # |
Rev. 3  
Vote: I like it +2 Vote: I do not like it

By the way, G is solvable without having to use PBDS / segtree structures, which is helpful for easier implementation

If we twist the perspective a little bit from the provided solution, then pref[l-1] < pref[r] when l is odd, and pref[r] < pref[l-1] when l is even. Notice how in both cases, the smaller side (l-1 and r respectively) are even, and the larger sides are odd. And in each case, r appears later than l-1 and vice versa. Since l-1 and r are different parity, they cannot be equal and hence each case goes either in the 'smaller' or 'larger' category, which these two criteria fit exactly. This means that we can just search for the number of EVERY pref[#odd] (regardless if the index is larger or smaller than that of the even index) that are larger than each pref[#even]. Since this requires no element addition or subtraction, this can be done with sorting + binary search.

Implementation

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

E and F are amazing!

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

this was my first contest, letsgo

solved a and b, and almost solved c, didn't know how to sort numbers that aren't divisible directly by 6, gotta work on my math

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

what am i doing i tried to maximise the subarrays in C

»
4 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

the persons who use ai , their family all died already btw, an excellent contest

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

a similar problem after c?

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

an elegant solution for F :) 373205287

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

in problem D, why couldn't we check if there is a palindrom bitween the tow zeros ,then if there is we check the number on the tow sides of it to make it longer, otherwise we assume that every zero is the center of the palindrom and the answer will be the maximum mex of each of the palindrims? I hope you understand my quastion '_' ^_^ 2227D - Palindromex

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

i got braingasm after solving A and B

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Wow, this was my first contest. Solved problems A, B and C smoothly, struggled in D.

But a great experience overall!!!

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

can it be said for D given the constraints that two palindromic subarrays cannot intersect unless one is completely inside the other, and then we can do a brute force on all palindromic subarrays which cant be extended by adding one element on each side? the only exception i can think of is a subarray of the form abab which can still be handled within the code

»
4 months ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

My post contest discussion stream.
https://codeforces.me/blog/entry/153409
Youtube VOD

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

yse Why has my rating not updated for this round? Why does it show a delta of 0?

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

The problems are interesting.

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

guys i made it :DD

»
4 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

I think there's a typo in the "Hint 2" of problem E, it should be "The number of rows/cubes that stay in column i is exactly the suffix minimum s_i."

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

good contest

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

I used a Fenwick Tree for all of problems E, F and G.

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

SegmentTreeforces

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

in G what sort of intuition would lead one to guess that alternatingsum>0 is a sufficient condition?

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

I did problem F completly differently

If you look at $$$a[i]$$$ and some block at height $$$y \leq a[i]$$$, the number of times that that block contributes to the total motion is the number if $$$j \gt i$$$ such that $$$a[j] \lt y$$$. If we loop through the array backwards and mantain a histogram $$$h[x]$$$ of the number of times $$$a[j] = x$$$ for $$$j \gt i$$$, then the number of times a block at height $$$y$$$ contributes to the motion is

$$$h[y-1] + h[y-2] + h[y-3] + \dots$$$

$ Adding this up for all $$$y \leq a[i]$$$ ands, the contribution is

$$$h[a[i]-1] + 2h[a[i]-2] + 3h[a[i]-3] + \dots = \sum_{k=0}^{a[i]} (a[i] - k) h[k] = a[i]\sum_{k=0}^{a[i]} h[k] - \sum_{k=0}^{a[i]}kh[k]$$$

$ Finally we can mantain two fenwick trees representing sums of $$$h[k]$$$ and $$$kh[k]$$$ to get the answer. You can get the maximum increase from decrementing one $$$a[i]$$$ with the same method

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

KAN MikeMirzayanov

Cant we do anything about blatant cheaters?

We can easily spot cheaters in this contest.

For example, cookingDSA is clearly using AI solutions in this contest.

Are there going to be any updates in the near future regarding anti-cheat tools, etc.?

If not, we can float a blog where people can share their ideas so that cheaters will be reduced.

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

First time upsolved all the problems of a Div-3 round!! Problem H was most interesting and only painful problem for me.

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

How to solve H if the set S contains all vertices of the tree?

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

    the only way for a tree to be only leaves is if $$$n=1$$$ or $$$n=2$$$ but the problem gives $$$3 \leq n$$$ so you don't have to think about that

    • »
      »
      »
      4 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      i meant the case where S is defined to include all vertices of the tree, not just the leaves, this leads to a different problem

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

For problem H, there is also a sneaky dp solution which somehow passes without TLE

I basically try to root the tree at each non leaf node and then try to calculate the best possible cost for that root.

373383493

This solution TLEs if you try to root the tree also on leaf nodes

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

    Hacked, I used a tree that looks like this:

    Tree

    Vertex $$$1$$$ would be forced to sort its neighbors $$$O(n)$$$ times, making the complexity $$$O(n^2 \log n)$$$.

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

H is so confused

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

the solution for H is so confused you said If cnt[u], is even, all leaves inside this subtree can be paired with each other internally. This edge does not need to be crossed by any path. so what about the sons of vertex u get used already, then u become the new leaf ? and cnt[u] = (adj[u].size() == 1); is so confused , what ru cal for ??? obviously not cal for the num of leaves

you said: let cnt[u] be the total number of leaves in the subtree rooted at u but yoru cnt[1] equal to 3 on sample 1, obviously the num of leaves is 2 who can explain this for me plz!!!

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it

    root in 1 . if 1 sons of vertex u get used , another sons of this vertex u have been paired with this . every node (expect root) have 1 father , so if adj = 1 , we expect father node cause adj = 0 , this is leaf

    • »
      »
      »
      4 months ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      thank you! now i have solved the problem, i misunderstood the definition of the leaf node, , ans the cnt of nodes is only, no matter with the root, thanks a lot btw, i dont read this sentence of the problem : this set is determined from the original tree and does not change, my bad XD

»
4 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

I love problem G

»
4 months ago, hide # |
Rev. 5  
Vote: I like it 0 Vote: I do not like it

C

»
3 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it
if(~x) y = i;
else x = i;

this is so cryptic for a beginner

»
3 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

This is my first Codeforces contest where I finished upsolving all the problems. It was incredibly rewarding!

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

There is a Fenwick Tree solution for E?

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

hmm, i used a different solution for F, i used fenwick tree 381029740

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

This is a excellent implement for problem G.

Look at this: https://codeforces.me/contest/2227/submission/373099193

»
11 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can everyone help me fix my code, wrong answer in tc#2 :(((


template<class T> struct Segtree { int n; vector<T> t; Segtree() {} Segtree(int n_) { n = n_; t.assign(n * 2 + 6, 0); } void upd(int pos, int val) { for(pos = pos + n - 1; pos; pos /= 2) t[pos] += val; } ll get(int l, int r) { int left = 0, right = 0; for(l += n - 1, r += n - 1; l <= r; l /= 2, r /= 2) { if(l & 1) left += t[l++]; if(!(r & 1)) right += t[r--]; } return left + right; } }; // _____________________________ MAIN _____________________________ // const int nmax = 2e5 + 10; int a[nmax]; ll pref[nmax]; void solve() { // Complexity: O(n log n) int n; fin >> n; FOR(i, 1, n) (fin >> a[i]), pref[i] = pref[i - 1] + a[i]; Segtree<ll> sum(n); Segtree<int> cnt(n); FOR(i, 1, n) sum.upd(a[i], a[i]), cnt.upd(a[i], 1); int mn = n, suff = 0, ans = 0; FORD(i, n, 1) { sum.upd(a[i], -a[i]); cnt.upd(a[i], -1); int app = max(0ll, a[i] - mn); int cur = min(mn, a[i] - 1); int tmp = max(0LL, sum.get(cur + 1, n) - cur * cnt.get(cur + 1, n)) + suff - (a[i] > mn) + app; ckmax(ans, tmp); ckmin(mn, a[i]); suff += app; // dbg(ans, tmp, pref[i - 1] - cur * (i - 1)); } cout << ans << '\n'; // cerr << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define TASK "_" // freopen(TASK ".inp", "r", stdin); // freopen(TASK ".out", "w", stdout); int nTest; fin >> nTest; while(nTest--) { solve(); } }

Thanks!