Блог пользователя wakanda-forever

Автор wakanda-forever, история, 11 месяцев назад, По-английски

2162A - Beautiful Average

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162B - Beautiful String

Idea: wakanda-forever Preparation: tridipta2806

Solution
Implementation

2162C - Beautiful XOR

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162D - Beautiful Permutation

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162E - Beautiful Palindromes

Idea: wuhudsm Preparation: wakanda-forever

Solution
Implementation

2162F - Beautiful Intervals

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

Solution
Implementation

2162G - Beautiful Tree

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Implementation

2162H - Beautiful Problem

Idea: wuhudsm Preparation: wuhudsm

Spoiler
Solution
Implementation1(wuhudsm)
Implementation2 (Dominater069)
Разбор задач Codeforces Round 1059 (Div. 3)
  • Проголосовать: нравится
  • +69
  • Проголосовать: не нравится

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

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

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

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

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

Fast Tutorial!!!!!

Nice Problem Set

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

Thanks for problem F.

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

Please provide editorial of H.

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

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; } ~~~~~

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

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

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

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

»
11 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится -17 Проголосовать: не нравится

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;
}

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

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

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

Any intuition behind problem E solution?

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

    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

  • »
    »
    11 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +5 Проголосовать: не нравится
    Spoiler
  • »
    »
    11 месяцев назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится

    If $$$a_i \neq a_{i-1} \text{ && } a_i \neq a_{i-2}$$$ for all i, then the array is guaranteed to not contains palindromes of length > 1. Proof: palindromes 2 and 3 do not exist by condition. Others palindromes contain palindromes 2 or 3.

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

    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.

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

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

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

Thanks for the quick editorial! As a participant, I found problem C to be a nice bit manipulation challenge

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

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

»
11 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится
Simple solution to G
  • »
    »
    11 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    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.

  • »
    »
    11 месяцев назад, скрыть # ^ |
    Rev. 4  
    Проголосовать: нравится +7 Проголосовать: не нравится

    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$$$.

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

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

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

    I'm getting wrong output / not working properly. Can someone please help me find the issue in my code?

    Here’s my code:

    #include <bits/stdc++.h>
    using namespace std;
    
    void solve() {
        long long n, k;
        cin >> n >> k;
        vector<int> v2(n);
        for (int i = 0; i < n; i++) cin >> v2[i];
        vector<int> v(3);
        map<int, int> mpp;
        int j = 2;
        for (int i = n - 1; i >= 0; i--) {
            if (mpp.find(v2[i]) == mpp.end()) {
                mpp[v2[i]]++;
                v[j--] = v2[i];
            }
            if (j == -1) break;
        }
        if (j >= 0) {
            for (int i = 1; i <= 3; i++) {
                if (mpp.find(i) == mpp.end()) {
                    v[j--] = i;
                    mpp[i]++;
                }
                if (j == -1) break;
            }
        }
        for (int i = 0; i < k; i++) cout << v[i % 3] << " ";
        cout << '\n';
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t;
        cin >> t;
        while (t--) solve();
    }
    

    Please tell me what’s wrong or how to fix it. Thanks!

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

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';
        }

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

    I thought about it too.

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

    could you please explain for me it from math perspectives?

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

      why not use only n^m?

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

        we cant directly use n^m because for cases where the MSB of m is less than n, n^m can be greater than n which will violate the codition

        Example n = 1000, m = 0001

        then xor will be 1001

        for this reason we can do it by first taking it to the largest possible value that is 1111 then going to 0001'

        in this way we will always be able to do it in 2 steps

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

      See in this problem as they have explained in the editorial it is not possible to convert a to b if b has a bigger MSB than a, otherwise it is always solvable.

      What is being done in this solution is that first we are finding the biggest number x that is possible which will be having all ones till the MSB of a. To find that we are first just finding the first power of two which is bigger than a and then when we subtract one from it we will turn the only bit of that power of two to zero and all other smaller bits to one.

      For example:

      Lets say a is 1010

      And b is 0100

      Now as b is smaller than 1111 it is clearly reachable

      So with the while loop we get 10000 which is the first square bigger than 1010

      Then we subtract one getting 1111.

      We did all this because we want to find a number x1 which on doing xor with 1010 will give us 1111 To find that we can now just simply xor 1010 with 1111 which will give us 0101 as the first number that we want to print.

      It is true because for three numbers x,y,z if x xor y equals z then you can interchange there positions in any way and it will always be true.

      So instead of doing a xor x1 to get 1111

      We did a xor 1111 to get x1

      Now from 1111 we have to get to 0100

      So essentially 1111 xor x2 equals 0100

      This can similarly also be done as 1111 xor 0100 to get x2.

      So x2 will be 1011

      And in this way x1 and x2 will be our solution.

      Hope this helped.

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

How is checker implemented for E

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

    I'm getting wrong output / not working properly. Can someone please help me find the issue in my code?

    Here’s my code:

    #include <bits/stdc++.h>
    using namespace std;
    
    void solve() {
        long long n, k;
        cin >> n >> k;
        vector<int> v2(n);
        for (int i = 0; i < n; i++) cin >> v2[i];
        vector<int> v(3);
        map<int, int> mpp;
        int j = 2;
        for (int i = n - 1; i >= 0; i--) {
            if (mpp.find(v2[i]) == mpp.end()) {
                mpp[v2[i]]++;
                v[j--] = v2[i];
            }
            if (j == -1) break;
        }
        if (j >= 0) {
            for (int i = 1; i <= 3; i++) {
                if (mpp.find(i) == mpp.end()) {
                    v[j--] = i;
                    mpp[i]++;
                }
                if (j == -1) break;
            }
        }
        for (int i = 0; i < k; i++) cout << v[i % 3] << " ";
        cout << '\n';
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int t;
        cin >> t;
        while (t--) solve();
    }
    

    Please tell me what’s wrong or how to fix it. Thanks!

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

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

"If no such subsequence exists, output −1"

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

interactive D and construction EFG, definitely top5 scariest div3 OAT

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

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
»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

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.

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

    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?

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

      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

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

    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

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

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

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

not my dumb ahh used binary search to find l and r and went to sleep

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

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

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

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

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

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.

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

Too hard!

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

D is rubbish.

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

Second question can also be solve like this right?

  • Reverse the string
  • Find the LCS with the given string
  • Find the difference (note the changes in character)
  • Check if the p formed (the difference bits) is valid or not
  • Return the answer

I was trying this approach but it failed don't know why please give some advice

»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
#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.

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

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.

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

I could not implement solution of Beautiful strings due to Skill issue. (TT) I read the problem wrong everytime and interpreted the subsequece requirement to be contiguous. ALWAYS READ THE FINE PRINT THRICE!!

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

As a contestant, the problems were amazing!

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

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!

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

For problem C beautiful XOR, we can do the conversion in two steps max, if the msb of b and a are opposite, we can take the safe route and XOR a with a number which will result in all set bits as b excluding msb, and then XOR with log2(a) to set msb of a to zero, further if the msb bits are set, we can directly take the XOR a^b, it'll always be less than a.

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

Problem statements of A and B looked intimidating but the solutions are very simple.

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

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!

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

For Problem C, I used the relationships a ^ b = c and a ^ c = b. The solution runs in O(1) time per test case.

https://codeforces.me/contest/2162/submission/344334659

~~~~~~~~~~~~~~~~~~~~~~~~~~

include<bits/stdc++.h>

using namespace std;

int main() { int t; cin >> t;

while (t--)
{
    int a , b;
    cin >> a >> b;

    int x = a^b;

    if(x == 0) cout << 0 << endl;
    else if(a < b && x > a) cout << -1 << endl;
    else
    {
        if(x<=a)
        {
            cout << 1 << endl;
            cout << x << endl;
        }
        else 
        {
            cout << 2 << endl;
            cout << b << " " << a << endl;
        }

    }
}

return 0;

}

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

Nice problems!

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

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!

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

    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.

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

I'm getting wrong output / not working properly. Can someone please help me find the issue in my code?

Here’s my code:

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

void solve() {
    long long n, k;
    cin >> n >> k;
    vector<int> v2(n);
    for (int i = 0; i < n; i++) cin >> v2[i];
    vector<int> v(3);
    map<int, int> mpp;
    int j = 2;
    for (int i = n - 1; i >= 0; i--) {
        if (mpp.find(v2[i]) == mpp.end()) {
            mpp[v2[i]]++;
            v[j--] = v2[i];
        }
        if (j == -1) break;
    }
    if (j >= 0) {
        for (int i = 1; i <= 3; i++) {
            if (mpp.find(i) == mpp.end()) {
                v[j--] = i;
                mpp[i]++;
            }
            if (j == -1) break;
        }
    }
    for (int i = 0; i < k; i++) cout << v[i % 3] << " ";
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) solve();
}

Please tell me what’s wrong or how to fix it. Thanks!

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

C's solution is overcomplicated, probably because you wanted to keep it concise, B is formable from A in 2 operations max if it is possible

if MSB(B) > MSB(A) then obviously B cannot be formed from A -> -1, if MSB(B) == MSB(A) then we can just compare their bits and get the required x -> 1 operation if MSB(B) < MSB(A) then first we compare all the bits that are right of A's MSB, and in the 2nd operation we XOR the MSB of A as well -> 2 operations, and all of these x's are guaranteed to be less than A.

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

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!

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

    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!

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

Randomized solution for G

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

I learn a lot from F.

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

W contest, L cheaters

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

344611157 submission number. for the code of C, am really stuck and not able to understand where my code is going wrong

Basically what i am doing is, if the msb of b > msb of a , then printing -1, if they are same printing , 0, other wise creating a string of the integers, both of equal lenght (appending 0s to b if b is smaller by if statement thing), then the logic i am using pushing the indices where bits are not same into vectors, first the vector v, will store all idx of a that are not equal to b(where b[i] ==0), and the u vector will store all idx of a where they are unequal but here b[i] == 1), the logic behind it would be to xor a with all that power 2 where its bit is 1 but b's bit is not 1, and xor a again with 1 where b's bit is 1 but a's bit is not one, i am getting this error "wrong answer Integer parameter [name=x] equals to 4, violates the range [0, 0] (test case 1)" and no clue why, please help me out, sorry if the explanation might not be clear, im not very good at explaining things.

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

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

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

I think the time complexity for solution F is written wrong by mistake. It is written as O(n+m) per testcase but I think it is O(n * m) per testcase.

for (int i = 0; i < m; i++){
   cin >> l[i] >> r[i];
        st[l[i]] = 1;
        en[r[i]] = 1;
        for (int j = l[i]; j <= r[i]; j++){
            f[j]++;
        }
    }
}

See the above code it is running for O(m * Interval Size) in the worst case all the interval sizes are n so the effective time complexity becomes O(m * n).

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

Was wondering why pref max was needed for Dominator's solution of problem H, although we're doing something similar to open/close interval DP. Turns out it isnt

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

My solution for problem $$$E$$$:

  • In a string, if the substring $$$[l, r]$$$ is a palindrome, then the substring $$$[l + 1, r - 1]$$$ must also be a palindrome.
  • Lets try to solve the problem for $$$k = 1$$$. If the suffix $$$[l, n]$$$ is a palindrome, we must not push back $$$a_{l - 1}$$$, otherwise $$$[l - 1, n + 1]$$$ will now become a new palindrome.
  • Thus, keep a set of numbers from $$$1$$$ to $$$n$$$, and for every suffix $$$[l, n]$$$ that is a palindrome, erase $$$a_{l - 1}$$$ from the set. Also, we must not push back $$$a_n$$$ as well, so erase $$$a_n$$$ from the set.
  • Finding all suffixes that are palindromes can be done using Manacher's algorithm.
  • Lets take any remaining number in the set to push back. Let this number be $$$x$$$.
  • Then, the next number that we can push back can be any number in the range $$$[1, n] - {a_n, x}$$$ (since $$$[..., a_n, x, a_n]$$$ and $$$[..., a_n, x, x]$$$ will form palindromes).
  • Let the next number we push back be $$$y$$$.
  • Now to push back the remaining $$$k - 2$$$ numbers, we can choose any $$$k - 2$$$ numbers in $$$[1, n] - x, y$$$.
  • Since we did not form any new palindromes, there will not be any new palindromes forming due to these numbers either. Thus we have minimized the number of palindromes.

Submission

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

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.

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

I used the right logic for problem E, but was updating the array (adding at the end) instead of printing directly and printing the whole array at last. This solution passed all the pretests in the contest but showed memory limit exceeded afterwards in the final standing. How to know when the memory limit will get exceed and when it will not? because in this problem I was using O(n) space only, still it gave MLE.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void read_fast(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
}

int op(int l,int r){
    int sum=0;
    cout<<1<<' '<<l<<" "<<r <<endl;
    fflush(stdout);
    cin>>sum;

    return sum;
}

int fp(int l,int r){
    int sum=0;
    cout<<2<<' '<<l<<" "<<r<<endl;
    fflush(stdout);
    cin>>sum;

    return sum;
}

void ans(int l,int r){
    cout<<"!"<<' '<<l<<" "<<r<<endl;
    fflush(stdout);
    return;
}
bool check_same(int a,int b){
    return a==b;
}
bool check(int l,int r,int op_s,int fp_s){
    return op_s+(r-l+1)==fp_s;
}


void solve(){
    int len;
    cin>>len;
    int l=1,r=len;
    int spp_r=0,spp_l=0,mid=0,ans_l=0,ans_r=0;
    if (check(l,r,op(l,r),fp(l,r)))ans(l,r);
    int first=1;
    while (1){
        mid=(r-l)/2;
        mid+=1;
        if (first)
        {int temp_a=op(mid,r),temp_b=fp(mid,r),temp_c=op(l,mid),temp_d=fp(l,mid);
        if (!check(l,mid,temp_c,temp_d)&& !check(mid,r,temp_a,temp_b)&& !check_same(temp_a,temp_b)&& !check_same(temp_c,temp_d)){
            spp_r=r;
            spp_l=l;
            first=0;
            r=mid;
            continue;
        }}
        int temp_x=op(l,mid),temp_y=fp(l,mid);
        if (check(l,mid,temp_x,temp_y)){ans_l=l;if (spp_l!=0&&spp_r!=0){l=spp_l;r=spp_r;}} else r=mid;
        if (check_same(temp_x,temp_y))l=mid;
        if (check(mid,r,op(mid,r),fp(mid,r))){ans_r=r;if(ans_l)ans(ans_l,ans_r);return;}

    }



}


int main() {
    int t;
    cin>>t;
    while (t--)solve();
    return 0;
}

why i always get Idleness limit exceeded.I have used ffulsh for my cout.

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

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.

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

    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.

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

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.

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

Hi there, I still could not figure out how to come up with the solution for the D T T

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

Problem E:

If you will check only last 2 different values then this case might fail. t=1 n=5,k=1 input 3 1 2 2 1

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

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

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

I really liked problem F. Thanks!

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

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

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

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

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

разве в В задаче говориться что можно делать несколько подпоследовательности? то есть на моменте когда мы встречаем '1' мы заканчиваем нашу подпоследовательность, и потом когда мы встречаем 0 снова открываем?