awoo's blog

By awoo, history, 4 years ago, translation, In English

1418A - Buying Torches

Idea: vovuh

Tutorial
Solution (vovuh)

1418B - Negative Prefixes

Idea: BledDest

Tutorial
Solution (Ne0n25)

1418C - Mortal Kombat Tower

Idea: vovuh

Tutorial
Solution (vovuh)

1418D - Trash Problem

Idea: vovuh

Tutorial
Solution (vovuh)

1418E - Expected Damage

Idea: Roms

Tutorial
Solution (Roms)

1418F - Equal Product

Idea: adedalic

Tutorial
Solution (adedalic)

1418G - Three Occurrences

Idea: BledDest and Roms

Tutorial
Solution 1 (pikmike)
Solution 2 (pikmike)
  • Vote: I like it
  • +165
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +27 Vote: I do not like it

Thanks for the editorial, the randomized solution to G is quite interesting.

»
4 years ago, # |
  Vote: I like it +11 Vote: I do not like it

I think there also should be tag graphs in the problem 1418C - Mortal Kombat Tower cause I solved it with Dijkstra's algorithm.

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

    could you please explain your solution a bit?

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 3   Vote: I like it +30 Vote: I do not like it
      Spoiler
      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thanks. Really nice solution.

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

        I think you can model most dynamic programming problems like a graph problem. After all, dynamic programming states are nodes and transitions are directed edges. Together they create a DAG and in this problem you minimize the cost of the transitions.

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

    Or maybe dijkstra is a DP algorithm?

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

      Not sure why you're getting downvoted. When running Dijkstra's on a graph, we can consider the graph as being implicitly ordered according to the eventual costs of the nodes, with edges going from low cost to high cost. This graph is acyclic, and the cost of a single node is computed in terms of the nodes which have an edge directed into it, which is literally just DP. The only thing that makes it different from a "normal" DP algorithm is that we compute the topological ordering of the nodes on the fly, rather than knowing it beforehand.

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

        Maybe they don't get the idea (in a funny way), since we all know of Dijkstra as a algorithm to find shortest path in non-negative weight graph.

»
4 years ago, # |
Rev. 2   Vote: I like it -28 Vote: I do not like it

I think it is not right that the model solution for D heavily relies on datastructures available in a relatively small subset of available languages, even though one of them is c++

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

Can someone explain the segment tree solution of G . what i didn't understand is that valid regions depend upon the choice of right border and also on the number under consideration for eg. for the array 1 2 3 3 2 5 4 4 2 3 4 3 if we fix the right border at last index and took the number 4 under consideration then the valid/invalid regions will look like 0 0 0 0 0 0 0 1 1 1 1 0 . But these regions will change if we change the right border or the number under consideration . so there are total n*n such arrays . How can we keep track of them all with segment tree . Do we require multiple segment tree or one will suffice .

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

    One segment tree is enough. Consider which numbers change their segments when you proceed from some right border $$$r$$$ to $$$r+1$$$. Easy to see that it's only $$$a_{r+1}$$$. So we can actually clear the segments it contributed to and add the ones it contributes to now.

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it +7 Vote: I do not like it

      Thanks for explaining , just one query remaining. How do we update the regions when moving from r to r + 1, I mean a subarray can be bad not just because it doesn't has exactly 3 numbers of $$$a_{r + 1}$$$ but may be it doesn't have exactly three numbers of some other number . We can convert a good subarray to bad when moving from r to r + 1 , but how do we convert from bad to good. How do we know what numbers are making this a bad subarray .

      • »
        »
        »
        »
        4 years ago, # ^ |
        Rev. 2   Vote: I like it +5 Vote: I do not like it

        You don't exactly store if the position is bad or good in the segtree. Each position only knows the number of bad segments it's covered by. For each bad segment $$$[l; r]$$$ you add 1 on positions $$$[l; r]$$$ in a segtree to maintain these values correct. And a position is good if the number of bad segments covering it is zero. Thus, adding a bad segment is adding 1 on a segment and removing a bad segment is subtracting 1 from a segment.

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

Such a clever and interesting approach for the problem E. I am interested to know if someone is able to solve it by going the usual way of (sum of damages for each perm / n! )

»
4 years ago, # |
  Vote: I like it -15 Vote: I do not like it

G and F should be swapped

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

Can someone give me links for more problems similar to G in which I can use string hashing but not of strings directly?

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

Hey awoo can you tell me more about the thinking process of G. Like how did you arrive at the conclusion that we can use numbers in base 3?

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

    Well, this xor hashing thingy is quite a common topic. I knew it because I encountered it multiple times. Initially the problem was to count the segments with 0 or 2 occurrences, so the usual xor worked perfectly.

    Thus, for me the transition was only from bitwise to tritwise. BledDest suggested that idea and I found it pretty cool, props to him. Couldn't really come up with it myself.

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

Hey awoo, In the post of Unofficial Editorial, I wonder whether problem D has an easier solution if we can only move one pile at a time.

I think the solution aniervs posts is right, but I wonder whether you have other clever solotuion?

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

    The aniervs's solution was harder to interpret ( and I am still not able to understand it fully), but it would be great if an alternate solution of problem D is added to the editorial based on his idea just like you guys did for problem G(hashing and segment tree). It will help people like me in knowing different ways to tackle the problem. Also thanks for both unofficial editorial and this editorial... Learnt a lot!!

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

    Idk if aniervs's solution is right, ternary search is quite hard to prove there. The function is surely convex but possibly not strictly convex which might break ternary search. Other than that I doubt it gets easier than such solution.

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

    We (adedalic, if I am not mistaken) once set a similar problem about bringing some boxes to the same point, and we thought it was possible to solve it with ternary search. But, unfortunately, the function $$$f(x) = \text{minimum number of actions required to bring all items to position }x$$$ is not strictly convex because if there are two items in positions $$$x$$$ and $$$x + 1$$$, then $$$f(x) = f(x + 1)$$$, but it is not necessarily the local minimum. Though there might be some way to handle it.

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

Hi awoo and adedalic . The way we have pushed the divisors into the set for all the values from ceil(l/x1) + 1 to floor(r/x1) in the way mentioned in the editorial solution is not clear ? Like as i understand that why we can't directly pushed all the divisors of all the numbers in the range with simple for loop of divisors of all the valid numbers of the segments . And also in the method of the solution we are also not clearing the set for further iterations of x1 is very complex to comprehend . Thanks for reading the comment and I will be grateful if you will explain .

»
4 years ago, # |
  Vote: I like it -8 Vote: I do not like it

Can anyone help me identify what's wrong in my approach in C :


dp[i][0] //denotes min coins spent if I kill the last boss dp[i][1] // denotes min coins spent if my friend kill the last boss . int ans(int i ,int j){ if(i<0) return 1e9 + 7 ; if(i==0) { return a[i]; // since my friend's turn is the first one } if(i==1) { if(j==0) return min(ans(0,0),ans(0,1)) ; else return a[i] + min(ans(0,0), ans(0,1)); } int& res = dp[i][j] ; if(res!=-1) return res ; if(j==0) res = min(ans(i-1,1) , ans(i-2,1)) ; else if(j==1) res = min(ans(i-1,0) + a[i], ans(i-1,0) + a[i-1] + a[i]) ; return res ; }

here's my submission : 92930622

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

    ` else if(j==1)` ` res = min(ans(i-1,0) + a[i], ans(i-1,0) + a[i-1] + a[i]) ;` ` return res ;` It should have been :-
    Your code here...
    

    res=min(ans(i-1,0)+a[i],ans(i-2,0)+a[i]+a[i-1]);

  • »
    »
    4 years ago, # ^ |
      Vote: I like it -8 Vote: I do not like it

    There is a mistake in the line: if(i==0) ** {** ** return a[i]; // since my friend's turn is the first one** ** }**

    so when you are calling ans(0,0) and ans(0,1) then you will get the same result as a[0] which shouldn't be. And really telling you only have to change only if(i==1) { if(j==0) return ans(i-1, 1); else return a[i] + ans(i-1,1); //as the first boss is only killed by the friend. }

    And you can write more simpler version of it like these two : 1) https://mirror.codeforces.com/contest/1418/submission/92964444 2) https://mirror.codeforces.com/contest/1418/submission/92964444

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

      Hello, can you please explain me what the lines for calculating first,second and min means?

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

Edit Request awoo

I think in problem E tutorial, you mistakenly replaced all $$$a_j$$$ for $$$b_j$$$ in every $$$max(K - a_j, 0)$$$ expression.

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

I've implemented the same code and it gives correct answer on my machine but it shows WA on Test Case 1. 92970290. I don't understand why this is happening.

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

    NVM I found the error, I was inserting to the multiset a value which was not initialized. This is the final submission. 92971068

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

For G you would be better off using xor instead of sum. For each value generate two random integers x and y, and cyclically put x, y, x xor y in places with this value, the hash will be prefix xor.

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

    Then how will we do the 2nd part of the problem to find the sub-array every integer that occurs in this subarray occurs there exactly thrice. using your xor trick I can see if in the range [l,r] freq of every element is multiple of 3.

»
4 years ago, # |
  Vote: I like it -35 Vote: I do not like it

**any one can help out why i am getting tle on Problem D and tc 52? i am using treemap and treeset **

public class D {

static TreeMap<Integer, Integer> tm;
static TreeSet<Integer> ts;
static int mod = (int) 1e9 + 7;

static int ar[];

// static Scanner sc = new Scanner(System.in); static StringBuilder out = new StringBuilder();

static HashSet<Integer> gr[];
int n;

static class pair implements Comparable<pair> {

    int f;
    int s;

    public pair(int a, int b) {
       f = a;
       s = b;
    }

    @Override
    public int compareTo(pair o) {
       // TODO Auto-generated method stub
       if (this.s == o.s)
         return o.f - this.f;
       return this.s - o.s;
    }

}

static int[] dp;

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    int tc =1;// sc.nextInt();
    while (tc-- > 0) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st = new StringTokenizer(br.readLine());
            int n = Integer.parseInt(st.nextToken());
            int q = Integer.parseInt(st.nextToken());

       int ar[] = new int[n];
        tm = new TreeMap<>();

        ts = new TreeSet<>();


        st = new StringTokenizer(br.readLine());
       for (int i = 0; i < n; i++) {
         ar[i] = Integer.parseInt(st.nextToken());
         ts.add(ar[i]);
       }

       Arrays.sort(ar);


       for (int i = 0; i < n - 1; i++) {

         int dif = ar[i + 1] - ar[i];

         tm.put(dif, tm.getOrDefault(dif, 0) + 1);


       }




       if(ts.size()<=2)out.append(0+"\n");
       else{

         out.append(ts.last() - ts.first() - tm.lastKey() + "\n");
       }


       while (q-- > 0) {

         st = new StringTokenizer(br.readLine());
            int t1 = Integer.parseInt(st.nextToken());
            int x1 = Integer.parseInt(st.nextToken());



         int ceil=x1;
         int floor=x1;
         if (t1 == 0) {


          if(ts.ceiling(x1+1)!=null){


               ceil=ts.ceiling(x1+1);

               tm.put(ceil-x1, tm.get(ceil-x1)-1);
               if(tm.get(ceil-x1)==0)tm.remove(ceil-x1);


          }
          if(ts.floor(x1-1)!=null){
              floor=ts.floor(x1-1);
               tm.put(x1-floor, tm.get(x1-floor)-1);
               if(tm.get(x1-floor)==0)tm.remove(x1-floor);
          }

          if(floor!=x1 && ceil!=x1){

              tm.put(ceil-floor, tm.getOrDefault(ceil-floor, 0)+1);
          }

          ts.remove(x1);


          if(ts.size()<=2){
              out.append(0+"\n");

          }
          else{
              out.append(ts.last()-ts.first()-tm.lastKey()+"\n");
          }


         } else {



          if(ts.ceiling(x1)!=null){

              ceil=ts.ceiling(x1);

              tm.put(ceil-x1, tm.getOrDefault(ceil-x1, 0)+1);


          }

          if(ts.floor(x1)!=null){

              floor=ts.floor(x1);
              tm.put(x1-floor, tm.getOrDefault(x1-floor, 0)+1);
          }

          if(ceil!=x1 && floor!=x1){

              tm.put(ceil-floor, tm.get(ceil-floor)-1);
              if(tm.get(ceil-floor)==0)tm.remove(ceil-floor);
          }

          ts.add(x1);
          if(ts.size()<=2){
              out.append(0+"\n");

          }
          else{

              out.append(ts.last()-ts.first()-tm.lastKey()+"\n");
          }

         }
       }

       br.close();

    }

    System.out.println(out);

}
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

i am getting runtime error in solution for problem c. can somebody help. https://mirror.codeforces.com/contest/1418/submission/92995605

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

I had read the problem 1418B - Negative Prefixes for more than 40 min and after that I had read the solution but I wasn't able to understand these lines in block ,can any one help , I think it says we need to find array so that it prefix sum array should be like starting from negative elements and once the positive element came then there should be no negative elements(all elements after that are positive) like eg [−8,−14,−13,−9,−5,2,0] and as given in solution and k is 5 , and we need to reduce the k some and propose a solution ,but that was not the case

Let k be the maximum j (1≤j≤n) such that pj <0. If there are no j such that pj<0, then k=0.

Your goal is to rearrange the values in such a way that k is minimum possible.

thanks in advance

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

    Ok so this is what it means. Lets have this prefix sum array [-1,-4,-5,-6,7,8,9]. Here, k = 4. This is because k is the index of the "right most negative element".

    Another example: prefix sum array = [1,-3,4,-5,-1,2,3] Here, k = 5. The "right most negative element" is -1, and so its index is 5. You can have positive in between the negatives, it doesn't matter. What the problem is asking is you want k to be as small as possible.

    So you want to arrange the array such that its prefix sum array will have the smallest k, when compared to other arrangements.

    I hope this helps!

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

A solution for F without any data structures, just simple math (although with many steps and cases):

For easier reading, let's rename $$$(x_1,y_1,x_2,y_2)$$$ to $$$x,a,b,c$$$, so we need to find three numbers $$$a,b,c$$$ such that $$$xa=bc \in [l,r]; a,c \in [1,m]; b \in [x+1,n]$$$. In fact, we can drop the constraint on $$$c$$$, it follows from the others.

So far, we have fixed a value of $$$x$$$. Now let's fix the greatest common divisor $$$g$$$ of $$$b,x$$$. There can only be a total of $$$n \log n$$$ such divisors across all values of $$$x$$$. The idea is to quickly find a solution given $$$g,x$$$ if it exists. We know that $$$b>x$$$ and since $$$g$$$ is their common divisor, we can write $$$b=x+yg$$$ for some $$$y>0$$$. The constraint on $$$b$$$ becomes $$$y\leq \frac{n-x}{g}$$$. Also, since $$$b$$$ divides $$$ax$$$, we have that $$$b/g$$$ divides $$$a(x/g)$$$ and so, since $$$b/g$$$ and $$$x/g$$$ are coprime, $$$b/g$$$ divides $$$a$$$, and so $$$a=(b/g)z=(x/g+y)z$$$ for some $$$z$$$.

Let's handle two cases:

1) $$$g > \sqrt{x}$$$. In this case there can be at most $$$x/g < \sqrt x$$$ different values of $$$y$$$ and we can try each. For a fixed $$$y$$$, we check whether $$$b$$$ fits the constraints and whether we can find $$$z$$$ such that $$$a$$$ also fits the constraints.

2) $$$g \leq \sqrt{x}$$$. We try each value of $$$z$$$ as long as $$$a$$$ is less than $$$m$$$. In fact, from $$$a=(x/g+y)z$$$ and $$$y>0$$$ we have $$$z \leq \frac{m}{x/g+1}$$$. After fixing $$$z$$$, we look for $$$y$$$ to fulfil the linear constraints given above (a couple of integer divisions and min/max).

Overall, the sum of small divisors (less than $$$\sqrt{x}$$$ for all numbers $$$x$$$ up to $$$n$$$ is asymptotically $$$O(n \sqrt n)$$$, and the sum of all values $$$\frac{1}{x/g+1}$$$ for all small divisors is asymptotically equal to $$$O(\sqrt n)$$$, so the overall complexity is $$$(n+m)\sqrt{n}$$$.

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

How to compute the probability of collision in problem G? Would some love to elaborate why it is the same as two vectors (out of n) colliding in a K-dimensional space with their coordinates being from 0 to 2? I will appreciate it a lot.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it
Your code here...
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
void solve()
{
    lli m = 1000000007;
    lli t;cin>>t;
    while(t--)
    {
        lli n;cin>>n;
        lli A[n];
        FOR(i,0,n-1)
        {
            cin>>A[i];
        }
        lli i=0,s=0;
        lli f=1;
        while(i<n)
        {
            if(f)
            {
                s+=(A[i]==1);
                i++;
                if(i<n&&A[i]==0)
                   i++;
                f=0;
            }
            else 
            {
                i++;
                if(i<n&&A[i]==1)
                  i++;
                f=1;
            }
        }
        cout<<s<<"\n";
    }

}
int main()
{
    solve();
}

Can anyone tell what's wrong in the code?

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

problem C have DP solution

solution