11

Codeforces Round #807 (Div 2.) Editorial

 2 years ago
source link: http://codeforces.com/blog/entry/104881
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Codeforces Round #807 (Div 2.) Editorial

Thanks for participating. We apologize for problem F that has appeared before. Still, we hope you all enjoy our round!

1705A - Mark the Photographer

Author: MarkBcc168

Hint 1
Hint 2
Tutorial
Code

1705B - Mark the Dust Sweeper

Author: MarkBcc168

Hint
Tutorial
Code

1705C - Mark and His Unfinished Essay

Author: MarkBcc168

Hint 1
Hint 2
Hint 3
Tutorial
Code

1705D - Mark and Lightbulbs

Author: MarkBcc168

Hint 1
Hint 2
Hint 3
Tutorial
Code

1705E - Mark and Professor Koro

Author: abc241

Hint 1
Hint 2
Hint 3
Hint 4
Tutorial
Code (Bitsets, by errorgorn)
Code (Lazy Segment)

1705F - Mark and the Online Exam

Author: MarkBcc168

Unfortunately, a harder version of this problem has appeared in a Chinese contest here and here. You can look at their solution here. We thank many contestants who pointed it out.

Hint 0
Hint 1
Hint 2
Hint 3
Hint 4
Tutorial
Code

4 days ago, # |

Can you continue in the sun time next time?

4 days ago, # |

So fast Editorial...Love it.

  • 3 days ago, # ^ |

    The editorial with hints is excellent :)

4 days ago, # |

Rev. 2  

+23

C was nice

4 days ago, # |

A and B were normal. C was kinda nice. D felt kind of easier than normal.

4 days ago, # |

why my solution is giving memory limit 164324383

  • 4 days ago, # ^ |

    Rev. 5  

    +1

    C in that problem can be up to 40, so the string can have a length of up to n times 2 powered by 40. That is because the string can be "duplicated" if they simply spam 1 as L and the current length of it as R, so it becomes twice as big. But you can not make a string of that size, because it takes too much memory, so you get MLE.

    • 4 days ago, # ^ |

      Oh okay, I got it..Thanks a lot.

  • 4 days ago, # ^ |

    l, r⩽10^18, means r-l ⩽ 10^18 this is already a large value, even if we do not take into account that q can be of the order of 10^4

  • 3 days ago, # ^ |

    pay attention to the range of data, especially 1e18.

  • 4 days ago, # ^ |

    Note that the length of the string can be upto 240⋅200000240⋅200000 because the copying operation could be [1,n][1,n], [1,2n][1,2n], [1,4n][1,4n], ……, [1,239n][1,239n].

    Your solution has a loop from l to r, which varies by the length of the string. Therefore, it can never loop through in time.

    • 4 days ago, # ^ |

      ok Thanks a lot!

4 days ago, # |

Rev. 3  

+8

Good Tasks for Codeforces Round #807)

Editorial is perfect.

4 days ago, # |

So fast Editorial!!!!!

  • 4 days ago, # ^ |

    you are 607 mouse ?

  • 4 days ago, # ^ |

    you are 607 mouse ?

4 days ago, # |

Problem C was just awesome <3

4 days ago, # |

Thanks for the superfast editorial, interesting round with mArK!

4 days ago, # |

If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints.

Disclaimer: There is a quota of 1 request per 24 hours on the free plan.

4 days ago, # |

Hints are always helpful.

4 days ago, # |

Wow, very fast editorial! I like this contest because the problems are easy to understand. Great job!

4 days ago, # |

I thik C is very nice

4 days ago, # |

Rev. 2  

0

for me, D is more difficult than E even than F
(though I didn't solve E or F because typing speed)

  • 3 days ago, # ^ |

    I am sorry to keying D as C

4 days ago, # |

So fast Editorial!!

4 days ago, # |

Question D is so difficult

4 days ago, # |

Amazing tutorial! It made me everything easy to understand.

4 days ago, # |

The tutorial for D "Moreover, if a1,a2,…,ak are the positions of 1's in s and b1,b2,…,bk are the positions of 1's in t. " , should here s/t be s¯¯¯ and t¯ ?

  • 4 days ago, # ^ |

    Great catch! Fixed.

4 days ago, # |

which topic i should learn for solving problems as C?

  • 4 days ago, # ^ |

    There is no specific knowledge required to solve C. You can try reading the hints and tutorial provided above.

    • 4 days ago, # ^ |

      ok i got it but always i solve A, B in div2 but i cant solve any C problem in div 2 could you give me some tips for improving) thx

      • 4 days ago, # ^ |

        the best way to improve at problems like div2C is through problem solving and gaining experience

      • 3 days ago, # ^ |

        Practice more div2Cs would be OK.

        Try to solve them as quickly as you can.

  • 3 days ago, # ^ |

    C is just about implementation

  • 3 days ago, # ^ |

    I though the observation that for each query you have to travel back through the (c<=40) operations was the hardest part.

4 days ago, # |

Rev. 2  

0

It was super hard for me. I should do more practice

4 days ago, # |

Rev. 4  

+7

it's funny how bitset codes pass (in E) but my std::set solution TLE'd until i replaced them with fenwick trees

desperate pretentious and angry comments
    • 3 days ago, # ^ |

      Rev. 2  

      +3

      This made my day a whole lot better i will just go and jump off a cliff now. Thank you, stranger

4 days ago, # |

very fast editorial

4 days ago, # |

D was really good. I don't know why I find such kinds of problem a bit hard. I wasn't able to come up with the invariant that sum of (01)(01) and (10)(10) is constant. Can someone suggest set of problems like DD (where I need to recognise an invariant)?

  • 4 days ago, # ^ |

    Same bro :(

  • 4 days ago, # ^ |

    It is easy to come up with solution for D by observing carefully.

    1. Say we have 10001 then we can convert it to 11101 -> the first block of 1 got expanded to last continuous 0 we had

    2. Say we have 11101 then we can do the opposite and convert it to 100001 -> the first block of 1 got compressed to first continuous 1 we had

    This is for left to right . U can observe similarly for right to left. Now this implies that if i have some block of 1 i can always expand(through continuous zeroes and stop before last zero) or compress it(through continuous ones and stop after 1st one). So we can easily notice that -1 case will be when count of blocks of 1 in s and t are different. Also there is that case for first and last character to be same which can also be deduced by the observation since when expanding [x,y] to [l,r] (l<x , r>y) element at l and y will never change.

    There we you can now just expand and compress these blocks greedily and make them same now.

    • 3 days ago, # ^ |

      this was my sol as well.

  • 4 days ago, # ^ |

    One perhaps easier solution is, if you separate the bit string into substrings containing either only ones or only zeros, you realize that the operations we can do only allow for moving borders between neighboring substrings. Then the solution exists only if the order of the substrings is matched and the minimal number of operations is calculated by differences in positions of borders of the substrings.

  • 4 days ago, # ^ |

    I think the invariant that the number of chunks of 1 stays the same is easier to be observed.

  • 4 days ago, # ^ |

    Same for me :/

    After the contest I continued to try and solve D and was surprisingly able to come up with a constructive solution that works, but it was hell to make. As a new expert who hasn't solved that many Ds before I was very pleased to read the editorial and realize that what I did wasn't the expected smart solution.

    Here is my code (it isn't pretty, but I will never touch it again): 164362277

  • 3 days ago, # ^ |

    I only solved that problem b/c I saw 3+ similar problems in the past and I knew which direction to push my solution in, if that makes any sense.

  • 3 days ago, # ^ |

    This could be helpful: https://codeforces.com/blog/entry/85172 (there is one problem with an invariant)

4 days ago, # |

my english is not well, so at that time ,i forgot this race ,however, i think there exit a long way for me to aprove my algorithm

4 days ago, # |

So fast editorial and fantastic contest!

4 days ago, # |

C was kinda hard for me, but I really enjoyed solving it. Great problem

  • 4 days ago, # ^ |

    yeah I got the idea from josephes problem finding the value of index in previous recursion using mathematics

4 days ago, # |

Thanks for the fast tutorial ...

4 days ago, # |

Enjoyed this contest — definitely has a good math olympiad flavor to it (both problems and solutions)!

4 days ago, # |

Does qn D answer overflow int ? passed three pretests then failed.

  • 4 days ago, # ^ |

    The maximum possible answer is on the order of n^2, so yes, there is an overflow, and you have to use long long.

    • 3 days ago, # ^ |

      Yup that was the problem,felt horrible to lose a qn like that,also i feel the bitmask soln is over complicated,all i did was checked that first and last same,the number of continuous groups of 1 same ,since a group of cont. ones can never split up or merge with another group of ones,then check if number of groups in s and t same ,then calculate distance by moving start and end points of each group in s to corresponding one in t.

4 days ago, # |

C was bit tricky for me, Good problemset overall. Happy Coding & Happy friday :)

4 days ago, # |

Rev. 4  

0

This contest was great, I wish I hadn't had classes until 8:45pm (quite sad). Anyways, that's a rapid editorial.

4 days ago, # |

Thanks for fast tutorial and E was cool.

4 days ago, # |

Does somebody have binary search solution for C?

  • 4 days ago, # ^ |

    Not needed

  • 4 days ago, # ^ |

    Rev. 2  

    0

    You can see 164372123

    But as the range of c is too little 1≤c≤40 , it doesn't make that difference.

4 days ago, # |

Awesome round and woah super fast tutorial. Absolutely loved it ❤️. C was special in many ways.

4 days ago, # |

problem C was breathtaking for me :D

4 days ago, # |

A and B was normal, but C was hard :)

4 days ago, # |

Hi all, if any of you could please give suggestion on this, please do so.

  1. I am able to solve at least A problem but still get negative rating after contest.. could u please tell why does that keep happening... I have been a user here for a while but just recently started being active here...

  2. I recently learnt there is hacking phase here during contest after passing pretests. how can one lock their solution? and if someone uses your code, would that mean you will get penalty for it..

thanks in advance.

4 days ago, # |

Fun fact: something happened during testing of problem F related to different behaviours of different versions (and 64-bit or not) of g++ and we couldn't figure out the reason behind it, so I'm putting it here to see if anyone can offer some insights into the different behaviours, thanks in advance :D

This submission gets AC:

Submission

But when I submit the same code in non 64-bit version, it gets WA:

Submission

But once again when I remove two lines from the header it gets AC again, however, those two lines don't seem to be relevant at all to the algorithm:

Submission
  • 4 days ago, # ^ |

    Rev. 4  

    +22

    It has to do with unsigned ints, in this case vector.size(). Casting all .size() values to int in the second submission makes it AC.

    Here's what I think is happening here:

    The first submission passes since you are dealing with 64-bit numbers on both sides, so converting unsigned to signed you get the same number even after overflow, so == works ok. Likewise for the third submission with 32-bit numbers.

    For the second submission, you convert a signed integer to an unsigned one in 32-bit (after some possible overflow), which is not the same as the signed int in 64-bit, so == doesn't work as intended. Removing #define int long long fixes this.

    Moral of the story: always cast .size() to a signed int to avoid situations like this.

    • 4 days ago, # ^ |

      Thanks a lot, that would explain what happened.

  • 3 days ago, # ^ |

    [unrelated] why are you removing the legendary line :(

    #define ick cout<<"ickbmi32.9\n"

4 days ago, # |

Rev. 2  

0

isn't the TL on D too tight?

My code with long long int gives TLE but when I change long long int to int, it passes in 600ms.

My solution is O(nlog2n)O(nlog2n). Was this done on purpose to cut the solutions of this logistic?

  • 4 days ago, # ^ |

    Based on our testing, the segment tree has a high constant factor that an O(nlog2n)O(nlog2⁡n) is very hard to pass. You should either use bitsets or aim for O(nlogn)O(nlog⁡n). We apologize for the strict TL.

  • 4 days ago, # ^ |

    The comment is hidden because of too negative feedback, click here to view it
    • 4 days ago, # ^ |

      I disagree, the O(nlog2n)O(nlog2n) solution was more obvious to me. I couldn't come up with that observation.

  • 4 days ago, # ^ |

    Can you share your Approach towards problem D?

4 days ago, # |

E, I would like to read a simpler version of the paragraph starting with "Proof: The upper bound is pretty clear...".

What is the upper bound here? What exactly is decreasing?

  • 4 days ago, # ^ |

    This was written when there was the second operation (of deleting numbers). The editorial is updated now. Hope this eliminates your confusion.

4 days ago, # |

Rev. 3  

+84

I'm very bad with segment trees, so I came up with the following solution for problem E. Let's maintain two sets SS and TT ("positive" and "negative" sets) such that

∑2ai=∑s∈S2s−∑t∈T2t.∑2ai=∑s∈S2s−∑t∈T2t.

The following properties should hold:

  • No number appears in both SS and TT.
  • The largest element of TT (if it exists) is not equal to the largest element of SS minus 11.

The second property is important because it allows us to answer queries by taking the largest element of SS and subtracting 11 if the largest element of TT is greater than the second largest element of SS. (If the property didn't hold, we might need to deal with something like 26−25−2426−25−24.)

To add 2x2x, do the following:

  • If xx is in TT, cancel it out.
  • Otherwise, if xx is not in SS, add it to SS.
  • Otherwise, remove the copy of xx in SS and add 2x+12x+1 instead (recursively).

Subtraction is entirely analogous. To maintain the second property, just check if it doesn't hold and if so, delete xx from SS, x−1x−1 from TT, add x−1x−1 to SS, and check the property again.

For all of these operations, we can do an amortized analysis by using |S|+|T||S|+|T| as a potential function. The overall time complexity is O((n+q)logn)O((n+q)log⁡n) since we use sets to maintain the maximum.

The implementation of this is very easy compared to the other solutions.

  • 4 days ago, # ^ |

    In the complexity, how do you account for the 'recursive' updates? Is there a bound on the number of updates? (a constant factor maybe? Certainly not just n+qn+q updates)

    • 4 days ago, # ^ |

      Rev. 2  

      +3

      That's what the amortized analysis handles, because all the extra steps that we do will reduce the value of |S|+|T||S|+|T|. You should be able to find some resources by searching "amortized analysis" and "potential method".

4 days ago, # |

Rev. 3  

+3

Other simpler in understanding approach for D: (TC: O(q*n*logn)):

  • s==t -> ans=0
  • basic check s[0]!=t[0] or s[n-1]!=t[n-1] -> ans=-1
  • create can_toggle[i] = (s[i-1]!=s[i+1])
  • want_toggle[i] = (s[i]!=t[i])
  • iterate from 1 to n-2
  • if want_toggle[i] = false, move on
  • if want_toggle[i]=true -> get id>=i such that can_toggle[id]=1 (can use binary search or monotonous can_toggle queue too)
  • if no such id present -> ans=-1
  • revert all want_toggle between i and id (i.e say add 1 to range[i,id] or other way xor with 1 for [i,id]) (use Binary index tree/segment tree Logn comes from here)
  • ans += range[i,id] i.e id-i+1;
  • using can_toggle[id] we reverted state of can_toggle[id+1] too, add this can_toggle[id+1] to our consideration if its true
  • TLDR: for each want_toggle[i] we get just right can_toggle[i] and bring that can_toggle to current i by toggling id, id-1, id-2.....
  • Idea comes from fact that when we use toggle at i the state of can_toggle[i-1] and i+1 changes.

Submission: https://codeforces.com/contest/1705/submission/164342095

  • 4 days ago, # ^ |

    in b what if the array is 3 0 0 0 0 2 0 0 4 how it work ??

    • 4 days ago, # ^ |

      Initially: 3 0 0 0 0 2 0 0 4 1 => 2 1 0 0 0 2 0 0 4 2 => 1 1 1 0 0 2 0 0 4 3 => 0 1 1 1 0 2 0 0 4 4 => 0 0 1 1 1 2 0 0 4 5 => 0 0 0 1 1 2 1 0 4 6 => 0 0 0 0 1 2 1 1 4 Now you can see that for clearing the rest, you need a minimum of (1+2+1+1) = 5 turns Total: 5+6 = 11 turns

      • 3 days ago, # ^ |

        the array should be 0 0 0 0 1 2 1 1 4 6 not 0 0 0 0 1 2 1 1 4
        the final answer is 9+6

4 days ago, # |

Rev. 2  

+3

C is a nice implementation problem. It also gave me a lesson:

BE EXTREMELY CAREFUL using int when you see anything like x≤1018x≤1018 or x≤1012x≤1012 . I literally spend ~1h struggling on C because of this :(

  • 3 days ago, # ^ |

    Yes, you are right, I made a lot of mistake on intint and long longlonglong.

    In a contest, you can try this:

    #define int long long
    ……
    signed main()
    {
    
    
    }

    **** And when you use printf to output, don't forget to use "%lld" instead of "%d".

4 days ago, # |

Rev. 3  

0

Can anyone please explain why for problem C, there is a runtime error in my solution. solution

.......got my mistake.(solved)

  • 4 days ago, # ^ |

    Coz of integer overflow. You need to use long long. Made the same mistake :")

4 days ago, # |

Nice contest! Thanks for the fast editorial! :)

4 days ago, # |

164338999 Can't understand why I am getting tle in C. Please help.

4 days ago, # |

I followed the editorial solution but I'm getting error for a particular test case that isn't even visible and I can't figure out why. Any help!? Here's my solution: https://codeforces.com/contest/1705/submission/164347601

  • 4 days ago, # ^ |

    Take a look at Ticket 15743 from CF Stress for a counter example.

4 days ago, # |

PROBLEM can someone plz explain why this code didnt work!! please

  • 4 days ago, # ^ |

    Take a look at Ticket 15750 from CF Stress for a counter example.

4 days ago, # |

Spoiler (Problem C)

4 days ago, # |

Funnily enough, in E, I never realised that we are just maintaing a binary number. The final algorithm you get is very similar to the editorial, albeit slower by a constant factor.

Here's what I did, let fxfx be the frequency of xx on the blackboard. Let gxgx be the maximum possible frequency of xx after some operations. It's easy to see that gx=fx+⌊gx−1/2⌋gx=fx+⌊gx−1/2⌋. The answer is simply the largest zz such that gzgz is non-zero.

Now, if we can somehow efficiently maintain gg after ff increases/decreases by 11 we'd be done.

Suppose we decrement fxfx by 11. Consider what happens to gg, let's call it g′g′ after the change. Obviously g′y=gygy′=gy for all y<xy<x, and g′x=gx−1gx′=gx−1. What about the following values?

g′x+1=fx+1+⌊g′x/2⌋=fx+1+⌊(gx−1)/2⌋={gx+1gx+1−1gx is oddgx is even.gx+1′=fx+1+⌊gx′/2⌋=fx+1+⌊(gx−1)/2⌋={gx+1gx is oddgx+1−1gx is even.

In the former case, we don't need to propagate the changes any further, in the latter case, it's as if we reduced $f_{x + 1}$ by 1 and the process repeats.

In effect, we find the maximal interval [x,r)[x,r) such that gygy is even for all y∈[x,r)y∈[x,r) and then reduce gygy by 11 for all y∈[x,r]y∈[x,r]. Similar thing happens on incrementing fxfx.

We can maintain all this information using two lazy segment trees, one storing gxgx and gx mod 2gx mod 2 on the leaves, and their sum in the internal nodes. The first segtree supports range increment, and the second range flip operation.

4 days ago, # |

Can someone please recommend some problems similar to D?

4 days ago, # |

Rev. 2  

0

problem C can anyone tell me why my solution is giving runtime error[submission:164331309][click here](https://codeforces.com/contest/1705/submission/164331309)

  • 4 days ago, # ^ |

    From diagnostics: "Probably, the solution is executed with error 'division by zero' on the line 37".

    • 4 days ago, # ^ |

      thanks . I will try to fix out

4 days ago, # |

Why is my solution for C throwing a memory limit exceeded error?

I am iterating over the queries and splitting the queries with length > n as subcomponents and evaluating the final answer.

I am essentially generating 40 * 40 * 1000 (q^2 * TC) query ranges for each test case which should still pass the given memory limit right?

4 days ago, # |

Rev. 2  

0

I don't get how to convert E to those binary numbers ? I mean i get the part for the sum being constant. But what about the condition of first number in the streak of 1's appearing more than once?

4 days ago, # |

can anybody explain the editorial by taking an example for problem c? im not understanding editorial

4 days ago, # |

Does anybody have the same idea with me? it got a Memory Limit Exceeded 164356635

  • 4 days ago, # ^ |

    I'm trying to enumerate the all indexes for lowercase chars

    m['a'~'z'][po1, po2, po3] when the query gives me a pos, I will search it and return the char. I think the pos array is too large

    • 3 days ago, # ^ |

      It doesn't change that much storing the position that each element of the final string is equivalent to in the original string (as you are doing) and just constructing the final string, because in both instances you are storing the equivalent of the final string's length in elements, which can be up to 10^5 * 2^40 (n duplicated c times) a.k.a: way too much.

      So your guess is correct, the position array is getting too big.

4 days ago, # |

Thanks for the superfast editorial, interesting round with mark!

4 days ago, # |

Can anyone please tell me what's wrong in my submission of C.

164359468

I wrote the code simple so that you understand.I just don't understand what's wrong in the idea.I used the index of the first element of each operation and distance of the indexes of asked element and first element of the operation.One glance to the code will be much appreciated :(

  • 4 days ago, # ^ |

    Take a look at Ticket 15738 from CF Stress for a counter example.

    • 4 days ago, # ^ |

      Rev. 4  

      0

      Thank you so much bro!!!!! Being depressed I almost started a mission to get the 4992-th testcase using this:

      if(cas==4992) { cout<<n<<"&"<<cc<<""«s«" "; for(i=0;i<cc;++i) { cout«lll[i]«"&"«rr[i]«""«s«" "; for(i=0;i<cc;++i) { cout«lll[i]«"&"«rr[i]«""; } return; }

      You saved me !

    • 4 days ago, # ^ |

      Bro please add stress test for 1702 — E.It's been 6 days

      • 4 days ago, # ^ |

        I don't add problems on demand. (Not on the free plan at least).

        • 4 days ago, # ^ |

          On what basis do you add problems?

          • 4 days ago, # ^ |

            If I feel like it, or when someone pays me to. You can find the details on the pricing page.

4 days ago, # |

For problem B, I started traversing the array from the second last element(n-1th for 1 Indexing). If the element is 0 and the previous element is a non-zero element, then that particular 0 will contribute in reducing the non-zero element. Thus, it will take two steps to reduce that 0 to 0 again(0 to 1 and again 1 to 0). So add 2 to the answer. If the element is non zero, then first decrement that element with the number of steps taken by the 0 element(if any) which was present next to it. Now, the dust remaining at that index is the actual dust which require individual steps to get reduced to zero. So add that number to answer.

Here is the code for the same.

include

include<bits/stdc++.h>

define ff(i,n) for(int i=0;i<n;i++)

using namespace std;

void solve(){

int tc; cin>>tc; while(tc--){ int n; cin>>n; vector vect(n,0); int i=0; ff(i,n){ cin>>vect[i];

}
   int curr = 0;
   int ans = 0;
   for(int i=n-2;i>=0;i--){
       if(vect[i]==0 && i>0 && vect[i-1]>=0){
           ans+=2;
           curr = 1;
       }
       if(vect[i]!=0){
           vect[i]-=curr;
           ans+=vect[i];
           curr=0;
       }
   }

   cout<<ans<<endl;

int main() { solve(); return 0; }

This logic is somehow not working and I am unable to determine the failed test case. Can anyone please help.

  • 3 days ago, # ^ |

    Rev. 2  

    0

    Try this test:
    
    Input:
    1
    5
    2 0 0 0 0
    
    Output:
    5
    • 3 days ago, # ^ |

      Got it. Thanks.

4 days ago, # |

One of the best CF rounds in a while!

4 days ago, # |

C was kinda tricky.

4 days ago, # |

I marked the topics I need to study

4 days ago, # |

For B: what if you can't fill all the zeros. for example, the input : 2 0 0 0 3 0 0 0 0 0 6 . In this input, you can't make all the zeros into ones and apply the operation on i and j. You would end up with 1 1 0 0 1 1 1 0 0 0 6. But then you haven't filled all the zero entries have you? You would then have to move the 1s to the right?

  • 4 days ago, # ^ |

    After you end up with 1 1 0 0 1 1 1 0 0 0 6, you can move 1->3, to do operation i->j, all elements from i to j-1 must be >=1, but the j-th element could be equal to 0 :)

4 days ago, # |

> be me
> reading editorial
> read "This is a well known fact"
> sure bud

4 days ago, # |

Thanks for the great contest!

Here is my solution to E which used ChthollyTree, i.e., use set to maintain all the intervals.

actually, we just need these operations and there are only two types of values: 0 and 1. :

  1. set interval

  2. find first a after b.

because:

operate +1 on position x:

  • a[x] == 0: set a[x] to 1

  • a[x] == 1: find the first 0 after x, assume on pos y; then set a[y] to 1, set a[x..y] to 0.

operate -1 on position x:

  • a[x] == 1: set a[x] to 0

  • a[x] == 0: find the first 1 after x, assume on pos y (always exist); then set a[y] to 1, set a[x..y] to 1.

So we can use set or map to maintain the 1 intervals.

here is my code: https://codeforces.com/contest/1705/submission/164369724

it is written by Rust, but the logic and comment are still suitable for all languages.

4 days ago, # |

I wonder that why I fail to hack to this code.

link

int a[110]; The size of the array is only 110110, but when n=100n=100, and there will be 200200 numbers, why it will not get WA / RE?

Can anyone tell me, thanks a lot.

4 days ago, # |

Rev. 3  

0

in problem D: I can't understand why the XOR is the key idea? I see how this applied on the 4th test case but Why not we lucky that this is the case with the 4th test case only?

  • 3 days ago, # ^ |

    In my opinion, after serveral operations, the number of the sum of 0101 and 1010 will not change.

    The problem says that si−1!=si+1si−1!=si+1, for example, "100" will change to "110", and "001" will change to "011".

    We can find that the count of differences of the adjoining numbers will not change.

    In fact, XOR can find that if the adjoining numbers is not same, because 0 XOR 1=10XOR1=1, 1 XOR 0=11XOR0=1, 0 XOR 0=00XOR0=0, 1 XOR 1=11XOR1=1.

    I hope this solution can help you.

    • 3 days ago, # ^ |

      Thanks alot

4 days ago, # |

Rev. 2  

0

For D, I understand how "cool" the solution looks like, but seriously, how can I even approach the XOR thing.

  • 3 days ago, # ^ |

    Personally I don't think I would ever directly land in the xor thing. The human and logical approach to get to the solution and possibly to that xor realization would be to look for blocks/groups of elements that have some relation with each other or certain relevant behaviour, that's a common and effective idea that you can train yourself to pay attention to in some problems and can become better in finding/identifying.

    For example, one group you might find is that all the elements with odd positions can only change the elements with even positions and vice-versa. But a more important one in this problem are the groups of ones and zeroes for example, you might realize that you can expand and contract those as much as you want, from there you start to get somewhere.

3 days ago, # |

Rev. 6  

-10

F can be solve in n/2.08n/2.08 times. Here is another solution: blog (in Chinese).

3 days ago, # |

F is the same as this

and solution here 20210527

3 days ago, # |

Problem E can also be solveed by set. I store the segments of continuous 1. For examplpe, 10110111 may be stored as {1,11,111}. We also stored the segment's left bounder and right bounder, so when we need to check a position, we can use std::set::lower_bound to find a section that may include that position. Modifying the set is similar as the solution of segment tree, including splitting a section, and merge two sections, tottal complex is NlogN.

3 days ago, # |

Can somebody tell me how to think of D's solution? I think, it's just a little hard to think of. So is there anything lead to the soltuion? thx.

  • 3 days ago, # ^ |

    Bro, I used to be very afraid of this kind of problems. But realize that the main approach is observing and trying. I found the solutution by this way: 110->100 100->110 This transform told me nothing, so I tried to write some long strings, such as the strings in the sample cases: 000101-> 001101 -> 011101 -> 011001 -> 010001 ->010011. I read this transform from the node under the problem statements. It is really amazing that 000101 can turn to 011101. It means that 0001 can turn to 0111, further more, 0001000 can be turn to 0111110. That is, you can freely extend or shrink a continuous ones(1111...) or zores(000...), but you can't make a section disappear. This lead to a great conclusion: If string s1 start by 1 and end by 0, then s1 is like this: 1..0...1...0...1..0.. Realizing that you can extend or shrink a section(but you can't make any section disappear), so if s1 can be transformed to s2, then s2 must be this way: 1..0...1...0...1..0.. The number of sections is the same as s1.

    The problem has almost been solved! Please finish the remaining work.

    • 3 days ago, # ^ |

      Oh yeah, i got it. It's just observing and trying to find the clues as you are calculating the samples. thx a lot, bro!

3 days ago, # |

All good fellows. Congratulations on your success. Problem C was interesting and good, wasn't it, friends?

3 days ago, # |

Rev. 2  

+6

I've decided to create my own editorial for the problems I solved(A-D)

A
B
C
D
  • 3 days ago, # ^ |

    Our solutions are identical, but I find that my vocabulary is not enough when writing editorials.

  • 35 hours ago, # ^ |

    Hi, thank you so much for your take on the problems! If you don't mind could you explain the logic behind this line of your code in problem C:k = operations[i].second - (origlen[i] - k);

    • 31 hour(s) ago, # ^ |

      Since we're going back in time, right now we know that the index of the kth character must be between the left and right values of operation i. This is true because operation i was the first value we found >= k, so the above statement must be true. What "origlen[i] — k" does is that it gets the distance between the end of the string and the kth character. But since the end of the string is the rth character of the previous string, we can reduce k by going that same distance down from that right value instead.

      • 22 hours ago, # ^ |

        That makes it so much more clear, thank you!

3 days ago, # |

The editorial with hints is excellent :)

3 days ago, # |

what's wrong with this submission for problem C. I'm using the same approach, but it is showing WA on test 2 ? link

3 days ago, # |

D is so beautiful that I used the second method and did'nt finish now!

3 days ago, # |

The comment is hidden because of too negative feedback, click here to view it

3 days ago, # |

The complexity of using bitests to solve D is O(n2/w)O(n2/w). But the maximum of n is 2∗1052∗105. I think this complexity can not pass the problem.

  • 3 days ago, # ^ |

    Can anyone explain this?

    • 3 days ago, # ^ |

      I can give you answer that "bitsets are fast lol" but I think that is not very satisfying so I shall summon sslotin and hopefully he can give a better explanation ;)

      • 3 days ago, # ^ |

        Bitsets are fast lol.

        But seriously, if you are performing a simple bitwise operation, then, by the virtue of vectorization (processing a block of 256 bits per instruction) and instruction-level parallelism (capacity to execute two such instructions per cycle), w=512w=512, or about 512×3.60×109≈2⋅1012512×3.60×109≈2⋅1012 bitwise operations per second on CF server, which theoretically even lets you submit a O(n2)O(n2) algorithm for n=106n=106.

        • 2 days ago, # ^ |

          Thank you very much for your explanation.

3 days ago, # |

The editorial of D is a bit obscure for what it is. A more natural way to interpret and make the key observation is to note that the number of "segments" of 1's, alongside the endpoints of the string, are invariant under operations. After checking this condition, simply notice that each operation moves a boundary of some segment by 1 unit (eg. 011000 -> 011100 is expanding the right boundary by 1), so repeatedly greedy matching the leftmost segments of both strings would give the answer if we take the absolute differences between their boundaries.

3 days ago, # |

Is there a way that i can access the testcase on which i fail? when i click on my failed submission it shows first few cases of each test but if it is failing the 272nd testcase then can i know what it was?

  • 3 days ago, # ^ |

    Actually there is no method to get the testcases.

    • 3 days ago, # ^ |

      thanks for the info, i think its good if i figure out the mistake on my own

3 days ago, # |

C is nice problem

3 days ago, # |

Nice round.I would like to have problems descriptions in MORE LANGUAGES。

3 days ago, # |

In problem D , why did we XOR like we did in solution? How to arrive at this conclusion of XORing , please help!

3 days ago, # |

c was quite good ,it seems easy but it is not...

3 days ago, # |

F can be solved with "Entropy" (https://en.wikipedia.org/wiki/Entropy_(information_theory)) .

You can split the positions into some groups , and each group includes at most B elements .

You find the positions of 'F' of each group independently , and use Entropy to find the optimal choice . which can be solved in O(22B)O(22B) for each group .

Solution: 164420897 ,(B=12, and the number of queries is about 520 for n=1000).

3 days ago, # |

can somebody explain me the solution of problem C , i am unable to get it . Thanks You.1705C - Mark and His Unfinished Essay

40 hours ago, # |

F is a great problem, thank you!

102 minutes ago, # |

I want to see solution O((clogc)q) or solution O(c*c*q) of problem C.

3 minutes ago, # |

Can someone kindly explain C.

Didn't understand the editorial.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK