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

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

1728A - Colored Balls: Revisited

Идея: BledDest

Разбор
Решение (Neon)

1728B - Best Permutation

Идея: BledDest

Разбор
Решение (Neon)

1728C - Digital Logarithm

Идея: BledDest

Разбор
Решение (awoo)

1728D - Letter Picking

Идея: BledDest

Разбор
Решение (awoo)

1728E - Red-Black Pepper

Идея: BledDest

Разбор
Решение (awoo)

1728F - Fishermen

Идея: BledDest

Разбор
Решение (BledDest)

1728G - Illumination

Идея: BledDest

Разбор
Решение (awoo)
  • Проголосовать: нравится
  • +113
  • Проголосовать: не нравится

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

Awesome problemset; I really liked Red-Black Pepper, even though I couldn't solve it on time!

As for problem D... somehow, my O(n) greedy-ish algorithm passed all pretests and didn't get yoinked out of existence after system testing..... I'll be confused for a while

Edit: Here is my source code if anyone wants to look at it:

Source code
  • »
    »
    4 года назад, скрыть # ^ |
     
    Проголосовать: нравится +14 Проголосовать: не нравится

    It can be proven that Bob have no winning strategy for every string. And a draw can be if and only if string have form ABC, where C is reversed A and B consists of pairs of identical letters standing next to each other (i.e. have form aabbcc...)

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

    I wanna know the intuition about your approach . Y does Bob never loose ?

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

      Since Alice always goes first, she can just pick the most optimal letter which remains. If there is a letter that Bob can pick to beat Alice, Alice can just pick that letter instead of the last letter she chose.

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

      My approach to Problem D is an O(n) solution. Since Bob never wins(Can check using strings of length 2,4,6 and manually verifying cases), what we need is to find the condition for the conclusion when the game ends in a draw. Firstly let us modify the input string given from s->s' where s' is s after removing all consecutive letters from start and end such that s[i]=s[length-1-i] and break the loop when s[i]!=s[length-1-i]. The reason is that if Alice picks one such letter then Bob can pick the letter from the opposite end to get a string to be the same as Alice's. Next the string s' left after removing all such letters (even 0 letters can be removed) ought to be of the form AABBCC.....ZZ where A,B,C,Z represents any letter. Reason is that if the string was AABBCC.....YZ,then Alice can pick Z and Bob has no other option but to pick either A or Y....i.e. He can never Draw with Alice and hence can only lose.

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

      I think the most important thing to note here is that the length of the string is even + the chosen letters are prepended in the string. If either of the above conditions were not true, then Bob could have had a chance to win.

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

    please explain your approach

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

      It was just like what magni93 described. I guessed heuristically (proof by lack of counterexample) that Bob can't win, so that he can at best force a Draw. Additionally, a Draw can be forced only if the string looks like this: ABC, where A is an arbitrary sequence of characters, C is its reverse, and B is a sequence of pairs of adjacent identical numbers. Example: abcgghhddcba. Thus, for the first part (palindromic part), if Alices chooses a side, Bob can choose the other and maintain the Draw, and for the second, Bob can always choose the same side as Alice.

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

      If we already know that Bob can never win, then we can proof as below.

      Proof: One necesary condition for a draw is "The occurence number of Every letter cnt[letter] is even".

      Let's proof that s=ABC by induction (where C is reversed A and B consists of pairs of identical letters standing next to each other).

      For len(s) == 2, it's obvious. Suppose len(s) > 2 and cnt[letter] is even for every letter.

      In the first turn, if Alice's best choice is s[1], and Bob's is s[n], s[1] == s[n], then by induction, s must be s[1] ABC s[n] which is also a form of ABC.

      If the first turn Alice's best choice is s[1], and Bob's is s[2],then s[n] = s[1] or s[n] = s[n — 1] or else Alice will win. If s[n] = s[n — 1],Alice can alway pick one from the same side before a letter equals s[n] is met,by the induction s is the form of B. If s[n] == s[1] and s[n] != s[n — 1], Alice can choose s[n], Bob can choose s[1], and the remains must be draw too, otherwise Alice have no reason to choose s[1]. By induction, s also has form B.

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

    Me after seeing other's D codes after contest and finding everyone used the same 100+ line dp algorithm and that doesn't remotely resemble my code.

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

(Problem E)

Let $$$d_1, d_2, ..., d_n$$$ be the sequence of values of $$$-a_i - b_i$$$ in a non-increasing order.

Shouldn't it be $$$-a_i + b_i$$$?

UPD: Fixed now, thanks!

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

for C my approach is like first remove element if it is present in both the arrays. After that only distinct element remains then decrease all the elements > 9 to single digits and add 1 to ans. Then repeat step 1 to remove duplicates. now only one digit distinct element remains for each of these elements add 1 to ans(if it is != 1). I think that my approach is right but it is not working can anyone tell why https://mirror.codeforces.com/contest/1728/submission/171533183

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

How to prove that Bob never wins in Problem D ?

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

    One way would be to look dp transitions. Having dp[i][i + 1] being equal to "Alice" or "Draw" there's no way to build winning dp of longer length.

    The other way would be the following: if Bob's string does not equal to Alice's, then it is no draw for sure. Consider there is some letter in result so Alice's string differ in that letter of Bob's string and all the other on prefix are the same. No matter how Bob plays Alice manages to take smallest letter when the left string is xSS...SSy. Regardless the step we encounter this configuration at. In fact Alice's strategy to take smallest letter at each step also leaves Bob no place to win.

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

Having the maximum values in C as $$$10^9$$$ actually allows for a simpler (imo) solution. A multi-digit number will always become a single-digit number by performing the operation. It's impossible for an operation to produce a multi-digit number.

So after you get rid of all the overlaps from the initial arrays, you can now safely perform the operation on every multi-digit number in both arrays (if a multi-digit number is present in one array but not the other, it's impossible for it to be generated on the latter, so you must apply the operation on it in the former). No sorting needed here.

Now everything is a 1-digit number. We can check for overlaps again, and get rid of them. When there are no overlaps remaining, we need to turn everything that isn't 1 into a 1, so we count how many non-1s we have, add that to our operation count, and get our final answer.

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

I'm going to link this $$$O(n)$$$ solution for Problem D here: https://mirror.codeforces.com/blog/entry/106726?#comment-951491

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

I have another solution for (1728E - Красно-черный перец) which runs in $$$O(K^2 \log_2{(K)} + M \frac{N}{K})$$$ where $$$K$$$ is a constant. Using $$$K = 150$$$ in my code has given me a runtime of $$$1606$$$ ms.

My Code: 171530099

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

I'll copy my approach for problem D from the annoucement :

First, because Alice play first and the length of the string is even, Alice can force Bob to take any particular character in the string by simply never take it. After Bob take this character either the string become empty and the game end, or it become a shorter string with even length, then we use this strategy again for the shorter string. By take advantage of this, when play optimally Alice will never lose.

The only way for Bob to draw is to always take the same character as Alice. Now we reverse the game from the end to begin to see what the original string would look like :

For each double turn of Alice + Bob, we add 2 identical character to the same side or different side of our string (for example : aa→aabb,bbaa or baab)

The critical point is that if we add to the different side, we can't add to same side anymore. Take the example baab→baabcc, here Alice can take b and Bob can't take any b so he will lose

So the form of our string is : half palindrome + some pairs + other half palindrome, the 1st and 3rd part form a palindrome

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

I have another solution for 1728E - Red-Black Pepper which runs in $$$O(N \log_2{(N)} + NK \log_2{(K)} + M \frac{N}{K})$$$ where $$$K$$$ is a constant. I have been able to obtain a runtime of $$$1606$$$ ms by setting $$$K = 150$$$ in this submission: 171530099.

My solution doesn't require the knowledge of diophantine equations and doesn't use some of the observations given in the editorial above. It relies on a square root idea.

Setting $$$K = \sqrt{N}$$$ results in a complexity of $$$O(N \sqrt{N} \log_2{(\sqrt{N})} + M \sqrt{N})$$$ which, I suspect due to poor constant factor, unfortunately TLEd for me.

Edit: Thanks to satyam343 for pointing out a flaw in my complexity analysis. I've fixed it.

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

    If I am not wrong, your solution runs in $$$O(N \log{(N)} + N \cdot K \log{(K)} + M \frac{N}{K})$$$.

    So on setting $$$K = \sqrt{N}$$$, your complexity is $$$O(N \cdot \sqrt{N} \cdot \log{(N)} + M \cdot \sqrt{N})$$$.

    Now you can improve the part of your solution which is contributing $$$N \cdot K \cdot \log{(K)}$$$. Instead of iterating over all numbers from $$$1$$$ to $$$K$$$, just iterative over all factors of $$$N-j$$$ which are smaller than $$$K$$$. This improves the constant factor.

    Here's my submission. I obtained a runtime around $$$1150$$$ ms by setting $$$K=500$$$.

    Edit: On putting $$$K=1000$$$, my submission runs in $$$826$$$ ms.

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

    I solved it in $$$O(N log_2(N) + NK)$$$ this is my solution

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

Very good round, hope more. -w-

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

In the editorial of E:

Then everything to the left of is is non-increasing. Everything to the right of it is non-increasing.

Shouldn't it be "Then everything to the left of it is non-decreasing"?

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

Can anyone please explain the solution to problem D? Why the question is of DP and not greedy?

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

    I solve it greedy in O(n) : if string s writing as s1 + s2 + s3 and s1,s3 is palindrome and s2[i]==s2[i+1] for any i :i is even then it is a draw else Alice win example string s=abbcca ,s=abeerrba ,s=abccba ,s=aabbcc here it is a draw here my submission :https://mirror.codeforces.com/contest/1728/submission/171626668

    but I suggest to solve it using DP suppose we have string of length n : s[l],s[l+1],,,,,,,,s[r-1],s[r] we have four cases : p1 : Alice choose s[l] and Bob choose s[l+1] ,p1=solve(l+2,r) if p1==0 then you need to compare s[l] , s[l+1]

    p2 : Alice choose s[l] and Bob choose s[r] ,p2=solve(l+1,r-1) if p2==0 then you need to compare s[l] , s[r]

    p3 : Alice choose s[r] and Bob choose s[l] ,p3=solve(l+1,r-1) if p3==0 then you need to compare s[r] , s[l]

    p4 : Alice choose s[r] and Bob choose s[r-1] ,p4=solve(l,r-2) if p4==0 then you need to compare s[r] , s[r-1]

    then the ans[l][r] = max(min(p1,p2),min(p3,p4)) you should calculate any substring with length 2 before here my submission using DP : https://mirror.codeforces.com/contest/1728/submission/171633331

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

Suppose Problem D also asked to print the two optimal strings that the players will select, how do we proceed then?

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

    You can build the same dp table as before, except in each entry, you also write down which indices $$$\ell$$$ and $$$r$$$ the dp value was copied from. The implementation gets a lot more annoying, but it's ultimately doing the same thing.

    After that, once the table is filled up, you can construct Alice's and Bob's strings by going backwards on the dp table. Start with $$$dp[0][n]$$$, check whether the value came from $$$dp[1][n]$$$ or $$$dp[0][n - 1]$$$, add the appropriate character to the Alice string, and then go to that corresponding entry. Use the same process to add the appropriate character to the Bob string, and so on, until the string has no characters left.

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

someone please make a video tutorial for D — Letter Picking... or if there is one then please point me towards it.

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

Could someone explaine a D problem case "baab": Alice can pick only "b", then Bob picks "a". So "a.." < "b.." anyway. So Bob wins, but the solution contradicts this?

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

    Since the letters are being prepended to the player's strings, if Alice chooses 'b' and then Bob chooses 'a', Alice will chose the other 'a' forcing Bob to take 'b' resulting in these strings below which makes Alice win:

    Alice : "ab"

    Bob : "ba"

    So Bob also chooses "b" in his first chance and in the end both have string "ab" which would result in a draw

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

My solution to the C problem was hacked 171387514. Next day I resubmited the same code 171579962 and it passed all the tests. How do I understand my solution basicaly is correct or not? I thought that the hachs are added to tests. Could someone clarify this please? (It's not neccesary to check my code, just explain how the system works)

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

Some clarifications on the tutorial for 1728G - Illumination

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

For question D:-171677046 simple o(n^2) solution using min-max We need to observe the fact that since Alice is going first therefore either Alice can win or it will be a draw. 1 means Alice wins and 0 means it is a draw. Also remember, (0|1)=1(bitwise or). I approached it considering all the possibilities when Alice is at [l,r]. It can follow with either Alice choosing s[l] or s[r]. dp[l][r] denotes who will win after considering substring s[l,l+1.....r].

If Alice chooses s[l] then Bob will choose either s[l+1] or s[r]. So, dp[l][r]=min((s[l]<s[l+1])|dp[l+2][r],ans3|(s[l]<s[r])).The first part of min function denotes the case when Bob chooses s[l+1] and the second part denotes the case when Bob chooses s[r]. The or operation is there to take the case that Alice has the advantage of going first. Similarly, if Alice chooses s[r] then dp[l][r]=min(dp[l|[r-2(s[r]<s[r-1]),dp[l+1]][r-1]|(s[r]<s[l])).


using namespace std; int find(vector<vector<int>> &dp,int l, int r,string &s){ if(l>r) return 0; if(dp[l][r]!=2) return dp[l][r]; int ans1=find(dp,l+2,r,s),ans2=find(dp,l,r-2,s),ans3=find(dp,l+1,r-1,s); dp[l][r]=max(min(ans1|(s[l]<s[l+1]),ans3|(s[l]<s[r])),min(ans2|(s[r]<s[r-1]),ans3|(s[r]<s[l]))); return dp[l][r]; } void solve(){ int n; string s; cin>>s; n=s.size(); vector<vector<int>> dp(n,vector<int>(n,2)); //initialiazation int ans=find(dp,0,n-1,s); if(ans==1) cout<<"Alice\n"; else cout<<"Draw\n"; return; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; cin>>t; while(t--){ solve(); } return 0; }
»
4 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Why am I getting TLE in Problem C?It seems like a O(n) solution.

#include<bits/stdc++.h>

using namespace std;

int calc(int k){
    return to_string(k).size();
}

int solve(){
    int i,j=0,n,z,c=0,one=0;
    cin>>n;
    vector<int> b(n);
    unordered_map<int,int> mp;
    for(i=0;i<n;i++){
        cin>>z;
        mp[z]++;
    }
    for(i=0;i<n;i++){
        cin>>z;
        if(mp[z]!=0){
            mp[z]--;
        }
        else{
            if(z>9){
                b[j]=calc(z);
                c++;
            }
            else
                b[j]=z;
            j++;
        }
    }
    if(j==0)
        return c;
    unordered_map<int,int> mp1;
    for(auto [x,y]:mp){
        if(y==0)
            continue;
        if(x>9){
            c+=y;
            mp1[calc(x)]+=y;
        }
        else
            mp1[x]+=y;
    }
    z=0;
    for(i=0;i<j;i++){
        if(mp1[b[i]]!=0){
            mp1[b[i]]--;
            z++;
        }
        else if(b[i]==1)
            one++;
    }
    c+=2*(j-z)-one-mp1[1];
    return c;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    cin>>t;
    while(t--){
        cout<<solve()<<"\n";
    }
    return 0;
}

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

In the solution for D. Letter Picking in the code inside the two for loops after these lines int r = l + len; dp[l][r] = 1; { why this brackets is used, these are used after for loop while loop if statements but what work these brackets are doing here in this code , I am unable to understand it

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

For problem G,why not DP?Dp from l1 to ln,only the leftmost unilluminated interest is useful,and we can do it similarly from ln to l1.

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

Alternate solution for G using binary search and inclusion-exlusion -
(POI = point of interest)
Accepted Solution

  • Sort lanterns and POIs by coordinate
  • For each mask, calculate the number of ways that at least all the POIs whose bits are set are unlit
    (eg. for mask 101001, POI 0, 2 and 5 should be unlit, rest can be whatever)
    To do this, you binary search for leftmost lanterns which have each POI in the mask as their closest, if lanterns $$$l_a$$$ through $$$l_b$$$ have $$$p_i$$$ as the closest POI, then multiply number of ways for that mask by $$$|p_i - l_a|*|p_i - l_{a+1}|*...*|p_i - l_b|$$$, let's call this $$$ways_{mask}$$$
  • For each mask, find the number of ways that exactly all the POIs whose bits are set are unlit
    You can do this using inclusion-exclusion, let's call this $$$ways_{mask}'$$$, then $$$ways_{mask}' = [\Pi_m ways_m*(-1^{popcnt(m)+popcnt(mask)})]$$$ where $$$m$$$ & $$$mask = mask$$$
  • With $$$ways_{mask}'$$$ computed, we can move to answering the queries -
    Let the new lantern be at $$$pos$$$ For each POI, calculate distance from $$$pos$$$ and sort them in decreasing order, let's say the distances are $$$[4, 2, 1]$$$ for POIs $$$[2,0,1]$$$, then we calculate the sum of $$$ways_{mask}'$$$ where mask contains bit no. 2, for all these masks, if the new lantern has a brightness of 3 or less, all POIs are still not lit. Next we calculate the sum for masks where mask contains bit no. 0 but not bit no. 2, for all these masks, if the new lantern has a brightness of 1 or less, all POIs are not lit. We continue this procedure m times, giving us the final number of ways in which the POIs are not fully illuminated
    For sum of $$$ways_{mask}'$$$ and for calculating $$$ways_{mask}'$$$ in the first place, you would need to use SOS DP

Final time Complexity — $$$O(m*2^m*log(n) + q*m*log(m))$$$

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

For the tutorial on Problem E — since we're checking how many dishes with black peppers we are going to include in our answer, it should say "Then we would want to check the answer for b*y black peppers" Right ?

Great problem and editorial btw. (y)

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

I found a simplier soution to problem E. It doesn't need any observations about the "non-strictly convex" property of tastiness and I didn't had to deal with diophantine equations. We can check all possibilities! There is not so much of them.

Sort the shops by their x_j values, (ans secondarily by their y_j values). For considering only the x_j values first, if x_j=1 you have N possibilities, if x_j=2 you have N/2... All in all you have (N+N/2+N/3+N/4+...N/N) possibilities, which is ~ NlogN,2022-12-06 posibbilities. For a fixed x_j, check all possibilities considering y_j too, that is (N/x_j)/(y_j) possibilities.

So all in all, u have to check NlogN+N/2log(N/2)+N/3log(N/3)... possibilities, which is

So this solution runs around O(Nlog^2N) and passes in 1 sec.

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

For problem F, the tutorial mentions "there are some implementations of Kuhn's algorithm which can run on graphs of size about $$$10^5$$$". It suggests one optimizing strategy that works here (don't always reset visited markers) and one that would fail here (greedy initialization??).

Can someone suggest a source where I could read more about different possible optimizations of Kuhn's algorithm depending on which assumptions are valid about our input graph? I searched around a bit but was not able to quickly find a good source or library code.

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

1728G — Illumination can also be solved using dp in $$$(n + q) * m ^ 2$$$ complexity. Here's my submission.