maroonrk's blog

By maroonrk, history, 19 months ago, In English

We will hold AtCoder Regular Contest 150.

The point values will be 300-500-500-700-800-1000.

We are looking forward to your participation!

  • Vote: I like it
  • +119
  • Vote: I do not like it

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

Hope to solve three problems. And not get too happy just by solving two problems.

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

    I only got 1 :(. Should have got C but not good with graphs/dijkstra's. :( :(

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

Hope you all have a good time in this competition!

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

I just wonder who aaa1a is.

»
19 months ago, # |
  Vote: I like it -28 Vote: I do not like it

Couldn't solve any :( I think A was too case-heavy for me

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

    A was really straightforward. Very little case work. (Actually 0).

    #include<bits/stdc++.h>
    
    void solve() {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        int tcnt0 = count(all(s), '0');
        int tcnt1 = count(all(s), '1');
        int cnt0 = 0, cnt1 = 0;
        for (int i = 0; i < k - 1; i++) {
            if (s[i] == '0') cnt0++;
            else if (s[i] == '1') cnt1++;
        }
        int ans = 0;
        for (int i = k - 1; i < n; i++) {
            if (s[i] == '0') cnt0++;
            else if (s[i] == '1') cnt1++;
            if (cnt0 == 0 && cnt1 == tcnt1) {
                ans++;
            }
            if (s[i - k + 1] == '0') cnt0--;
            else if (s[i - k + 1] == '1') cnt1--;
        }
        cout << (ans == 1 ? yes : no) << '\n';
    }
    
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int t = 1;
        cin >> t;
        while (t--) {
            solve();
        }
        return 0;
    }
    
    • »
      »
      »
      19 months ago, # ^ |
      Rev. 4   Vote: I like it 0 Vote: I do not like it

      yeah... my code was a massive lump of spaghetti with probably 7~8 if-else statements, each containing a line of yes or no. :( I guess I get lost into nowhere more often on atcoder than on cf

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

        it happens. :)

        I got 3 WAs because I wrote s[i - k] instead of s[i - k + 1]. But it was worth it, in the end.

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

Is it only me that feels that Atcoder problems are more simple and interesting (even hard ones), as compared to codeforces.

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

I don't understand why this code for A doesn't work. I believe I have the same idea as the editorial, although I implemented it badly, or maybe did something wrong, but I can't find it even after searching for an hour. Could someone help me out?

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

This is the code for starr(https://atcoder.jp/contests/arc150/submissions/35550331), which I think is TLE.

#include <iostream>
using namespace std;
#define int long long
signed main(){
    int t,a,b,i;
    cin>>t;
    while(t--){
        cin>>a>>b;
        if(a>=b) cout<<a-b<<"\n";
        else{
            if(b%a==0) puts("0");
            else{
                int now=b/a,mi=b-a;
                mi=min((b/a+1)*a-b,mi);
                //b/(mi+a-1)
                for(i=now;i>=1;i--){  //i是倍数
                    int x=b/i+(b%i!=0);
                    //cout<<x<<" "<<x*i<<" "<<a<<endl;
                    mi=min(mi,x-a+x*i-b);
                }
                cout<<mi<<endl;
            }
        }
    }
}
  • »
    »
    19 months ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    I write this solution during contest too and it passed. Maybe the tests are too weak.

»
19 months ago, # |
Rev. 4   Vote: I like it +8 Vote: I do not like it

Can problem — A Continuous 1 https://atcoder.jp/contests/arc150/tasks/arc150_a

be solved using DP ??

I tried dp but it couldn't get correct solution .

My idea :

Each '?'has 2 options to be replaced using '0' or '1' .

I formed this recursive logic

idx -> index , leftOne == K — alreadyPresentOne '1'

I'm asking from isPossible() , whether it's possible to have K consecutive 1's And for each True I increment counter , If counter > 1 return False.


bool isPossible(int idx, int leftOne) { if (idx >= n && leftOne != 0) return 0; if (leftOne == 0) { //Check if K consecutive 1' possible and ++counter if (pfxSum[i] == tar) { ++counter; return true; } } return false; } while (idx < n && s[idx] != '?') { ++idx; } bool way1 = false, way2 = false; // if (dp[leftOne][idx] != -1) // return dp[leftOne][idx]; if (s[idx] == '?') { s[idx] = '1'; way1 = isPossible(idx + 1, leftOne - 1); s[idx] = '0'; way2 = isPossible(idx + 1, leftOne); s[idx] = '?'; } return (way1 || way2); }

But when I turned it into dp[index][leftOne] , I couldn't do I realise it's because let say I've 00?1???10? if at one instance like 001111010? dp[7][0] == No but then for 0001111100 dp[7][0] == Yes

Please verify if it's dp solvable or not ??

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

I think my solution to problem C is a little weird, and I am not sure if it is correct (although get AC).

I use normal bfs, starting from node-1, but with k stages, where k is just the input in the statement. For each stage, we keep visiting all the unvisited nodes until we meet nodes-x, where a[x]=b[stage]. During the process, if node-n has been visited, then the answer is no, otherwise is yes.

By the way, problem B uses the sqrt-technique, which is somehow similar to problem B in ARC139. I feel very lucky that I realize this quickly.

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

    That is essentially the same as editorial, just another way how to implement 0/1 bfs.The values in array $$$d$$$ are euqivalent to the amount of iterations before you discovered some value in your case.

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

      Thank you for your kind reply. But after reading tutorial, I find that solution is more reasonable and formed in a systematic way, while mine seems a little bit unclear.

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

How I "solved" B:

  • First, notice that ans is at most min(b-a,a) so you can bruteforce for all x<=1 mil and this handles all cases with a<=1mil.

  • For the remaining cases(a>1mil): Suppose b+y = z(a+x), bruteforce for all z <= b/a and y<=b/a. Both loops are <= 1000 runs

  • I think this is WA solution but weak tests. If it happens to be correct, can anyone show proof?

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

    Why would it be wa? also if you know z you don't need to bruteforce y. This is my solution

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

      I thought there might be a case with y>1000, thats why. I guess the idea is AC, when you get y with math.

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

    Will you please explain more what you are doing in remaining cases(a>1mil) part? I am unable to understand only by watching your code.

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

    maybe 01bfs

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

    Let me know if you got the reason why using BFS is not working.Thanks in advance

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

      because if n is 9, and a road is 1 -> 4 -> 8 -> 9, and another is 1 -> 3 -> 6 -> 7 -> 5 -> 8 -> 9, and 4 8 is b[2], b[3],3 6 7 5 in not in b[], then BFS with get dp[9] = 4,but the answer should be dp[9] = 2

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

        4 8 is b[2], b[3],3 6 7 5 in not in b[]

        can you elaborate this part

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

In editorial of problem D — tester's solution: "Not only bad vertices but also good vertices can be chosen.". Why doesn't that change the result?

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

    The outcome of the random variable $$$X_v$$$ depends on status of the parents of $$$v$$$. That is, it just needs to 'keep track' of the parents to know when $$$v$$$ has become good and it needs to stop performing operations. This intuition explains why we don't care about the operations which pick something apart from $$$v$$$ and its parents. Those operations neither change the state of $$$v$$$'s parents, nor do they directly change the value of the random variable (which would only be affected when we pick precisely $$$v$$$).

    A similar argument holds for not caring about whether we pick good or bad vertices. Even if we pick a good vertex, it was already coloured black from earlier. So the status of none of the parents change. We know we didn't pick $$$v$$$ itself, since the process would have already stopped if $$$v$$$ was good beforehand. This operation doesn't affect the value of the random variable as well. And so we're free to consider them as valid moves.

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

I was lost while reading editorial of B. Someone save me please.

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

    We want $$$B+Y$$$ to be a multiple of $$$A+X$$$, i.e., $$$B+Y = k(A+X)$$$ for some positive integer $$$k$$$.

    If someone told you what $$$k$$$ you need to use, then part 1 of the editorial tells you how to find $$$X$$$ and $$$Y$$$ such that $$$X+Y$$$ is smallest. We have, $$$Y = kX+kA-B$$$ and $$$X+Y = (k+1)X+kA-B$$$. To minimize $$$X+Y$$$ is then same is to minimize $$$X$$$, because $$$k$$$, $$$A$$$, and $$$B$$$ are fixed. We choose smallest $$$X$$$ such that $$$Y \ge 0$$$ (recall $$$Y = kX+kA-B$$$). This value is $$$\max(0, \lfloor\frac{B-1}{k}\rfloor + 1 -A)$$$ (convince yourself of this).

    Part 2 then tells you that for $$$k \le \sqrt{B})$$$ you can just compute this value, and the number of different values of $$$\lfloor\frac{B-1}{k}$$$ for $$$k > \sqrt{B}$$$ is at most $$$\sqrt{B}$$$. For example, if $$$B = 101$$$, then $$$\lfloor\frac{B-1}{k}\rfloor$$$ can take values $$$9, 8, 7, 6, 5, 4, 3, 2, 1$$$ if $$$k\ge 11$$$. So you compute minimizing $$$X$$$ using $$$k = 1, 2, \ldots, \sqrt{B}$$$ first then compute minimizing $$$X$$$ using $$$\lfloor\frac{B-1}{k}\rfloor = 1, 2, \ldots, \sqrt{B}$$$. (Maybe try 2 or 3 more values if I am missing some constants.)

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

      Thanks for your explanation!

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

      Y = kX + kA -B
      Having Y >= 0, I get X >= floor((B-kA)/k)
      How do I reach X >= floor((B-1)/k) + 1 -A from here?

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

        This is just a hacky shortcut. You want $$$kX + kA - B \ge 0$$$, i.e., $$$X \ge \frac{B}{k} - A$$$. There are two cases.

        Case 1. $$$\frac{B}{k}$$$ is an integer, so we can set $$$X = \frac{B}{k} - A$$$. You can see that, in this case, $$$\frac{B}{k} = \lfloor\frac{B-1}{k}\rfloor + 1$$$ (because $$$\lfloor\frac{B-1}{k}\rfloor = \frac{B}{k} - 1$$$).

        Case 2. $$$\frac{B}{k}$$$ is not an integer, so the next higher integer is $$$\lfloor\frac{B-1}{k}\rfloor + 1$$$.

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

          Thank you! This shall help me in the future as well

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

    I also couldn't understand the editorial of B. Then I looked at more editorials and found a better explanation from the official video editorial:

    For B + Y = k(A+X), there are two approaches to find X + Y:

    • Approach 1: Enumerate X from 0 to A-1, we can get a value of X + Y for each case and keep the minimum.
    • Approach 2: Enumerate k from 1 to B/A, we can get a value of X + Y for each case and keep the minimum.

    Note the range of X and k, when A is extremely small or extremely large, either Approach 1 or Approach 2 can be very slow. Therefore, pick a value of sqrt(B) to determine to only use the faster of the 2 approaches.

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

      Much cleaner! Just to add a tiny elaboration: if $$$A \le \sqrt{B}$$$, then Approach 1 uses $$$\le\sqrt{B}$$$ iterations, and when $$$A > \sqrt{B}$$$, Approach 2 uses at most $$$B/A \le\sqrt{B}$$$ iterations.

      Edit: Actually, reasoning for why Approach 2 only checks until $$$B/A$$$ is not clear to me.

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

        IF you go over the range, you will notice the Xs and/or the Ys computed by the formula can become negative. Negative Xs or Ys are not allowed.

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

In problem B, he has not used the $$$\sqrt{N}$$$ algorithm but something else. Can someone explain his code please

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

I have a small confusion regarding simple path

Simple path doesn't mean it is the shortest path right , it means path which doesnot contains two or more same vertices

Then in the sample test case 2 of problem C why we are not considering path (1,2,3,4,5) ?

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

    In path every consecutive vertex is connected directly by an edge. 2 and 3 do not share an edge

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

      2 and 3 have an edge right ?

      This is the sample test case 2 :

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

      3rd line of the test case represents there is an edge between 2 and 3

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

        If we consider path (1,2,3,4,5) then yes the condition will be satisfied but the problem statement says that condition has to be satisfied for all simple paths. It is not satisfied for (1,2,5)