Vladosiya's blog

By Vladosiya, history, 3 months ago, translation, In English

1702A - Round Down the Price

Idea: MikeMirzayanov

Tutorial
Solution

1702B - Polycarp Writes a String from Memory

Idea: MikeMirzayanov

Tutorial
Solution

1702C - Train and Queries

Idea: MikeMirzayanov

Tutorial
Solution

1702D - Not a Cheap String

Idea: MikeMirzayanov

Tutorial
Solution

1702E - Split Into Two Sets

Idea: MikeMirzayanov

Tutorial
Solution

1702F - Equate Multisets

Idea: MikeMirzayanov

Tutorial
Solution

1702G1 - Passable Paths (easy version)

Idea: MikeMirzayanov

Tutorial
Solution

1702G2 - Passable Paths (hard version)

Idea: MikeMirzayanov

Tutorial
Solution
 
 
 
 
  • Vote: I like it
  • +74
  • Vote: I do not like it

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

Problem Solved

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

Sorry, can someone explain me, how to solve problem E with DSU and how these formulas works. I only realized that if two elements should be in different sets, the formula will be to unite(x, y + n), unite(y, x + n). And if in one then unite(x,y), unite (x + n, y + n)

  • »
    »
    3 months ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    Uniting dominos sharing the same number and checking if there is a set of odd size would suffice.

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

    As Editorial said, each number must exist exactly two times, i.e. every node has exactly two edges. Graphical representation of this will be just a set of disjoint cycles. Just use DSU to check if any of the cycles is odd lengthed.

  • »
    »
    3 months ago, # ^ |
      Vote: I like it +4 Vote: I do not like it
    0...n-1 ==> blue 
    n...2n-1 ==> red
    

    In bipartite graph each edge end (edge: x-y) should have different colors so (Blue, Red) or (Red, Blue). so we unite(x,y+n) or(x+n,y) In Bipartite odd cycle doesn't exist. since cycle must start with vertex x and end with vertex x. Lets say cycle starts with vertex x of red or vice versa and start assigning colors alternatively then if it is

    Even cycle ends with vertex x of red [ x(Red) -> y(Blue) -> x(Red) ]
    Odd cycle end with vertex x of blue [ x(Red) -> w(Blue) -> z(Red)-> x(Blue)]
    so if x(Red) and x(Blue) which is (x,x+n) belongs to same component then odd cycle exists. Hence answer is "NO".
    
    • »
      »
      »
      3 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      why can we add an edge between the two sides of the domino, because they must be in the same component?

      • »
        »
        »
        »
        3 months ago, # ^ |
        Rev. 2   Vote: I like it +1 Vote: I do not like it

        First, if any number appears more than $$$3$$$ times, print NO. For $$$n$$$ dominoes entered, it contains $$$2n$$$ numbers. According to Pigeonhole Principle, every number appears inevitably $$$2$$$ times.

        If we think of numbers as vertices (graph theory) and dominoes as edges (graph theory), the graph $$$G$$$ will be constructed by many cycle (graph theory).

        Consider two dominoes $$$a=\lbrace 1, 3 \rbrace$$$ and $$$b=\lbrace 1, 4 \rbrace$$$. Because they have a common number $$$1$$$, they must be placed in different sets. Without loss of generality, we can think of $$$a$$$ as the outgoing edge and $$$b$$$ as the incoming edge. It's like we dyed the edges in Red and Blue. two different colors. In a circle (graph theory's circle), bijection from edge to point can be constructed. So we can transform the edge dyeing problem into the vertex dyeing problem.

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

Video Solutions for the complete problemset.

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

Problem C had anti hash test cases got accepted during contest but TLE after the hacking phase :)

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

We can use priority queue to solve problem F yet the implementation is the same.

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

Can someone figure out which test case is giving wa https://codeforces.cc/contest/1702/submission/163618387

»
3 months ago, # |
Rev. 2   Vote: I like it -6 Vote: I do not like it

Analysis of the third problem: just to start, we need to store the start and end positions of each individual number. And then we compare if the first position of a[j] is less than the last position of b[j] then the answer is YES, otherwise the answer is NO:

ll n, k;
cin >> n >> k;

map < ll , ll > first;
map < ll , ll > last;

for (int i = 1; i <= n; i++) {
    ll u;
    cin >> u;

    if (first[u] == 0)
        first[u] = i;
    last[u] = i;
}

while (k--) {
    ll a, b;
    cin >> a >> b;

    if (first[a] == 0 || first[b] == 0) {
        cout << "NO\n";
        continue;
    }

    if (first[a] < last[b]) {
        cout << "YES\n";
    } else {
        cout << "NO\n";
    }
}
  • »
    »
    3 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    hey, I did the same but I used unordered_map and it showed tle but when I used map only got AC why?

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

      unordered_maps are ridiculously slow in C++, and can easily be hacked to TLE. Someone must have hacked an unordered_map solution, and that hack would have made it into the test cases. map on the other hand is kinda fast (sometimes even faster than unordered_map), and will give AC because the log(n) factor doesn't impact the solution much. use map, not unordered_map, or you will be hacked

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

        Or you can use unordered_map with a custom hash function that uses chrono::steady_clock::now().time_since_epoch().count()

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

        thanks

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

        thankx

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

        Yeah, initially I too used unordered maps/sets a lot and got unexpected TLEs which is never fun. Although, in some rare cases, we don't have log(n) space. Here is a custom hash that I use when using unordered maps/sets is the only way. Hope this helps.

        Custom Hash
        Usage
    • »
      »
      »
      3 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      the same thing happened with me

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

      use of unordered_map in codeforces is an offence.

»
3 months ago, # |
Rev. 7   Vote: I like it 0 Vote: I do not like it

.

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

Implemented Q3 as it is in the tutorial. But still getting TLE for Python Soulution. Any improvements?

https://codeforces.cc/contest/1702/submission/163751004

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

Problem F solved using 3 approaches:

Using Trie: https://codeforces.cc/contest/1702/submission/163755742

Using PriorityQueue+Editorial idea: https://codeforces.cc/contest/1702/submission/163692721

Storing counts of b prefixes: https://codeforces.cc/contest/1702/submission/163692721

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

    Please explain your Trie Solution like what is the intuition.

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

      Firstly, The initial is idea is same as editorial(make A and B array elements odd)

      Secondly, the trie stores the elements of array B in bitwise fashion starting with the Most significant digit at root to Least significant digit. Here, the Trie node stores pointers to two children 0, 1, parent node and count. The count indicates the number of such bits.

      Finally, when you are matching the bitwise pattern of A elements, if you find all the bits of A[i], you decrease the count of them, else the answer is No.

      If you can match all the A elements with the trie return Yes

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

I solved G2 using Heavy-light Decomposition during contest. After finding both ends of the path, we can do a lazy range addition of 1 on the path between them in the HLD. Now every node in the query must have the value 1. Again range add -1 to reset the HLD.

https://codeforces.cc/contest/1702/submission/163628298

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

    i used hld too, but i've added 1 to every node in the query and then calculated sum on path so, if it equals to k, the answer is yes, otherwise no

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

    Once you find the both ends of the path you actually don't need HLD though. If a node $$$w$$$ lies on the path between $$$u$$$ and $$$v$$$ then $$$dist(u,w)+dist(w,v)=dist(u,v)$$$ so just checking this for every node in each query gonna suffice.

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

Can someone explain the dfs function of Tutorial of G1? I understand that first we precalculate each node's depth and choose the deepest node as the start point of our dfs as it will be one of the end points of our path, but I can't understand the dfs working.Thanks...

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

    The idea like this: Denote the set of selected vertexes as S. And we already get the start point which has deepest depth and denote it as root. And in a DFS from the root, we say a vertex is a leaf if it is selected and none of its children vertexes is selected. If the selected vertexes can build a path, we should have only one leaf if we DFS from the root. So just calculate how many leaves we have. You can check whether this submit is easier to understand: https://codeforces.cc/contest/1702/submission/163945410

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

Problem E:

void solve(){
    int n;
    cin >> n;
    unordered_map<int,int>mp1,mp2;
    bool flag = true;
    rep(i,n){
        int a,b;
        cin >> a >> b;
        if(a==b)    flag =false;
        if(flag){
            if(mp1.find(a)==mp1.end() && mp1.find(b)==mp1.end()){
                mp1[a]=0, mp1[b]=0;
            }
            else if(mp2.find(a)==mp2.end() && mp2.find(b)==mp2.end()){
                mp2[a]=0, mp2[b]=0;
            }
            else flag = false;
        }
    }
    if(flag){
        py;
    }
    else    pn;
}

Which case is my solution missing??

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

    I have followed the similar approach using set and getting W/A

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

      I got it. In this case, it dosent work. 6 (1 3) (1 4) (2 5) (2 6) (3 6) (4 5) the answer is YES, but it prints NO.

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

        Thanx a ton dude, u saved my hours of cries!!!

        Just out of curiosity how did u find or guessed about the testcase? I was just not able to convince myself that my logic is wrong XD...

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

          Actually, it also confused me hours and I cant find the loical wrong at first. Finally, I casually found we shouldnt classify a pair to setA or setB if the two nums of the pair both didnt appear even once in setA or setB. Sometimes if a pair can both go to setA or setB, you shouldnt randomly put them in one of them. You have to skip and deal with them later. So the sequence to deal with the pairs is important. Which pair you should deal with first is the pair that only appeared once in both set. It will be put into the set that didnt inclued the same num and the classification is definitely not ramdomly. Hope it will help you :) I am not good at English:p

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

            makes complete sense, thanx again ^_^

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

    I used stl set and did the same things as you.W/A on test 2

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

    I got it. In this case, it dosent work. 6 (1 3) (1 4) (2 5) (2 6) (3 6) (4 5) the answer is YES, but it prints NO.

  • »
    »
    3 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    1
    6
    1 2
    2 3
    4 5
    3 4
    5 6
    6 1
    

    Your answer is NO.

    Now if we swap the order of input to:

    1
    6
    1 2
    2 3
    3 4
    4 5
    5 6
    6 1
    

    Then, now your answer is YES.

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

solved g1 later since I didn't have time to do in the contest. just bashed it with lca 163805347

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

Can somone please explain to me why when using unordered_map I get TLE and when changed to a normal map it just get accepted.

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

    Because a hash table should only store unique data, if there is more data that is repeated its complexity is O(n), you can search for it as collisions in hash tables

»
3 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it
»
3 months ago, # |
  Vote: I like it +1 Vote: I do not like it

I wanted to mention that F appeared in a recent AtCoder contest: https://atcoder.jp/contests/abc254/tasks/abc254_h

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

    Hey Edlue Can You help where my approach went wrong in problem F ? My basic idea was to reduce every number in array A and B to their largest odd factors (by dividing them 2 as long as possible) and store the values in two different multisets.
    First multiset contains all reduced values of array A.
    The second multiset contains all the reduced values present in array B. Then I would simply iterate over the second multiset and for each value in this multiset I will check if there is a matching value in first multiset.
    If yes then I will delete that value from first multiset else I will further reduce this element further by 2 and again try to match with first multiset as long as this element is > 0. >br> After iterating over all values of 2nd multiset, if first multiset is empty then my answer is YES, otherwise it is NO.

    UPDATE : I figured out the issue, which was due to continue statement in one of my while loops.
    My wrong submission here

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

Hello,

In problem E, is it obligatory that the two sets must have equal size. If yes, where is problem statement this is written?

Thank you :)

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

    There are $$$n$$$ dominos with value from $$$1$$$ to $$$n$$$.

    If each number appear exactly twice, both sets must contain all number from $$$1$$$ to $$$n$$$, thus have the size of $$$\frac{n}{2}$$$

    Edit : the size is $$$n$$$, not $$$\frac{n}{2}$$$, my bad

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

My solution to G2 with dfs and range query data structure(BIT for example):

First get the pre-order sequence of the tree, store the time stamp when you enter/exit each node.

For each query, find the node X with max depth, and node Y with min depth.

As described in the solution, X must be one end of the path. Let's enumerate the other end.

We put +1 on the timestamp you enter each node, and range_sum(X,Y) gives us the number of points covered by path X-Y.

Now we enumerate each node Z in the set, skip it if it's on path X-Y(can be determined by timestamp), otherwise see if range_sum(X,Y) + range_sum(Y,Z) = |S| + 1.

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

In problem F, why do we have to take the largest number from array b? The solutions works even if we take random number from b.

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

    I would assume that it would be easier to just start from the largest number. Do you have a proof as to why it would still work even if we just take a random number from b?

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

    I think you start with a larger number, because in the worst case that you divide it, you could still use it for smaller numbers. That's much more efficient than taking smaller numbers, because you don't know if you'll be able to use them again.

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

      tgp07 Can You help where my approach went wrong in problem F ? My basic idea was to reduce every number in array A and B to their largest odd factors (by dividing them 2 as long as possible) and store the values in two different multisets.
      First multiset contains all reduced values of array A.
      The second multiset contains all the reduced values present in array B. Then I would simply iterate over the second multiset and for each value in this multiset I will check if there is a matching value in first multiset.
      If yes then I will delete that value from first multiset else I will further reduce this element further by 2 and again try to match with first multiset as long as this element is > 0. >br> After iterating over all values of 2nd multiset, if first multiset is empty then my answer is YES, otherwise it is NO.

      UPDATE : I figured out the issue, which was due to continue statement in one of my while loops.
      The wrong submission here

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

Can u explain why Graph solution for Problem E works?

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

Can anyone provide me better and easy solution for the 1702B. Thank You...

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

    Hello sir.

    This is muy solution to 1702B.

    We can travel though the hole string, and use $$$ans$$$ to add up the sum of the days, and use $$$tot$$$ to add up how many letters Polycarp has remembered today.

    Another array $$$rem_x$$$ is used to mark if the letter is remebered.

    When $$$tot == 3$$$, this means he should use at least one more day, so we can ++ans.

    When a new letter is remembered today, he don't have to remember it again, otherwise, ++tot.

    Note that: please remember to clear $$$rem$$$ when '++ans'.

    My code:

    #include <bits/stdc++.h>
    typedef long long ll;
    using namespace std;
    inline int read()
    {
    	bool flag = true;
    	char c = getchar();
    	if (c == '-') flag = false;
    	while(!(c >= '0' && c <= '9')) c = getchar();
    	int x = 0;
    	while(c >= '0' && c <= '9')
    	{
    		x = x * 10 + c - '0';
    		c = getchar();
    	}
    	if (flag == true) return x;
    	return -x;
    }
    bool rem[27];
    int main()
    {
    	int t = read();
    	while (t--)
    	{
    		string s;
    		cin >> s;
    		int ans = 1, tot = 0;
    		memset(rem, 0, sizeof(rem));
    		for (int i = 0; i < s.size(); ++i)
    		{
    			if (rem[s[i] - 'a'] == 1) continue;
    			if (tot == 3)
    			{
    				memset(rem, 0, sizeof(rem));
    				++ans;
    				tot = 0;
    			}
    			++tot;
    			rem[s[i] - 'a'] = 1;
    		}
    		printf("%d\n", ans);
    	}
    	return 0;
    }
    
    
»
2 months ago, # |
  Vote: I like it -10 Vote: I do not like it

In the editorial of problem G why are we doing return go(now) + 1; ?

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

problem E,

after checking that no node with degree >= 3

there can be no paths in the graph, only cycles, correct?

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

Can someone please explain the reason why it is written in the editorial of problem E that each set will consist of n numbers ?

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

    here is proof by contradiction suppose 2 sets A, B form a valid partition && set A has < n numbers

    then we at least have an extra pair of numbers that is in set B (an edge) && also the rest of the numbers that are not in A go in B, so we have set A has <= n-2 numbers && set B has >= n+2 numbers

    but note that we have at most n distinct numbers, so by pigeon hole principle, with n + 2 numbers in set B, we are guaranteed a duplicate, hence this isn't a valid partition, so what we assumed at first is wrong

    so each set must have exactly n numbers

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

In problem E, can we consider a Domino as a node and and if two Dominoes have same integer then we can make an edge between them.

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

include <bits/stdc++.h>

using namespace std;

int main() { //900000000 int t; cin >> t; while (t--) { long long int a; cin >> a; int x = 0; while (a > pow(10, x+1)) { x++; } cout << a — pow(10, x) << endl; }

return 0;

}

Why is this code not working for 999999999 only else its working fine

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

Average round

»
4 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

python solution for problem B


def solve(): inp=input() s=set() ans=0 for i in inp: s.add(i) if (len(s) > 3): ans+=1 s.clear() s.add(i) if(len(s)>0): ans+=1 print(ans) t=int(input()) while (t>0): solve() t-=1