the_nightmare's blog

By the_nightmare, history, 12 months ago,

1527A - And Then There Were K

Author: loud_mouth
Idea: Bignubie

Editorial
Solution (Loud_mouth)
Solution (the_nightmare)

1527B1 - Palindrome Game (easy version)

Author: DenOMINATOR
Idea: shikhar7s

Editorial
Solution (DenOMINATOR)
Solution (shikhar7s)

1527B2 - Palindrome Game (hard version)

Author: DenOMINATOR
Idea:DenOMINATOR

Editorial
Solution(Greedy) (DenOMINATOR)
Solution(DP) (DenOMINATOR)

1527C - Sequence Pair Weight

Author: sharabhagrawal25
Idea: rivalq

Editorial
Solution (sharabhagrawal25)
Solution (mallick630)

1527D - MEX Tree

Author: mallick630
Idea: CoderAnshu

Editorial
Solution (shikhar7s)
Solution (the_nightmare)

1527E - Partition Game

Author: rivalq
Idea: rivalq

Editorial
Solution (rivalq)
Solution (the_nightmare)

• +166

 » 12 months ago, # |   +118 contest was the_nightmare
•  » » 12 months ago, # ^ |   +5 haha, nice one XD
 » 12 months ago, # |   -47 nightmare round
 » 12 months ago, # |   +23 How to solve E using divide and conquer DP? (and especially how to maintain the cost around?)
•  » » 12 months ago, # ^ | ← Rev. 2 →   +13 link here is my code for divide and conquer techniquei took the idea from the code given below and understood it linkbasically what we are doing here is we are maintaining a persistent segment tree on every ith index which will provide us with the information that if we consider a segment of [i,j] then what will be its cost. The basic idea here is to use segment tree with range updates and point query.You could see from my code how to update ranges its pretty straightforward.now that we can find out the cost of any segment in log(n)complexity all we have to do is calculate the dp which can be calculated with the help of divide and conquer the only hard part of this method was the persistent segment tree part which was difficult to understand and actually think by yourself(atleast for me it was very new idea)
 » 12 months ago, # |   0 In problem B1, when all the elements of the string is 1, then how Bob wins?
•  » » 12 months ago, # ^ |   +6 It is given in the input section that string $s$ contains at least one $0$
•  » » » 12 months ago, # ^ |   0 Thanks bro!
•  » » » 12 months ago, # ^ |   0 But for this, why Draw is not the correct answer?
•  » » » » 12 months ago, # ^ |   +1 Yes, technically it should be DRAW but to avoid confusion we omitted that case
•  » » » » » 12 months ago, # ^ |   0 Now I am clear. Thanks
•  » » » » » 10 months ago, # ^ | ← Rev. 2 →   0 i have implemented dp for B2, but it's giving me incorrect output, pls help me find the bug const int N = 1e3; ll dp[N/2 + 1][N/2 + 1][2][2]; void solve() { int n; cin >> n; string s; cin >> s; int cnt00 {}, cnt01 {}, mid {}, rev {}; for(int i = 0; i < n - 1 - i; i++) { if (s[i] == s[n - 1 - i] && s[i] == '0') { cnt00++; } if (s[i] != s[n - 1 - i]) { cnt01++; } } if (n % 2 && s[n/2] == '0') { mid = 1; } if (dp[cnt01][cnt00][mid][rev] < 0) { cout << "ALICE" << '\n'; } else if (dp[cnt01][cnt00][mid][rev] > 0) { cout << "BOB" << '\n'; } else { cout << "DRAW" << '\n'; } } int main() { fastio(); for(int i = 0; i <= N/2; i++) { for(int j = 0; j <= N/2; j++) { for(int mid = 0; mid < 2; mid++) { for(int rev = 0; rev < 2; rev++) { dp[i][j][mid][rev] = INF; } } } } dp[0][0][0][0] = 0; for(int i = 0; i <= N/2; i++) { for(int j = 0; j <= N/2; j++) { for(int mid = 0; mid < 2; mid++) { for(int rev = 0; rev < 2; rev++) { // i -> cnt of symmetric 01 pairs // j -> cnt of symmetric 00 pairs if (i > 0) { dp[i][j][mid][rev] = min(dp[i][j][mid][rev], 1-dp[i-1][j][mid][0]); } if (j > 0) { dp[i][j][mid][rev] = min(dp[i][j][mid][rev], 1-dp[i+1][j-1][mid][0]); } if (mid > 0) { dp[i][j][mid][rev] = min(dp[i][j][mid][rev], 1-dp[i][j][0][0]); } if (rev == 0 && i > 0) { dp[i][j][mid][rev] = min(dp[i][j][mid][rev], -dp[i][j][mid][1]); } } } } } int tc = 1; cin >> tc; while(tc--) { solve(); } } 
•  » » » 12 months ago, # ^ |   0 I managed to solve only 1 problem, but i love all the problem upon upsolving. Indian contest are on another level.
•  » » » » 12 months ago, # ^ |   0 Bro will u please explain the first problem I am a newbie:( and not able to understand it
•  » » » 8 months ago, # ^ |   0 if string is 1011 then your solution fails.
 » 12 months ago, # |   +4 orz
 » 12 months ago, # |   +6 Does someone know of any problem similar to C?
 » 12 months ago, # | ← Rev. 2 →   0 what is the time complexity in B2 dp approach. 1.is it O(n^2) for one test case as it depends on no of 00 pairs and no of 01 pairs? 2.also if n^2 per test case how it passes the judge in 1 sec as n^2*t=1e9 ?
•  » » 12 months ago, # ^ |   +4 We precompute the dp and use it to answer all test cases
•  » » » 12 months ago, # ^ |   0 understood thank you.
•  » » 12 months ago, # ^ |   +3 Dp is pre-computed not run for each test case
•  » » » 12 months ago, # ^ |   0 thank you.got it
•  » » 12 months ago, # ^ |   0 but i think it is simple implementation problem with O(n) time complexity
•  » » » 12 months ago, # ^ |   0 ya,but i wanted to try dp approach and got doubt that as strings are different we may need to compute eveytime. but understood that structure of string wont matter as we only need 00 and 01 pairs.therefore we can precompute for 501,501 and answer queries directly after getting 00 and 01 pairs from given string.
 » 12 months ago, # |   -13 Alternative solution to A: SpoilerKeep doing $n=n$ & $(n-1)$ while $n$ & $(n-1)>0$. Print $n-1$.
•  » » 12 months ago, # ^ |   0 Интересно! Вы очень смелы, чтобы сделать что-то столь суровое.
•  » » 12 months ago, # ^ |   0 I think this will TLE in python
•  » » » 12 months ago, # ^ |   +2 It does not. PyPy 3 submission: 116877296
•  » » 12 months ago, # ^ |   0 Yes, you can do that too, but it would take too much time and result in TLE
•  » » » 12 months ago, # ^ | ← Rev. 2 →   +2 The complexity is logn for this so it won't TLE. At each iteration, you're setting at least 1 set bit to 0.
•  » » 12 months ago, # ^ |   0 Or you could just take the log base 2, and return 2^(this value)-1. O(1) approach.
•  » » 12 months ago, # ^ | ← Rev. 2 →   0 Hey! I am not able to understand why I am getting TLE for this solution :( Solutionll ans=n; while(n--){ ans=ans&n; if(ans==0){cout<
•  » » » 12 months ago, # ^ |   0 as limit of n was 10^9 no of operation for this limit of n is around 1/2*(10^9); which is will take higher time than Time limit given.
 » 12 months ago, # |   0 In A, 2^msb can also be calculated using logint msb = (int)(Math.log(n)/Math.log(2));
 » 12 months ago, # | ← Rev. 3 →   0 MY ISSUE PLEASE HELP ( PROBLM B1) !!ok when the number of zeros are even for example example 0 0 0 0 0 0 A pay1 1 0 0 0 0 0 B reverse 0 0 0 0 0 1 A pay1 1 0 0 0 0 1 B pay1 1 1 0 0 0 1 A reverse 1 0 0 0 1 1 B pay1 1 1 0 0 0 1 A reverse 1 0 0 0 1 1 B pay1 1 1 0 0 1 1 A pay1 1 1 1 0 1 1 B reverse 1 1 0 1 1 1 A pays1 1 1 1 1 1 1 isn't this optimal here every 4 changes means DRAW and extra 1,2,3 means BOB winsAnd when zeros are odd: this part of editorial its not clear to me :( Alice will change s[n/2] from '0' to '1' and play with the same strategy as Bob did in the above case. This way Bob will spend 1 dollar more than Alice resulting in Alice's win. example 0 1 0 1 0 1 0 1 0 A pay1 0 1 0 1 1 1 0 1 0 B pay1 0 1 1 1 1 1 0 1 0 A reverse 0 1 0 1 1 1 1 1 0 B pay1 0 1 1 1 1 1 1 1 0 A pay1 1 1 1 1 1 1 1 1 0 B reverse 1 1 1 1 1 1 1 1 1 in this case every 2 means DRAW and its repeating pattern of pay1 as AB BA AB BA then if cnt_of_0 is odd and cnt_of_0/2 is odd we will have 1 zero extra which will be paid by B means ALICE wins.but if cnt_of_0/2 is even we will have 1 zero extra which will be paid by A means BOB wins.PLEASEEEEE HELPPPPPPP!!UPD: Understood!!How to solve this. Thanks everyone
•  » » 12 months ago, # ^ | ← Rev. 2 →   0 If no of 0s is 4 (even)0000Alice spends 0100Bob spends 0110Alice spends 1110Bob reverses 0111Alice spends 1111Bob winsIf no of 0s is 5 (odd)00000Alice spends 00100Bob spends 00101Alice spends 10101Bob spends 11101Alice reverses 10111Bob spends 11111Alice winsHope it helps !
•  » » » 12 months ago, # ^ | ← Rev. 2 →   0 0000 0001 Alice +11000 Bob +01001 Alice +11101 Bob +11011 Alice +01111 Bob +1 DRAW!
•  » » » » 12 months ago, # ^ |   0 yes, this is a possibility, but the question states that both of them make the best move possible, if bob flips then it is a draw, if he doesn't then he winsAlice has no such option available to her
•  » » 12 months ago, # ^ |   0 Actually, for example $000000$ game goes-$A$ pay $1$ $100000$$B pay 1 100001$$A$ pay $1$ $100101$$B pay 1 101101$$A$ pay $1$ $111101$$B reverse 101111$$A$ pay $1$ $111111$$B$ winsNow, analyse $010101010$
•  » » 12 months ago, # ^ | ← Rev. 2 →   0 example 0 0 0 0 0 0A pay1 1 0 0 0 0 0 B pay1 1 0 0 0 0 1A pay1 1 1 0 0 0 1B pay1 1 1 0 0 1 1A pay1 1 1 1 0 1 1B reverse 1 1 0 1 1 1A pay1 1 1 1 1 1 1This is the optimal strategy discussed in the editorial. In each step except the last one, try to make the string palindrome again and in the last reverse the string. Yours is the same strategy I used during the contest but guess I needed to think more.
•  » » » 8 months ago, # ^ |   0 In the 2nd step y does "B" pay 1? He can just reverse the string and give it back to "A" right?
 » 12 months ago, # |   0 After spending about 20-25 minutes I understood the logic of problem C ( yes I'm still a noob), but I was wondering how does one come up with that logic during contests ( I know practice, practice practice) but I suck at dp and I've been trying to improve it, so if anyone has dp sheets that can build my foundation it'll be of great help thanks :), I've been doing classic dp problems like knapsack, longest common subsequence type questions and even started with matrix chain multiplication recently.
•  » » 12 months ago, # ^ |   0 The states are easier to come up during contests if you really try to, most probably you just take what the problem asks and derive subtasks as a prefix, eg: Kadane-ish (or multiple prefixes across multiple sequences, eg: LCS), suffix, subarray of the original sequence, eg: matrix chain multiplication. I'm sure after a lot of $practice$, things would become somewhat more intuitive and reflexive.
•  » » » 12 months ago, # ^ |   0 okay thanks
 » 12 months ago, # |   +29 Alternative solution to E:First steps are also coming up with the $dp$ and writing the brute-force transition formula. Then, by considering $last(a_{r + 1})$, we can prove the following property: $c(l - 1, r) + c(l, r + 1) \leq c(l - 1, r + 1) + c(l, r)$ Therefore, $c$ satisfies Quadrilateral Inequality, where a divide-and-conquer solution works in $O(nk\log n)$ time.Note that calculating $c$ needs a two pointers trick similar to 868F - Yet Another Minimization Problem.
•  » » 12 months ago, # ^ |   0 I am using divide and conquer dp optimization for problem E. can you help me why i am getting TLE code
•  » » » 12 months ago, # ^ |   +1 for(int k = mid; k >= optl; k--) { $k$ should be enumerated from $optr$ to $optl$, not $mid$ to $optl$. Otherwise, the parameter optr is unused.
•  » » 12 months ago, # ^ |   0 How to prove complexity of two pointers trick?
 » 12 months ago, # |   0 Anyone please, help me to understand..For problem B1 help me to figure out the answer for this test case 00100
•  » » 12 months ago, # ^ |   0 ALICE — 10100 BOB — 10101 ALICE- 10111 BOB — 11101 (REVERSE) ALICE — 11111ALICE --> 3 BOB -->1 BOB WINS
•  » » » 12 months ago, # ^ |   0 Why it is not possible?ALICE — 10100 BOB — 00101(REVERSE) ALICE- 10101 BOB — 11101 ALICE — 10111(REVERSE) BOB — 11111ALICE --> 2 BOB -->2 DRAW
•  » » » » 12 months ago, # ^ |   0 Exactly my confusion.
•  » » » » 12 months ago, # ^ |   +1 it's not that you can't do that . you can .But you know what , the word optimal is mentioned in the question, means if i got a chance to play then i tried my best to win , so if bob put a 1 in the string instead of reversing he will land in the winning position , instead of a draw. You can't just brute force and say bob or alice win or its a draw. its not mandiatory that if i have a chance to reverse the string then i have to reverse it , so that i will be relived from that 1 dollar penalty, you can't do that .
•  » » » » » 12 months ago, # ^ |   0 Yes finally understood, the greedy approach of reversing anytime possible is where I was thinking wrong. Thanks a lot for taking the time to explain.
•  » » » 12 months ago, # ^ |   0 But can this not be the case instead? Alice -> 10100 (A=1,B=0) Bob -> 00101 (A=1,B=0) (reverses) Alice -> 10101 (A=2,B=0) Bob -> 11101 (A=2,B=1) Alice -> 10111 (A=2,B=1) (reverses) Bob -> 11111 (A=2,B=2) SO DRAW.
•  » » » » 12 months ago, # ^ | ← Rev. 3 →   0 Yes, definitely this can be the case. But, here every player is trying to win right, so as BOB is second player, he will make moves such that he can win. Here this is surely one of the ways the game can proceed, but not optimal. Optimal can be- String: 00100 Alice: 01100 Bob: 01110 Alice: 11110 Bob: reverses -> 01111 Alice: 11111So, finally Bob spends 1 whereas Alice spends 3. It's not always optimal to greedily reverse the string whenever possible. Hope it helps. Sorry if I did something wrong.
•  » » » » » 12 months ago, # ^ |   0 Yes finally understood, the greedy approach of reversing anytime possible is where I was thinking wrong. Thanks a lot for taking the time to explain.
 » 12 months ago, # |   0 rivalq the_nightmare I am confused in the editorial for E. Aren't the k mentioned in the dp transitions and the k mentioned in the big oh notation different?
•  » » 12 months ago, # ^ |   +3 Yes they are different. The one in dp transitions you can regard as a temp variable.
 » 12 months ago, # | ← Rev. 2 →   0 Problem B2 — Palindrome Hard Please explain this case for string: 1000 A reverses -> 0001 (A=0 B=0) B pays -> 1001 (A=0 B=1) A pays -> 1101 (A=1 B=1) B reverses -> 1011 (A=1 B=1) A pays -> 1111 (A=2 B=1) So BOB should win. But by the above code, it's making Alice the winner. Please guide me where I am doing a mistake in the implementation.
•  » » 12 months ago, # ^ |   0 Alice can win this way:A pays -> 1001B pays -> 1101A reverses -> 1011B pays -> 1111B = 2 A = 1, so Alice wins. Bob has no other moves.
•  » » » 12 months ago, # ^ |   0 According to you Alice wins..But according to Jyotirmaya Bob wins...So what is the exact ans...Both of you correct.
•  » » » » 12 months ago, # ^ |   +2 Alice wants to win right? So she would do exactly what I stated. Bob has no other move than just to lose. It is not logical to make a move that will allow your oponent to win.
•  » » » » » 12 months ago, # ^ |   0 Yes finally understood, the greedy approach of reversing anytime possible is where I was thinking wrong. Thanks a lot for taking the time to explain.
•  » » » 12 months ago, # ^ |   0 By this explanation, I think we need to think of a solution with the least number of moves overall rather than a greedy approach of reversing whenever possible. Is my interpretation correct?
 » 12 months ago, # |   0 In Problem B1 in case of string s= 111001 Alice is winning even though no. of zeroes are even
•  » » 12 months ago, # ^ |   0 string is already palindrome lol got it nvm
 » 12 months ago, # | ← Rev. 2 →   0 In fact , some users of the Chinese online judge : Luogu said that the difficulty of these problems is not monotonically increasing and they suggested that you should have changed the order of problem B and C. the_nightmare
•  » » 12 months ago, # ^ | ← Rev. 2 →   +4 There is only one additional case to be dealt in B2 if you look at the editorial. That would explain why they considered B2 as easier probably.. and dp is not what most div 2 contestants used.
•  » » 12 months ago, # ^ |   +19 Basically, we have to put together B1 and B2 due to contest restrictions due to which we are not able to swap B2 and C. But we have provided the scoring according to difficulty B(750+1500) total 2250 and C only 1500.
•  » » » 12 months ago, # ^ |   +3 In problem D's editorial shouldn't it be "We will always break before or on reaching root" instead of "Note that we will always break at the root as it is marked visited in the initial step."
 » 12 months ago, # |   -13 The term "Contiguous Subarray" is much more quicker to grasp than "Subsegment".Hope future authors see this :) Nice Contest btw
•  » » 12 months ago, # ^ |   +4 but you know what you got something to learn.
•  » » 12 months ago, # ^ |   0 I mixed subsegment with subsquence, and it wasted me lots of time to solve this problem in a wrong way
 » 12 months ago, # |   0 Could someone please write a simpler edit for Problem-C, I have gone over it a lot of times but am still confused as to why the question creator went for:value[a[i]] += (i + 1);please help me out with the logic. I understood the task but couldn't implement it that well and now I'm even more confused.
•  » » 12 months ago, # ^ |   0 I think (i+1) refers to the total number of subarrays ending at i. Since we are using 0 indexing, so +1 for the adjustment.
•  » » 12 months ago, # ^ |   0 Consider and element i. Now if take an element j such that a[i]==a[j] and j < i then subarray a[j-i] will occur as part of all subarray's from  i = 0 to j i.e j + 1 times. So  value[a[i]] += i + 1
•  » » » 12 months ago, # ^ |   0 Oh now i get it, thank you so much
•  » » » » 12 months ago, # ^ |   0 Happy to help
•  » » » 4 months ago, # ^ |   0 I was confused over this part. Thanks now,I got it.
•  » » 12 months ago, # ^ |   0 Suppose there is an element at an index that comes before i, and value of both indexes is same, then how many times will value at index that came before will pair with value at index i ? The answer is the number of times that former element is present in subsegments that END at index i. Now think how many such subsegments are possible ? (hint- index of former element).
•  » » 12 months ago, # ^ |   0
 » 12 months ago, # |   0 Can someone help me with my solution :My idea: Maintain a path and its endpoints. Maintain a 'visited' array which denotes whether or not this node is in current path. Consider 0 as base case and mark it visited and initialize both the endpoints to 0. Iterate from 0 to i In order to find if there can be a path having all nodes [0, i] we just need to check if the endpoints of path having all of [0, i-1] can be extended to i, so move from ith node to its parent till we find a node that is in the path that includes the nodes [0, i-1], that is first visited node. If this node happens to be one of the endpoints then extend the path and update endpoints else there can be no such path that includes all of [0, i] nodes and we dont need to check this for following i's. I am unable to figure out why this gives TLE ! https://codeforces.com/contest/1527/submission/116924323
»
12 months ago, # |
-13

include<bits/stdc++.h>

using namespace std;

int main(){

int t;
cin>>t;

while(t--){
int n;
cin>>n;
string s;
cin>>s;
int count=0;
if(n==1&&s[0]=='0'){
cout<<"BOB"<<'\n';
continue;
}
for(int i=0;i<s.length();i++){
s[i]=='0'?count+=1:count=count;
}
if(count%2==0){
cout<<"BOB"<<'\n';
}
else{
cout<<"ALICE"<<'\n';
}

}

}

•  » » 12 months ago, # ^ |   0 This is my logic, you can use to improve your code 116814530
 » 12 months ago, # |   0 please give some another approach for problem D
 » 12 months ago, # |   +9 Nice explanation of problem B2
 » 12 months ago, # | ← Rev. 2 →   +4 Can someone give a small test for those codes which fail test case 5 by printing 1 instead of 0 at 1923rd position for problem D? Submission
•  » » 12 months ago, # ^ | ← Rev. 2 →   0 I got that error by incorrectly calculating in the tree "0 -- 2 -- 1" the number of pairs with mex == 2 (there are 0).
 » 12 months ago, # |   0 In problem A I am getting wrong output format Expected integer, but "2.68435e+008" found Solutionlong long n;cin>>n; long long ans=log2(n); cout<
•  » » 12 months ago, # ^ |   0 Probably, pow() doesn't work well with integers- (I was stuck here too) read this.
•  » » 12 months ago, # ^ |   0 pow() returns a double, while the expected output is an integer, hence the WA. Also as anubh4v stated, pow() (and log2() too) can be imprecise at times, leading to incorrect rounding of the number.
•  » » » 12 months ago, # ^ |   0 I will keep that in mind. Thanks
• »
»
12 months ago, # ^ |
0

a^b=e^(b*log(a)). This type of floating point calculation is error prone, and is recommended never to use. Built your own code of logarithmic time multiplication or directly copy from internet like gfg. Some C++'s function on which you can rely (at least I had experience no error ) is: - sqrt - log10,log2,log10

 » 12 months ago, # |   0 How to solve c using DP. Suppopse that TL is 3 sec. I am not getting how to solve it using DP. can it be solved using the same idea as for count palindromic substring.
 » 12 months ago, # |   0 For problem B1, why should Bob spend $1 to restrict Alice from doing operation 2 ?? He himself can simply perform operation 2 whenever possible and that will force Alice to do operation 1 on next step. By this, DRAW will happen only when no. of 0s is divisible by 4. In all other cases, BOB will win. Can someone explain what's wrong in this concept?? •  » » 12 months ago, # ^ | 0 i thought exactly same as you. but we should concern about when the zero is at center.(it meanse the count of zeros is odd)  » 12 months ago, # | 0 Can someone explain me how does the dynamic programming solution for B2 works?From my understanding of the problem when we consider alice we add positively, when we consider bob we add negatively. But how does that happen in code? How does the code distinguish bob from alice? And how does it simulate turns?In other words: can someone explain me how the simulation of the game occurs during the bottom up transitions of the editorial / given code?Thanks in advance. •  » » 12 months ago, # ^ | 0 Because dp[i][j][k][l] is the optimal answer for a state where i is the number of 00, j is the number of 01 or 10, and k = 1 denotes if the middle position in case of odd length string is 0 and l = 1 denotes that in the last turn other person reversed the sting thus we can not reverse. For all the states, we will assume that the current turn is of Alice and to compute the answer for that state, we will add negative of the transition states, which will denote Bob's optimal score. •  » » » 12 months ago, # ^ | 0 So each move is assumed to be alice, to simulate bob we just make it negative. On the other hand when it's bob, the negative of a negative is positive, so it's alice. This also mean that the concept of turn doesn't really exist in this case, because in dynamic programming we only care about the previous state's value, but not the actual move of a simulation.I think I kinda understood, thank you!  » 12 months ago, # | 0 In problem C "This is just the sum of prefix sums of indexes of the element having the same value as that of a[i] which has occurred before index i" What does prefix sums of indexes mean here??? •  » » 12 months ago, # ^ | ← Rev. 8 → 0 We are standing at position i, assuming there is a value at position j ( j < i ) that is equal to the value at i, so in all subsegments which end at i, the pair (j, i) will be counted how many times ? It will be the number of subsegments containing both i and j which end at i. So how many subsegments are there? The answer is j, yes, it is j. It will include subsegments [j, i] ; [j - 1, i] ; [j - 2 , i] ..... [1, i] . In the explanation, the author is j + 1 because they start at index 0 •  » » » 12 months ago, # ^ | 0 So calculated the sums of indexes of the element having the same value as that of a[i] is to calculated the numbers of new pairs when we add i •  » » » » 12 months ago, # ^ | 0 Thnx a lot man!!!! This helped loads!!! •  » » » » 12 months ago, # ^ | 0 Thanks man!!  » 12 months ago, # | 0 I cannot understand Problem D at all! I went through the code of people who solved it, and could understand broadly what they do, but couldn't get the intuition still. Can someone please help !  » 12 months ago, # | 0 In the question B2,why can we print "DRAW" when we find that cnt_0==0&&cnt_1==1 instead of cnt_0==0&&cnt_1%2==1 ? And why both of them are right?（Please forgive my poor English QAQ）  » 12 months ago, # | 0 can anyone pls tell why i am getting time limit exceeded on test case 7 in problem D MEX TREE i am just doing a dfs traversal once to calculate subtree sizes and then iterating from 1 to n and marking not visited nodes as visited in my current path and calculating answer for each mex value.my submission link https://codeforces.com/contest/1527/submission/116996030  » 12 months ago, # | 0 In Problem D: as mentioned in the editorial that we need to "update the subtree sizes as we move up to parent recursively", we don't need to do this. When (l!=r) we will always choose the other parent. Only when we are calculating MEX1 (the previous l was equal to r) so we have to update the size of 0 subtree only once. •  » » 12 months ago, # ^ | 0 Nice Solution Man! •  » » 12 months ago, # ^ | 0 Yoir solution simplified the question mqnyfolds. Thanks anadii! •  » » 12 months ago, # ^ | 0 Thanx for ur imsights anadiii!!  » 12 months ago, # | 0 Good one  » 12 months ago, # | -8 Extremely wierd contest ! Please don't do these more ! Bring some good ones ! Problems were not intresting to solve !  » 12 months ago, # | +8 I do not know if this approach has been covered for E using divide and conquer dp. To get cost of current interval, maintain global 2 pointers on the array, sum variable and array of deque. Fit the pointer for each query. Amortized complexity over per level of dp should be N*log(N). So with K layers it becomes K*N*log(N).  » 12 months ago, # | 0 In problem B1, when all the elements of the string is 0, then how Bob wins? •  » » 12 months ago, # ^ | 0 eg 0000 •  » » » 12 months ago, # ^ | ← Rev. 3 → 0 when all the elements of the string are 0, bob will not always win. for ex, 00000in the case of 0000 though: alice has to use the first operation since it's a palindrome [0001] bob can use the first operation [1001] alice has to use the first operation since it's a palindrome [1101] bob can use the second operation since it's not a palindrome [1011] alice has to use the first operation since bob used the second operation for the previous turn [1111] in total, alice used$3, bob used \$1
•  » » » » 12 months ago, # ^ |   0 thank you so much !
 » 12 months ago, # |   0 for problem B2 (hard version), the game will only ever end in a draw if the middle number of an odd non-palindromic sequence is a 0 and there is exactly one other 0.this is because alice will have to fill in the "other 0" for the first turn to keep bob from taking control of the second operation, but the next turn will end the game when bob fills in the center 0 (due to the sequence becoming palindromic), resulting in a draw.example: 001every other sequence that does not start as a palindrome will result in alice's winmy solution
 » 12 months ago, # |   0 for problem A https://codeforces.com/contest/1527/submission/116832364 in this 2nd test case calculate int r=log(8)/log(2) as 2 but i have tried it everywhere it is 3.
 » 12 months ago, # | ← Rev. 4 →   0 im idiot.. i finally understood what i miss.
 » 12 months ago, # |   0 in problem C consider an example 3,1,4,1,5,1,6(1 based indexing)....consider index 2 and 4 both are "1"..so elements on right side of index 4 and on the left side of index 2..... is (7-4+1)*(2) = 8... my confusion is among these 8 segments one possible segment is 3,1,4,1,5,1....but here we counted only for index 2 and 4 which is only for two "1" how it is taking the 3rd 1 which is at index 6.....please someone help...very poooor at this :(
 » 12 months ago, # | ← Rev. 2 →   0 If we are up for some golf coding my solution for A used property that x and x-1 differ in a single bit so the last bit remaining will be MSB int n; cin >> n; while(n&(n-1)){ n &= n-1; } cout << n-1; 
 » 12 months ago, # |   0 Can somebody explain B2(dp version)? I am not getting how transition is done.
 » 11 months ago, # |   0 the_nightmare's solution for D will TLE for the following case. https://drive.google.com/file/d/1K-1sb5ls2PP0lKiGQf5dy9BVuqMBvReG/view?usp=sharing
 » 11 months ago, # | ← Rev. 3 →   0 Can some one tell me why this approach won't work for Problem A? n&n-1 will set the last "1" bit to 0. So the number of times I do that will set all bits to 0. t = int(input()) for i in range(0, t): n = int(input()) my = list(bin(n))[2:] print(bin(n), my) e = my.count('1') print(n-e) 
 » 10 months ago, # |   0 Does anyone know why test case 2 for question A is timing out? 122251733
•  » » 10 months ago, # ^ |   0 Because time complexity of your solution is O(n). To solve the problem you need to get O(1)
 » 10 months ago, # |   0 I'm confused on A. In the notes it says "In the second testcase, the maximum value for which the continuous & operation gives 0 value, is 3. No value greater then 3, say for example 4, will give the & sum 0." If we plug in 4, it still gives 0 and also 5 gives 0, I don't understand why it stops at 3? To my understanding 5&4&3=0 0%2%1 = 0 since it is zero then any number we plug in will automatically be 0 as well.
 » 10 months ago, # |   0 Problem 1527C - Sequence Pair Weight could have been done greedily (and imo it's easier). Let $d(x, y)$ denote the number of segments which contain elements at indices $x$ and $y$ (indices start from 0 so $x,y \in {0, 1, 2, \dots, n-1}$). It is easy to see that if $y > x$ then $d(x, y) = (x+1)*(n-y)$. This allows obvious $O(n^2)$ solution, but it can be done faster in $O(n)$. Let's say we have a vector $v$ and we are at it's $i$-th element. Then, we can calculate the answer as: $d(v_0, v_i) + d(v_1, v_i) + \dots + d(v_{i-1}, v_i)$which is just $(v_0+1)*(n-v_i) + (v_1+1)*(n-v_i) + \dots + (v_{i-1}+1)*(n-v_i)$and this can be simplified to: $(v_1 + v_2 + v_3 + \dots + v_{i-1} + i-1) * (n-v_i)$Which you can easily calculate while iterating through the vector. Code: 124839948
 » 9 months ago, # |   0 I did not understand why we need to count the prefix indexes in sequence pair weight problem. can someone explain?
 » 9 months ago, # |   0 I think more short solution for A: https://codeforces.com/contest/1527/submission/127546267
 » 8 months ago, # |   0 @DenOMINATOR B1 will fail if our string is 1011. In this case alice should win but according to you Bob is winning.
 » 6 months ago, # |   0 I have a simpler solution for A: void sol(){ int n; cin >> n ; int msb = log2(n); int ans = (1 << msb)-1; cout << ans << endl; } `
 » 2 months ago, # |   0 in b1 can anyone explain output for this string 01011010 i am getting draw