awoo's blog

By awoo, history, 13 months ago, translation, In English

1886A - Sum of Three

Idea: fcspartakm

Tutorial
Solution (fcspartakm)

1886B - Fear of the Dark

Idea: BledDest

Tutorial
Solution (Neon)

1886C - Decreasing String

Idea: Roms

Tutorial
Solution (Roms)

1886D - Monocarp and the Set

Idea: Roms

Tutorial
Solution (Roms)

1886E - I Wanna be the Team Leader

Idea: BledDest

Tutorial
Solution (awoo)

1886F - Diamond Theft

Idea: BledDest

Tutorial
Solution (Neon)
  • Vote: I like it
  • +25
  • Vote: I do not like it

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

amazing round loved it

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

Just become expert again Amazing contest

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

What will be the expected difficulty for problem B it seems bit tough compared to other div 2 B Questions

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

    No it is pretty balanced. You need to know basic geometry and/or binary search

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

      I thought of using binary search at first, on w but w is floating point number. so how can we apply binary search on it can you please elaborate.

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

Thanks for slow editorial!

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

Div2 D Alternate explanation:

Consider ?<??>?>??

Observation : Last > will be n and last < be 1

This is always true. If 1 and n are always in the permutation could there be a > after n and < after 1? No. If there aren't any such symbols for either > or < they will be in 1st position .

However will the subsequent >s after last > also be n-1,n-2,n-3? And will the subsequent <s after last < also be 2,3,4.....?

The answer is No.

This is because we have ?s after last > and last < respectively. There we could put values So that we could form a decreasing sequence from n ( > > >) and an increasing sequence from 1 (< < <). There could be many such sequences clearly.

Now let's propose an algorithm,

?<??>?>??

Starting from the back we have n-2 choices where n is number of els in the set. This is because only the minimum(1) and maximum(N) is fixed. Now remove last element.

?<??>?>? again n-2 choices where n is number of els in the set.

?>??>?>

1 choice last > is fixed. Similarly last < is fixed.

?>??>? now last > is the max el in the set. We remove 2 els and N so far. Irrespective of our choices for the 2 els, there will be 1 unique maximum in the set which will take place of last >. So for ? we have n-2 choices again where n is number of els in the set.

Hope you get the solution.

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

    "However will the subsequent >s after last > also be n-1,n-2,n-3? And will the subsequent <s after last < also be 2,3,4.....?

    The answer is No."

    I made this wrong assumption and was wondering why my solution is wrong! Thanks for this. I never would have realised why my solution is wrong( This happens a lot to me, I make wrong assumptions and get to a wrong solution but I never find that wrong assumption on my own, and usually some other person points it out for me. I need to work on this by embracing the Sherlock's quote : "When you have eliminated the impossible, whatever remains, however improbable, must be the truth"

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

I don't understand why my solution for C didn't work. It passed the first test case fine, and was very similar to the expected output for the second testcase, so I was wondering where I went wrong.

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

    The fifth character is different.

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

Problem C. Decreasing String ***** In this problem test number 2 in test case 5. The string is given "pbdtm" and the POS=8 If I select s1=pbdtm s2=pbdm s3=bdm s4=bd s5=b The condition is still right. Because s1>s2>s3>s4>s5 And the POS value of 8 is 'd'.. But why this is wrong answer?

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

    $$$s_2 = \tt{bdtm}$$$, not $$$\tt{pbdm}$$$

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

      i'm having same problem just wondering why ( s1= pbdtm -> s2 = pbdm )is not valid ???

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

        The problem statement says that we always need to make the next string lexicographically minimal.

        From $$$\tt{pbdtm}$$$ we could go to $$$\tt{pbdt}$$$, $$$\tt{pbdm}$$$, $$$\tt{pbtm}$$$, $$$\tt{pdtm}$$$ or $$$\tt{bdtm}$$$. The lexicographically smallest one of these is $$$\tt{bdtm}$$$.

        We cannot go to $$$\tt{pbdm}$$$, since $$$\tt{bdtm}$$$ is lexicographically smaller.

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

Great idea for E, thanks a lot for the wonderful contest :)

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

You can also solve problem B by binary search. By the way, problem c editorial is great,thanks:).

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

    I tried to solve it using binary search 247760061, but getting a TLE though. can someone please help me figure out why?

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

      Change while to for(int i=1;i<=200;i++) and set high to 1e9 and you'll pass this question!

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

        Thanks. It worked 248056414.

        So, was that because there will be so many real values between the extremes?

        And we are can get to an approximate value in < 200 iterations?

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

          I think it's possible that the way your code is written could have some precision issues causing it to get stuck in a dead loop. In fact, we would halve the search each time, and even 100 searches would be enough to achieve the precision needed for the question.

          (I'm sorry my English is not good, the above is the result of using a translator, I hope you can read it:)

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

Nice editorial

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

Awesome Editorial and Problem set.

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

Can someone help me with understanding my solution to B more clearly? I tried using binary search by answer here. I felt it was a plausible solution since if a radius (say r) is an answer then all radiis > r are also the answer. I tried implementing some solution, didnt work, maybe my impl on binary searching for real answers suck. Help/Correction appreciated!

// checks if point is inside circle
bool check(pair<float, float> circle, pair<float, float> point, float w){
    return ((circle.first - point.first) * (circle.first - point.first) + 
    (circle.second - point.second) * (circle.second - point.second)) <= w * w;
}

//checks if circles A and B are touching
bool istouch(pair<double, double> a, pair<double, double> b, double w){
    return w + w >= ((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second));
}


//checks if the radius is a valid answer
bool isok(pair<float, float> b, pair<float, float> p, pair<float, float> o, pair<float, float> a, float w){
    return (check(b,o,w) & check(b,p,w) | (check(a,o,w) & check(a,p,w))) | 
    (check(b,p,w) | check(a,p,w)) & (check(b,o,w) | check(a,o,w) & istouch(a,b,w));
}

double WORST = 3e3f;
double EPS = 1e-6f;

void solve(){
    pair<float, float> p, a, b, o;
    cin >> p.first >> p.second;
    cin >> a.first >> a.second;
    cin >> b.first >> b.second;

    o = make_pair(0.0f, 0.0f);

    double l = 0.0f, r = WORST;
    double w = r;

    //binary searching the answer starts from here
    while(r - l > EPS){
        double mid = (l + r)/2;
        if(isok(b,p,o,a,w)){
            w = mid;
            r = mid;
        } else {
            l = mid;
        }
        // cout << l << " " << r << "\n";
    }

    cout << w << "\n";

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

    In theory binary search should be possible here, even if I don't think it makes the problem easier to solve than the direct solution.

    Problem 1: istouch() is wrong. You need to square the distance at the left-hand side (as you do in check()), or take the square root of the righthand side (hypot() is useful here).

    Problem 2: isok() is wrong. You need to check four possible paths: O->A->P, O->B->P, O->A->B->P, and O->B->A->P. The first two subexpressions (check(a,o,w) & check(a,p,w) and check(b,o,w) & check(b,p,w)) cover the first two possibilities. The third expression doesn't really work. instead of check(b,p,w) | check(a,p,w)) & (check(b,o,w) | check(a,o,w) & istouch(a,b,w) you need something like (check(b,p,w) | check(a,p,w)) & (check(b,o,w) | check(a,o,w)) & istouch(a,b,w) (note the different places of the parentheses!)

    (Also, generally don't use & and | for Boolean logic. Use the proper Boolean operators && and || instead. But that's not causing any bugs in this case.)

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

For problem E, I was able to squeeze in what I think is an $$$O(m \times 2^m \times \sqrt{n})$$$ solution that doesn't precompute the number of programmers needed to satisfy a project: 227729256

Not sure if this was intended to pass or not!

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

For Problem F, the editorial states that when "when you move a camera of type 2 to the first block, it can force multiple cameras of type 3 into the second block". I'm unable to see why this is true.

It seems to me that from the way we pick the camera of type 3, after a single move, it will fix all the $$$x$$$ which have $$$d_x - x > 0$$$. Because after moving the camera of type 2 from block2 to block1, we might cause several indices to have $$$d_x - x = 1$$$ (and not any value higher than $$$1$$$), which can be fixed by a single camera move.

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

    Good question, it was one of our main points of concern while designing the solution (we weren't sure if it is necessarily to change the positions of multiple cameras of type $$$3$$$). This is because, when you move a camera of type $$$2$$$, it increases $$$d_x$$$ by $$$1$$$ on some suffix of values. But when you "move" a camera of type $$$3$$$, it actually stays in the first block (we still have to hack it before stealing the first diamond). It just has a bigger range of possible positions. So, it decreases $$$d_x$$$ on some suffix, but increases $$$d_x$$$ back on some shorter suffix (i. e. it just decreases $$$d_x$$$ by $$$1$$$ on one subsegment, not on the whole suffix). If there are multiple points where $$$d_x - x$$$ became positive after we moved a camera of type $$$2$$$, some of them might be outside that affected segment.

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

In F why "if we skipped some camera which could be inserted into the first block, and chose some other camera for the same slot, they can be "swapped". " is right?

I think if we first insert the camera which $$$s_i$$$ is small and then insert the camera which $$$s_i$$$ is big,for the second block it is greater,but for the first block it is not greater than we first insert the bigger one.

BledDest

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

    Suppose we already placed cameras of type $$$1$$$ and $$$2$$$, and try to place cameras of type $$$3$$$. Why is it always optimal to do it in non-descending order of $$$s_i$$$, and place a camera only in the first block (instead of placing it in both) whenever possible?

    Suppose we fixed an optimal placement of cameras, and it differs from the one produced by that greedy algorithm. Let's fix the first (in sorted order) camera $$$i$$$ that would be placed in the first block by the greedy algorithm, but is not placed there by the optimal solution.

    There should be a moment of time (some $$$x$$$ seconds before stealing the first diamond) where we would place that camera $$$i$$$ in the first block in the greedy solution. So, the condition $$$s_i \ge x + len$$$ must hold. If there's no camera of type $$$3$$$ occupying that moment, then we just place that camera $$$i$$$ there and remove it from the second block.

    If there is such a camera, suppose its index is $$$j$$$. We can show that if we place the camera $$$j$$$ in both blocks, and the camera $$$i$$$ in the first block only, nothing will break: we can place the $$$j$$$-th camera in both moments of time occupied by the $$$i$$$-th camera, and the $$$i$$$-th camera in the slot of the $$$j$$$-th camera:

    • since $$$s_j \ge s_i$$$, the position for the $$$i$$$-th camera in the first block is suitable for the $$$j$$$-th camera;
    • since $$$s_j \ge s_i$$$, the position for the $$$i$$$-th camera in the second block is suitable for the $$$j$$$-th camera;
    • $$$s_i \ge s_j$$$ does not necessarily hold, so it might be possible that the slot occupied by the $$$j$$$-th camera is unsuitable for the $$$i$$$-th camera. But remember that the greedy algorithm would place the $$$i$$$-th camera in the time slot occupied by the $$$j$$$-th camera, so the condition $$$s_i \ge len + x$$$ holds. So, that slot is suitable for the $$$i$$$-th camera.

    By repeating this process enough times, we can transform the optimal solution to the solution produced by the greedy approach, and nothing breaks.

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

Can anyone help me to find why is it wrong in th case 16 of the problem E: https://mirror.codeforces.com/contest/1886/submission/228496275

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

    Obviously, the programmers that you finally choose are the biggest ones, so you should sort them decreasingly instead of increasingly before DP. Otherwise it means that you assumed choosing the smallest one, making it not optimal sometimes.

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

    e.g.

    2 1

    1 3

    3

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

Hi, can anyone explain for D, (TestCase 1, before all queries.)why the permutation 2, 1, 3, 5, 4, 6 is not possible ?

Maybe I am missing something, but the sequence is <?>?>

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

    when you add 3, it becomes maximum so it shouldn't be a question mark, but rather a '>'

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

For C, I did an answer using the quadratic equation that avoids data structures, but it has a runtime error on test 8, don't know why :(

/* solve.cpp
 * solution to problem X
 */

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

int solver(string s, long long pos) {
  long long L = s.length();
  // quadratic equation to find length of x.

  // x - the length of the substring of the solution.
  long long x = floor((1 + sqrt(1 + 4 * (pow(L, 2) + L - 2 * pos))) / 2);

  // xb - the index of the beginning of substring x
  long long xb = (pow(L, 2) + L - pow(x, 2) + x) / 2 - x;
  // xi - the index in the substring x of the solution.
  long long xi = pos - 1 - xb;
  // cout << "*** x: " << x << " xi : " << xi << "\n";

  long long i = 0;
  long long erased = 0;
  while (L - erased > x) {
    // what to do before getting to the end of the string
    if (i + 1 <= L - erased) {
      char curr = s[i];
      char next = s[i + 1];
      if (curr > next) {
        s.erase(i, 1);
        erased++;
        if (i > 0)
          i--;
      } else {
        i++;
      }
    }
    // at the end of the string
    else {
      s.erase(i, 1);
      erased++;
      i--;
    }
  }

  // Print result.
  cout << s[xi];
  return 0;
}

int main() {
  int numTests;
  cin >> numTests;
  while (numTests--) {
    string s;
    int pos;
    cin >> s >> pos;
    solver(s, pos);
  }
  return 0;
}
»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

for question C you should put a hint (use a stack) as only knowing this information made me solve it

overall, good question and perfect tutorial thanks

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

In problem $$$C$$$, I was wondering what if we had some $$$Q$$$ queries, where each query is asking the character at queried position and length of string as ($$$N < 10^6$$$).

Any ideas on how we will go about it, like what information do we need to store ?
Or should the constraints be reduced ?
Like any approach for this so called Bonus problem ?

Roms

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

Another approach for problem A which is more concise 248334395