wakanda-forever's blog

By wakanda-forever, history, 13 months ago, In English

2162A - Красивое среднее

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162B - Красивая строка

Idea: wakanda-forever Preparation: tridipta2806

Solution
Implementation

2162C - Красивый XOR

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162D - Красивая перестановка

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162E - Красивые палиндромы

Idea: wuhudsm Preparation: wakanda-forever

Solution
Implementation

2162F - Красивые отрезки

Idea: frostcat, wuhudsm, wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162G - Красивое дерево

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162H - Красивая задача

Idea: wuhudsm Preparation: wuhudsm

Spoiler
Solution
Implementation1(wuhudsm)
Implementation2 (Dominater069)
  • Vote: I like it
  • +69
  • Vote: I do not like it

| Write comment?
»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by wakanda-forever (previous revision, new revision, compare).

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

Fast Editorial! could have solved 'E' but couldn't know its failing tc :(

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

Thanks for problem F.

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

Please provide editorial of H.

»
13 months ago, hide # |
 
Vote: I like it -35 Vote: I do not like it

I feel like my code is correct. Can anybody help me to point out why it is wrong? ~~~~~ // this is code

include <bits/stdc++.h>

using namespace std;

int t, n; int a,b;

int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); cin >> t; for(int q=0;q<t;++q){ cin>>a>>b; if(b==0){ cout<<1<<"\n"<<"0\n"; continue; } if(a<b) { cout<<"-1\n"; continue; } if(a==b){ cout<<"0\n"; continue; } int kq=a^b; if(kq<=a) cout<<1<<"\n"<<kq<<"\n"; else{ int k1=kq^a; int k2=k1^b; cout<<2<<"\n"<<b<<" "<<a<<"\n"; } } return 0; } ~~~~~

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

D was really fun! I was so close to solving E but couldn’t find the bug. Anyway, it was a good problem set.

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

can E be solved using rolling hash to get dp[i] which returns true if [i..n] is palindromic?

»
13 months ago, hide # |
← Rev. 2  
Vote: I like it -17 Vote: I do not like it

what's wrong in my solution

#include <bits/stdc++.h>
using namespace std;

#define sz(x) (int)x.size()
void Solve(void) { 
  int n;
  cin >> n;
  string s; 
  cin >> s;
  vector<int>res; 

  for(int i = 0; i < n; ++i) {
    if(s[i] == '0') {
      res.push_back(i + 1);
    }
  }
  cout << sz(res) << endl;

  for(auto& i : res) {
    cout << i << ' ';
  }

  cout << endl;

}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int tc; cin >> tc;
  while(tc--) Solve();
  return 0;
}

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it -24 Vote: I do not like it

    Well for starters, your code simply removes all 0's when the problem states to find any non-decreasing subsequence whose removal leaves a palindrome. Also, your code never even checks to see if the removal leaves a palindrome.

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

EFG are such cool problems. Def one of the best div-3 this year for me

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

    Agreed, this div3 certainly's got high quality problems. I think it's a little bit close to Edu div2 in fact. Really cool tasks.

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

Any intuition behind problem E solution?

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

    my intuition was, i cannot do anything about palindromes occurring within the vector, what i can do is create a boundary beyond which no palindrome is possible, for that i need asymmetry at the border, that asymmetry can be constructed following approach in edu

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it
    Spoiler
  • »
    »
    13 months ago, hide # ^ |
    ← Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    my intuition for E was trying not to add number that can create palindrome of length 2 or length 3 (when we append a[x], try to make a[x] != a[x — 1] and a[x] != a[x — 2]). The code was:

    int v1 = a[x — 1], v2 = a[x — 2];

    int v3 = v1;

    while (v3 == v1 || v3 == v2) v3 = v3 % n + 1;

    But then i realized that my solution failed to this testcase : 4 1 4 3 2 3 (my code will output 4, which will make a palindrome of length 5). So to avoid this i will try to add number that hasn't appear in original array, then follow the above algorithm.

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

Auto comment: topic has been updated by wakanda-forever (previous revision, new revision, compare).

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

BRUH I GOT THE OBSERVATION IN E ABOUT REPEATING 3 NUMBERS BUT IDK HOW TO FIND THEM :((

»
13 months ago, hide # |
← Rev. 2  
Vote: I like it +3 Vote: I do not like it
Simple solution to G
  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    Seems we can't construct an answer for $$$n = 5$$$ with this method.

    $$$S = 2 + 3 + 4 + 5 = 14$$$. $$$d = 16 - 14 = 2$$$.

    I assume you mean $$$d \gt 2$$$ by "If $$$x \gt 2$$$". Since $$$d = 2$$$, we fall back to $$$d = 25 - 14 = 11$$$. But $$${3, 4, 5}$$$ can't add up to 11.

  • »
    »
    13 months ago, hide # ^ |
    ← Rev. 4  
    Vote: I like it +7 Vote: I do not like it

    It's a good solution indeed. Hopefully not just for myself, the crucial part feels a little vague.

    we can detach some nodes from $$$1$$$ and connect them to a greater node to increase the tree's value to $$$x^2$$$, we can instead from a sum $$$(x+1)^2$$$.

    My attempt to explain it more elaborately.

    1. Claim 1: $$$x$$$ being the smallest integer such that $$$x^2 \geq S$$$ . Then for all $$$n \geq 5 $$$ , we have $$$n \gt x$$$ and $$$ \lceil (n+1)/ \sqrt 2 \rceil \geq x $$$ .
    Proof

    Corollary: Taking $$$d = x^2 - S$$$, then $$$2 \sqrt S \gt d$$$ must be true.

    Proof

    Now, our initial tree is rooted at $$$1$$$, and all other nodes are directly connected to $$$1$$$ as leaves.  Now I want to rearrange the leaves such that my sum is increased by $$$d(=x^2-S)$$$. 

    Let's say we pluck the leaf $$$v$$$ and attach it to $$$u$$$. Then our new sum, $$$S_{\text{new}}$$$, is calculated as:

    $$$S_{\text{new}} = S - v + uv = S + v(u-1)$$$

    If the required increment satisfies $d \leq n$, we can choose the parent $$$u=2$$$ and the leaf $$$v=d$$$. Our job will be done in a single step. If $$$d \gt n$$$, we need a multi-step approach. We claim that we can always find $$$v_1$$$ and $$$v_2$$$ such that $$$n \geq v_1, v_2 \gt 2$$$ and their sum is $$$d$$$.

    One possible method for partitioning $$$d$$$ into two parts (given $$$d \gt n$$$):

    1. If $$$d$$$ is odd: Choose $$$v_1 = \lfloor d/2 \rfloor$$$ and $$$v_2 = \lfloor d/2 \rfloor + 1$$$.

    2. If $$$d$$$ is even: Choose $$$v_1 = d/2 - 1$$$ and $$$v_2 = d/2 + 1$$$.

    For $$$n \geq 5$$$, these vertices $$$v_1$$$ and $$$v_2$$$ will be $$$\geq 3$$$ in both cases.

    But if $$$d=1$$$ or $$$d=2$$$, we need to be a little careful.

    Case 1: If $$$d=2$$$, then $$$2 = x^2 - S$$$. We can rewrite this as $$$2 + 2x + 1 = (x+1)^2 - S $$$. Let's break the LHS into 2 parts, $$$3 + 2x$$$. We need to achieve a $$$2x$$$ and a $$$3$$$ increment. We can get that easily in 2 steps: plucking $$$x$$$ and attaching it to $$$3$$$ (which will give a $$$2x$$$ increment), and then joining $$$3$$$ to $$$2$$$ (which will give a $$$3$$$ increment).

    Case 2: If $$$d=1$$$, then $$$1 + 2x + 1 = (x+1)^2 - S$$$. Let's make the LHS into $$$2(x+1)$$$. Then we can always pluck $$$x+1$$$ and attach it to $$$3$$$, giving us a $$$2x+2$$$ increment. This is a legal move since $$$x+1 \leq n$$$ . 

    Submission link 344535551

    Edit1:

    Mistake: In calculating $$$x^2$$$ and in using the Ceiling property $$$\lceil x_1 \cdot x_2 \rceil \leq \lceil x_1 \rceil \cdot \lceil x_2 \rceil $$$, I mistakenly took the lower bound of this product when determining the upper bound. That was a stupid mistake.

    The following edits are made:

    1. In the Proof section, it is acceptable to take the lower bound when proving that $$$x$$$ indeed follows this inequality: $$$ \lceil (n+1)/ \sqrt 2 \rceil \geq x $$$.

    2. In the Corollary Proof, a much easier and tighter bound to prove is $$$2 \sqrt S \gt d$$$.

    3. Handling the case when $$$d \gt 2$$$: Since $$$d$$$ is the required increment, we can always find $$$v_1$$$ and $$$v_2$$$ such that $$$n \geq v_1,v_2 \gt 2$$$ and their sum is $$$d$$$.

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

I guess my O(n^3) solution for H that leaves less than 1MB memory free was not the intended one

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

my solution for problem c

#include <bits/stdc++.h>
using namespace std;

int main(){
    long long t;
    cin >> t;
    while(t--){
        int n,m;;
        cin >> n >> m;
        int x = 1;
        while(n >= x){
            x = x << 1;
        }
        x--;
        int x1 = n^x;
        int x2 = m^x;
        if(m > x){
            cout << -1 << '\n';
        }else{
            cout << 2 << '\n';
            cout << x1 << ' ' << x2 << '\n';
        }

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

How is checker implemented for E

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

In problem B, a solution always exists, then what is the point of:

"If no such subsequence exists, output −1"

»
13 months ago, hide # |
 
Vote: I like it -7 Vote: I do not like it

interactive D and construction EFG, definitely top5 scariest div3 OAT

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

For 2162G - Beautiful Tree I had a different construction, which works for $$$n \gt 5$$$. For smaller $$$n$$$ you can just brute force solutions by trying all trees, or hardcoding the solutions (the first four cases are already given in the sample input).

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

    For n=5, s is 14, s' is 16 so x=2, which falls in your last case, so for next s' you will try s'=25, but here x becomes 25-14=11, which is greater than 2*n (10), so that contradicts your explanation for the last case right? Although we can still make 25 using some arrangement.

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

Randomised solution for G:

Separate 1 from other vertices. Generate a random tree on vertices (I didn't even do some uniform distribution of trees, just generate the parent and then relabel the vertices randomly), and then try to connect 1 to some vertex.

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

    Very nice solution!

    To clarify, like implementation-wise, does this mean that you permute 2..n randomly, attach them into a star, then try to attach 1 somewhere?

    Also did you prove it or was it intuitively obvious that it would work within TL?

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

      I didn't attach 2..n in a star. I created a random tree where $$$parent_v \lt v$$$ and then relabeled the vertices. As for why it should work within TL I don't really have a proof (and my first implementation barely passed), but the amount we need to add for a number to be a perfect square is up to $$$2n$$$ and we can add up to $$$n$$$, so it seems like we only need a small number of checks

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

    Your solution actually gave me the intuition of how to get a deterministic solution, so thank you for that.

    We try to form some tree without 1 and add something from 2 to n using the edge with 1 to get to a perfect square.

    If we use a star tree with 2 in the center we get the sum 2*(3+n)(n-2)/2=n+n^2-6. It's greater than n^2 in most cases, so let's try (n+1)^2:

    (n+1)^2-n-n^2+6=n+7 > n, so let's try to increase the sum of the star tree by something >= 7, for example by connecting 5 to 4 instead of 2.

    Then the sum will increase by 5*4-5*2=10, so we'll need to add n+7-10=n-3. So the construction for n>=5 is:

    1 n-3

    2 3

    2 4

    2 i, 6 <= i <= n

    4 5

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

Auto comment: topic has been updated by wuhudsm (previous revision, new revision, compare).

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

all tasks were pretty cool, like D and E was so fun to solve read F too its pretty nice I will upsolve it

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

hi everyone, everyone let me ask about problem E. With test case 5, why is the answer I printed 1 4 5 worse than the answer of the question? I tried to count why when I combined the 2 arrays, I saw that both results were the same 10

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

First of all i appreciate the time and efforts made to make this contest, this was very good and easy contest. I am no one to argue for this small mistake. But i want to share something that i face.

In question B, which is now changed, took me more time only because of this test case explanation, i could have accepted this in under 2 min, but took me total 20 min with 2 WA, :(

In the fourth test case, we remove p=010 (indices 3, 4, 6), resulting in x=110011, which is a palindrome. which should be---- In the fourth test case, we remove p=01 (indices 3, 4), resulting in x=110011, which is a palindrome.

it took me so much time that this is a mistake from your side, because i thought above is written non dec , so how could 010 is removed, i was so frustrated.

But overall i am very happy to solve 5 problems , as all were very straight forward to me.

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

Too hard!

»
13 months ago, hide # |
 
Vote: I like it -7 Vote: I do not like it

D is rubbish.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
  int n,k; cin >> n >> k;
  vector<int> v(n);
  for(int i =0;i < n;i++) cin >> v[i];
  int ans = *max_element(v.begin(),v.end()) + 1;
  for(int i =0;i < k;i++){
	  cout << ans << " ";
	  ans++;
  }
  cout << endl;
}
signed main(){
	int tt = 1; cin >> tt;
	while(tt--){
		solve();
	}
	return 0;
}

Will this code for E pass, as the system testing is going on i cannot check this and i didn't want to see the editorial section.

Logic: So the question has asked us to minimize the number of palindromic subarray. So to do that we can just append the maximum element in the array + 1 to the end and then increase this variable by 1 every time so that it will not form any palindromic subarray with the previous ones.

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

An easier sol'n to C would be, if highest set bit of B exceeds A, cout -1. else set all the bit of A to 1 using its compliment then converting A to B by A xor compliment of B.

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

    C has another neat solution. If $$$ a \oplus b \le a $$$, we just print $$$ a \oplus b $$$. Else if $$$ b \le a $$$, print $$$ b $$$ then $$$ a $$$. (Otherwise its $$$ -1 $$$).

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

As a contestant, the problems were amazing!

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

Hi folks,

I tried solving E, but got WA on test 2(pretest 53) — solution

I tried finding out what's wrong, but no luck!

My solution:

If I just keep adding 'x' to the end, then it won't contribute to a new palindrome substring.

x: A number chosen such that it differs from the last two distinct numbers. I just keep track of the last two distinct numbers.

Example:

n=4 k=2

array = 2 2 3 3

last two distinct numbers — 2,3 -> so we can place 1 at the end => 2 2 3 3 1

last two distinct numbers — 1,3 -> so we can place 2 at the end => 2 2 3 3 1 2

Any help will be much appreciated!

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

OMG I'm such an idiot! I just realized we need to minimize palindromic SUBARRAYS, not SUBSEQUENCES!!! I've been struggling with this for a whole day now. Could anyone point me in the right direction? Any suggestions would be greatly appreciated!

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

Nice problems!

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

In fact, this was a very good competition, but I'm not very good at it, I don't understand why the output of index 0 in question B was fine (the first group in input 1 had 0 but there was no output), I don't quite understand, hope someone can help me explain!

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

    The output is: the length of the sequence of indices and the sequence of indices in the next line.

    If the length is 0 the sequence is empty, so we don't remove anything, and it's fine because in that case the string was already palindromic, and the empty sequence is considered non-decreasing as stated in the problem.

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

      I understand, but in the solution '010' from the problem, the first and second '0's in the code are placed into the pos array, yet they are not output when printed. I don't see any palindrome checking logic in the code either

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

        I see your confusion. So the samples are there to provide some valid answers, to help you understand the problem, but not necessarily the answers that would be generated by the author's solution, usually when there are multiple possible answers they pick something different to not give additional hints for the solution

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

The solution for problem G is amazing! I don't think I not have came up with that even if I had two more hours. For those who have solved the problem, would you mind sharing a little bit about your mindset while encountering this kind of construction problems? Thank you!

  • »
    »
    13 months ago, hide # ^ |
    ← Rev. 4  
    Vote: I like it +4 Vote: I do not like it

    Here was my thought process for G:

    • We are asked to find a valid construction... but for what $$$n$$$s is a construction even possible? - So we try a couple of sample cases around $$$n = 7-9$$$. In all of these cases a construction is possible... hm, perhaps it is always possible from $$$n = 3$$$ onwards? Let us try proving this hypothesis.
    • We use induction, setting $$$n = 3$$$ as our base case (in which the square is $$$k = 9$$$). From here, is there a method of transitioning to $$$n + 1$$$ nodes such that we maintain a square $$$k$$$?
    • After trying some random methods out, we find the iterative 'add-remove-add' method as specified in the official solution. This proves our hypothesis and solves the problem.

    Hope this helps!

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

Randomized solution for G

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

I learn a lot from F.

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

W contest, L cheaters

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

Just curious, why were the constraints on n, m so low for F? Is it because the complexity of the verifier is higher?

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

I wrote an O(1) solution for G

345123116

just using the observation that we can always make the sum of product equal to n^2 and starting from the tree where all are connected to 1. Let diff be the increase in sum required ie. n^2-(n(n+1)/2-1). Now adjust n to a place such that diff comes between n and 2n-1. now it can simply be achieved by one or two place changed. Ive proved that it will always work for n>5.

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

wuhudsm Is your implementation correct for this problem? Because it doesn’t print any binary string. Also, I think it’s a bit hard to understand why the second state is better. If I understand correctly, you’re saying:

In the first state, we get

  • $$$r[2]-l[0]+1-(r[0]-l[1]+1)-(r[1]-l[2]+1)$$$ indices of type 1,

  • $$$0$$$ indices of type 2, and

  • $$$(r[0]-l[1]+1)+(r[1]-l[2]+1)$$$ indices of type 3.

In the second state, we get

  • $$$r[2]-l[0]+1$$$ indices of type 1, and

  • $$$0$$$ of type 2 and type 3.

Therefore, we can say that it is always optimal to have fewer type 3 indices when we use the same number of indices for type 1 or type 2.

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

    Bruh, I just copied the wrong code. Fixed now.

    I think your understand is correct. Because $$$x+y$$$ indices of type 1 is always not worse than $$$x$$$ indices of type 1 and $$$y$$$ indices of type 3.

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

What's the reasoning for the smaller constraints on F? I wrote an O(n * m) solution for it without thinking of trying to optimize it due to the constraints, but after reading the editorial I'm wondering if this was done on purpose to allow these less optimal solutions to pass.

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

I have a weird solution for problem G: 347103995

I find the minimum x such that $$$S = 1 \cdot 2 + 2 \cdot 3 + $$$ ... $$$ + (n - 1)\cdot n \leq x^2$$$. Then I greedily increase $$$S$$$ until $$$S = x$$$. And somehow it worked xD

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

I really liked problem F. Thanks!

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

My dumbass cant think of using binary search in D..I dont know what to do now

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

im not really understand the G's solution, i am stucking at using an star graph (a graph where we pick 1 vertex as an center and make edges that link all other vertex to the picked vertex) to find the sum quickly but dont really sure how to construct a tree when that way fail, please help