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

Автор Vladosiya, история, 2 года назад, По-русски

1931A - Восстановление маленькой строки

Идея: myav Разработка: myav

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

1931B - Сделай равными

Идея: MikeMirzayanov Разработка: Vladosiya

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

1931C - Снова сделай равными

Идея: ibraevdmitriy Разработка: ibraevdmitriy

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

1931D - Делимые пары

Идея: MikeMirzayanov Разработка: Vladosiya

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

1931E - Аня и подарок на День святого Валентина

Идея: Gornak40 Разработка: Gornak40

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

1931F - Скриншоты чата

Идея: ibraevdmitriy Разработка: ibraevdmitriy

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

1931G - Одномерный пазл

Идея: ibraevdmitriy Разработка: ibraevdmitriy

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

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

Can anyone explain why for Problem. F this submission gets TLE.

246345877

and this submission gets accepted?

246347175

I don't feel like there's any difference in logic, just the way of implementation is different.

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

A different way to do F:

  • Each element (after the 0th element in the kth list) can only be it's current position or its current position+1

  • Store both possibilities in a vector

  • Remove possibilities for elements when they're no longer possible (i.e. the current element's position in the kth list conflicts with the elements position in a previous list)

  • The answer is no if an element has no possibilities, or if its only possibility conflicts with another elements only possibility

  • Otherwise the answer is yes

Code: https://mirror.codeforces.com/contest/1931/submission/246250979

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

How was my solution hacked? https://mirror.codeforces.com/contest/1931/submission/246212609

I see it was probably due to collisions in the map but does that mean I need a better hashing function for pairs? If so can someone suggest one?

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

can somebody help me understand why this solution got TLE Problem D

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

Hey, I have submitted a java code for problem C but it gave me error on test case 3 whereas when i submitted the exact same code in c++ it passed all test cases why

java link: https://mirror.codeforces.com/contest/1931/submission/246230264

c++ link: https://mirror.codeforces.com/contest/1931/submission/246276255

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

In G we don't need to calculate factorials to $$$4 \times 10^6$$$ since the biggest factorial we need in combinations is actually $$$2 * max(c_{i}) \leq 2 \times 10^6$$$

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

i think something wrong in solution code of problem A ._.

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

Thanks for good Round. Good F

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

Why D's solution is correct? It uses a hash table (dict), so is it possible to hack this hash table?

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

Nice problems , had fun solving them

I really hope more rounds like this exist

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

why the code of problem A uses the string cur outside loop so I think this isn't true; please anyone can review it?

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

    It's correct because the first triples that it finds is guaranteed to be the minimum, so everything after that affecting the cur variable doesn't matter

    (That also means it doesn't need to continue the loop to find the minimum here and can return immediately when it finds the first triples)

    But I agree that appending to the cur variable after that is definitely unnecessary and the logic is also wrong IF the minimum is not the first triples lol)

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

I still don't understand anything in G, can someone please explain me

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

    This was my thought process when upsolving G.

    The main observation needed is to notice that 1 + 3 = 1 and 2 + 4 = 2, so we can condense all 3 and 4 pieces. However, it is possible to start the sequence with a 3 or 4 piece, so for this reason we can imagine having an "extra" 1 or 2, respectively. Now there are a few cases:

    • No correct puzzle sequence can have consecutive 1's or 2's, so if $$$ |c_1 - c_2| \gt 1 $$$, the answer is 0.

    • If $$$ c_1 = c_2 = 0 $$$, the answer is 1 if $$$ c_3 = 0 $$$ and/or $$$ c_4 = 0 $$$ and 0 otherwise. This is true because 3 cannot mesh with 4.

    • If $$$ c_1 = c_2 $$$ and $$$ c_1, c_2 \gt 0 $$$, either 1 or 2 can be used to start the puzzle sequence, so we can fix the first number and calculate the rest of the sequence. Counting the # of ways to condense $$$ c_3 $$$ 3 pieces into $$$ c_1 + 1 $$$ 1 pieces is the same as counting the # of ways to put $$$ c_3 $$$ indistinguishable balls into $$$ c_1 + 1 $$$ boxes, which is $$$ \binom{c_1 + c_3}{c_1} $$$ (this can be visualized with the stars and bars technique). Note that 1 is added to $$$ c_1 $$$ for that "extra" piece, if we fix 1 to be the first puzzle. As such, the answer is $$$ = \binom{c_1 + c_3 - 1}{c_1 - 1} \cdot \binom{c_2 + c_4}{c_2} + \binom{c_1 + c_3}{c_1} \cdot \binom{c_2 + c_4 - 1}{c_2 - 1} $$$

    • If $$$ c_1 = c_2 + 1 $$$ or $$$ c_1 + 1 = c_2 $$$, then we are required to fix either 1 or 2 to be the first piece, respectively. Then it's the same exact idea as the previous case

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

why my solution TLE on system test.
is unordered_map of unordered_map took significantly more time to find than unordered_map that pair as key?

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

Can some one explain the approach for problem G in detail?

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

Problem G:

Let the four types be denoted by $$$\color{purple}{b_1}$$$, $$$\color{blue}{b_2}$$$, $$$\color{olive}{b_3}$$$ and $$$\color{teal}{b_4}$$$ respectively.

Lemma 1: Between any two $$$\color{purple}{b_1}$$$, there must be at least one $$$\color{blue}{b_2}$$$. It is easy to see that one cannot fit only $$$\color{olive}{b_3}$$$ and/or $$$\color{teal}{b_4}$$$ between two $$$\color{purple}{b_1}$$$.

Lemma 2: Between any two $$$\color{blue}{b_2}$$$, there must be at least one $$$\color{purple}{b_1}$$$. It is easy to see that one cannot fit only $$$\color{olive}{b_3}$$$ and/or $$$\color{teal}{b_4}$$$ between two $$$\color{blue}{b_2}$$$.

It follows that between any two $$$\color{purple}{b_1}$$$, there must be exactly one $$$\color{blue}{b_2}$$$. Likewise, between any two $$$\color{blue}{b_2}$$$, there must be exactly one $$$\color{purple}{b_1}$$$. In other words, they must follow an alternating pattern $$$\color{purple}{b_1}, \color{blue}{b_2}, \color{purple}{b_1}, \color{blue}{b_2}, \ldots$$$ or $$$\color{blue}{b_2}, \color{purple}{b_1}, \color{blue}{b_2}, \color{purple}{b_1}, \ldots$$$, with possibly some $$$\color{olive}{b_3}$$$ and $$$\color{teal}{b_4}$$$ in between.

Lemma 3: If a block exists to the left of a $$$\color{olive}{b_3}$$$, it must either be another $$$\color{olive}{b_3}$$$ or a $$$\color{purple}{b_1}$$$. If a block exists to the right of a $$$\color{olive}{b_3}$$$, it must either be another $$$\color{olive}{b_3}$$$ or a $$$\color{blue}{b_2}$$$.

Lemma 4: If a block exists to the left of a $$$\color{teal}{b_4}$$$, it must either be another $$$\color{teal}{b_4}$$$ or a $$$\color{blue}{b_2}$$$. If a block exists to the right of a $$$\color{teal}{b_4}$$$, it must either be another $$$\color{teal}{b_4}$$$ or a $$$\color{purple}{b_1}$$$.

Putting everything together, any valid arrangement will be of the form

  • $$$\{\color{teal}{\{b_4, b_4, \ldots\}}, \color{purple}{b_1}, \color{olive}{\{b_3, b_3, \ldots\}}, \color{blue}{b_2}, \color{teal}{\{b_4, b_4, \ldots\}}, \color{purple}{b_1}, \color{olive}{\{b_3, b_3, \ldots\}}, \color{blue}{b_2}, \ldots\}$$$ or
  • $$$\{\color{olive}{\{b_3, b_3, \ldots\}}, \color{blue}{b_2}, \color{teal}{\{b_4, b_4, \ldots\}}, \color{purple}{b_1}, \color{olive}{\{b_3, b_3, \ldots\}}, \color{blue}{b_2}, \color{teal}{\{b_4, b_4, \ldots\}}, \color{purple}{b_1}, \ldots\}$$$

For such an arrangement to exist, it must hold that $$$\mathrm{abs}(c_1 - c_2) \leq 1$$$. Then, the number of valid arrangements is the number of ways to arrange $$$\color{olive}{b_3}$$$ and $$$\color{teal}{b_4}$$$ into the 'slots' between alternating $$$\color{purple}{b_1}$$$ and $$$\color{blue}{b_2}$$$. This can be done using the stars and bars technique.

Submission: 246362383

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

what are the prequesites for G . I didn't understand anything, and why am i still green and i hit the 1400 mark

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

For the problem F, I do understand why existence of topological sort (and therefore absence of cycles) of such graph is necessary condition for existence of the order. However, why is it sufficient? I.e. What is exact proof that absence of cycles guarantee that such order of chat participants exists?

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

    If there are no cycles in the directed graph that means that you can run the topological sort algorithm on it.

    The correct sequence will be the sequence after we topologically sort it as it will be satisfying all the conditions ie all the edges in the graph are not forming a cycle.

    Therefore it is enough to no if we can topologically sort the graph or no ie check for any directed cycles.

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

Video Solution for problemD : https://www.youtube.com/watch?v=FPRiSmmyfiE

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

Can someone please figure out what the mistake with my submission is? Any help is appreciated. https://mirror.codeforces.com/contest/1931/submission/246419122

  • »
    »
    2 года назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится
    	    sort(trailnums.begin(), trailnums.end(), [&](pair<long long int, long long int> a, pair<long long int, long long int> b) {
    	        return to_string(a.first).length() < to_string(b.first).length();
    	    });
    

    I think this part is the issue. You are sorting the numbers with trailing zeros by their absolute lengths in increasing order (instead of their numbers of trailing zeros in decreasing order). Seems like test #2 was only of integers from 1 to 10, thus this solution will be automatically be correct for just that case (but not when the integers are higher).

    For example: 4000000 should be prioritized more than 711100 since it has 6 trailing zeros (compared to 2 of 711100), but your sorting will make 711100 stand first.

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

      Hi AkiLotus, I also tried that approach before using the number of trailing zeros in decreasing order:

      Submission:https://mirror.codeforces.com/contest/1931/submission/246418992

      But even that does not seem to work.

      Appreciate your effort in looking into my code.

      Thanks

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

        I see now. The remtrail function isn't correct either, specifically this part.

            string ans = "";
            for(int i=0;i<n;i++) {
                if(s[i] == '0') break;
                else {
                    ans += s[i];
                }
            }
        

        Take a guess of what the output of remtrail(170011000) would be with this code. Correct answer is supposed to be 170011.

        Spoiler (open only when you figured out the bug)
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

From tutorial D: Since (ai + aj) mod x = 0, it follows that (ai mod x + aj mod x) mod x = 0. Since (ai − aj) mod y = 0, it follows that ai mod y − aj mod y = 0.

Quite not clear, how it follows, and why it's not (ai mod y − aj mod y) mod y = 0 for 2nd case then.

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

    0\ \le\ ai\ mod\ y\ \le\ y-1 \newline

    0\ \le\ aj\ mod\ y\ \le\ y-1 \newline

    -(y-1)\ \le\ (ai\ mod\ y − aj\ mod\ y)\ \le y-1 $$$

    So if $$$\ (ai\ mod\ y − aj\ mod\ y)\ mod\ y = 0$$$

    Then $$$\ (ai\ mod\ y − aj\ mod\ y) = 0$$$

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

Solution for D without hash:

We need to count number of pairs equal with respect to $$$(mod\ y)$$$ and sum of which equal to $$$0$$$ with respect to $$$(mod\ x)$$$.

For each $$$a[i]$$$ store pair $$$ \{ a[i] (mod\ y), a[i] (mod\ x) \} $$$. Sort array of pairs.

Now pairs can be divided into continuous blocks of numbers equal with respect to $$$(mod\ y)$$$. And inside each block elements are sorted by their $$$(mod\ x)$$$.

So inside each block we can count number of pairs which add up to $$$0$$$ with respect to $$$(mod\ x)$$$ using 2 pointers in $$$O(n)$$$.

Complexity of solution $$$O(n * log(n))$$$. (Because of sorting)

My submission 246421822

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

In Problem D,when i use unordered_map,i got TLE on test 30.But if use map,this test only need 70ms.Is this intentional design by the author?

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

    The unordered map uses a certain hashing algorithm and the author has put in testcases that cause collisions to it.

    You can read this for more information.

    So answering your question, yes it is intentional design by the author as the testcases are set in such a way.

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

why tle? D link

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

in G, why do we need this condition? It is guaranteed that the sum of ci for all test cases does not exceed 4⋅1000000.

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

Getting RUNTIME_ERROR (Exit code is -1073741819) on large inputs for this solution https://mirror.codeforces.com/contest/1931/submission/246676323 for Problem E. Does anyone know what am I doing wrong here?

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

    I tested your code and I think error occured when you sort vector b.

    This is an AC submission based on your code. 247704863

    I don't know what wrong on your code too (maybe Segment fault?).You can make some further study.

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

thanks for the problem E! really enjoyed solving it :)

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

Can G use dp?

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

In E can somebody explain why have we used ans -1 instead of ans.

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

    ans : the amount of digits that we have in final number.

    but how many digits does $$$10^m$$$ have ? It's $$$m+1$$$.

    So we need to check if ans >= m+1 ,ans it's the same as ans-1 >= m.

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

1931C can anyone tell me there is i>=1 that means first index of the array is not changeable in order to make the whole array equal . all the other elements of array needs to be equal as first index isn't it ?

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

Whenever i will be asked for quality,i would reply 1931F

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

Another approach for F

For every pair of screenshot with the 1st screenshot, iterate through elements of both the screenshots , except the first one and try to form the parent pattern from which it could have been made for these two users. If finally you could get a pattern n-1 times, you can say it is possible else not.

Only 3 cases would arise while iterating , either

  1. a[i] equals b[i] ,

  2. a[i] or/and b[i] equal to the beginning of screenshot i.e the user itself ,

  3. a[i] and b[i] being different and unequal to starting user.

Ofc , a is always the first row and b would change as the consecutive rows after that.

Have a look at my submission: 286579692

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

Another approach for F

For every pair of screenshot with the 1st screenshot, iterate through elements of both the screenshots , except the first one and try to form the parent pattern from which it could have been made for these two users. If finally you could get a pattern n-1 times, you can say it is possible else not.

Only 3 cases would arise while iterating , either

  1. a[i] equals b[i] ,

  2. a[i] or/and b[i] equal to the beginning of screenshot i.e the user itself ,

  3. a[i] and b[i] being different and unequal to starting user.

Ofc , a is always the first row and b would change as the consecutive rows after that.

Have a look at my submission: 286579692

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

Why is My submission wrong??

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
void solve(){
    int n,x,y;
    cin >> n >> x >> y;
    vector<int> a(n);
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    vector<int> mod_x = a;
    vector<int> mod_y = a;
    for(int i=0; i<n; i++){
        mod_x[i] %= x;
        mod_y[i] %= y;
    }
    for(int i=0; i<mod_x.size(); i++){
        if(mod_x[i]>(x/2)) mod_x[i] = mod_x[i]-x;
        //if(temp[i]==(x/2)&&(x%2==0)) ct++;
    }
    unordered_map<int,vector<int>> m;
    ll ans = 0;
    for(int i=0; i<n; i++){
        m[mod_y[i]].push_back(mod_x[i]);
    }
    for(auto it: m){
        vector<int> temp = it.second;
        ll ct1 = 0; ll ct2 =0;
        for(int i=0; i<temp.size(); i++){
            if(temp[i]==0) ct2++;
            if(temp[i]==(x/2)&&(x%2==0)) ct1++;
        }
        unordered_map<int, pair<int,int>> m1;
        for(int i=0; i<temp.size(); i++){
            if(temp[i]>0) m1[temp[i]].first++;
            else m1[abs(temp[i])].second++;
        }
        for(auto i: m1){
            ans += (i.second.first)*(i.second.second);
        }
        if(ct1>0) ans += (ct1*(ct1-1))/2 ;
        if(ct2>0) ans += (ct2*(ct2-1))/2 ;
    }
    cout << ans << endl;
}
 
int main() {
	// your code goes here
	
	int t;
	cin >> t;
	while(t--) solve();
}
»
4 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I guess problem E can be solved using simple observation and little greedy thinking like if we think in an Anna way where she will try to reduce the sum of length a[i] and Sasha will try to do opposite where she will try to minimize the Anna's work. I can explain that with an example...

4 10 1 2007 800 1580

so total digit is 11 => len(2007) = 4 Now, first move anna will make where she will try to maximize the reduction of digit so 800 => 008 => 8 Anna : 1 2007 8 1580 now total digit is 11-2 = 9 Sasha : 1 8 15802007 after this Anna cannot reduce the digit so Sasha will concatenate a[i] and a[j]

so Anna will win because total digit = 9 which is not > m

you can try other example too

355043878

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

Problem F can be solved with implementation: [submission:https://mirror.codeforces.com/contest/1931/submission/323661770] don't know why I didn't have the idea of topological sort tho