### BledDest's blog

By BledDest, 5 months ago,

1430A - Number of Apartments

Idea: fcspartakm

Tutorial
Solution (fcspartakm)

1430B - Barrels

Idea: fcspartakm

Tutorial
Solution (fcspartakm)

1430C - Numbers on Whiteboard

Idea: BledDest

Tutorial
Solution (fcspartakm)

1430D - String Deletion

Idea: BledDest

Tutorial
Solution (BledDest)

1430E - String Reversal

Idea: fcspartakm

Tutorial
Solution (fcspartakm)

1430F - Realistic Gameplay

Idea: BledDest

Tutorial

1430G - Yet Another DAG Problem

Idea: BledDest

Tutorial
Solution (BledDest)

• +119

 » 5 months ago, # |   +1 Editorial in Russian will be posted soon. Please be patient!
•  » » 4 months ago, # ^ |   0 Great round. Thanks =)
 » 5 months ago, # |   0 My solution of A got hacked! Lesson learnt.
•  » » 5 months ago, # ^ |   0 I'm saying it every contest)
 » 5 months ago, # |   0 A,B,C were easy to do but couldn't solve out D in time :(
•  » » 5 months ago, # ^ |   0 same pinch bruh !!!
•  » » » 5 months ago, # ^ |   -51 Does anyone give a f?
•  » » » » 5 months ago, # ^ |   +57 did I ask for anyone to give f?
 » 5 months ago, # | ← Rev. 3 →   -10 .
 » 5 months ago, # |   +3 Can A be solved using a linear diophantine equation in 3 variables?
•  » » 5 months ago, # ^ | ← Rev. 2 →   +2 Yes. Nugget number tells us that all number more than or equal to $3*5-3-5 = 8$ could be written in the form $3a+5b$ where $a,b \geq 0.$ So, now you get the general formula1,2,4 -> no answer7 -> 0,0,1The rest could be either brute force or notice that the smallest $a$ is either $0,1,2,3$ or $4.$ So, you could solve this in $O(1)$
•  » » » 5 months ago, # ^ |   0 Thanks for helping :)
•  » » » » 5 months ago, # ^ |   0 An easier O(1) solution to A.. void solve() { int n; cin>>n; if(n==1 || n==2 || n==4) { cout<<-1<
•  » » » » » 5 months ago, # ^ |   0 Thanks it was helpful.
•  » » 5 months ago, # ^ |   0 I converted it to diophantine by doing this: 3x+5y=n-7z for z = 0 to n/7 Then using the extended euclids algorithm I found x0 and y0 st 3x0+5y0=gcd(3, 5)=1 and multiplied x0 and y0 by n-7z. Then I tried to find t such that x0+5t, y0-3t are positive. We can use some inequalities to determine range of t and loop over these t and check if the current is correct if yes return. Actually all values in the range are correct but i looped over some modification of the range because I could not think whether to +1 at each iteration or -1(i am new to CP and CS in general) so i checked the cases. My algorithm was correct(you can check my profile for code) but it was very messy, hard to implement for someone not good in math, a lot of code so I don't suggest this.
•  » » » 5 months ago, # ^ |   0 Thanks, I had trouble implementing it too will check out your solution
 » 5 months ago, # |   0 Can F be solved using BinarySearch? I tried but because of poor implementation and slow speed, got wrong answer on test case 6? Did anyone else try using binary search?
•  » » 5 months ago, # ^ |   0 If I am not Wrong this is your Fake Account to hack your own solutions and try some alternate solutions if passed you can just make some changes submit with your original ID so that your rating will not be disturbed.
•  » » » 5 months ago, # ^ | ← Rev. 2 →   0 Lol! Bro you can search this username. It's everywhere. Facebook, Instagram, LinkedIn, Codeforces, Codechef, AtCoder, XDA-Dev, Hackerrank, HackerEarth, LeetCode, and any possible site where one can register and choose a username irrespective of whether it's a coding platform or not. Cant think of any reason why you thought so, but that's not the case. Peace :)
•  » » » » 5 months ago, # ^ |   0 Sorry, Yes You are right. I earlier thought it to be suspicious. Sorry Bro Again, No such intention. !!I got to know I was about to delete my comment but it was already more than 2 min..Apologies from my Side. !!!
•  » » » » 5 months ago, # ^ |   0 moli2398 I see your solution for problem B got hacked. Is it because of Arrays.sort() being O(N^2) ? Just curious.
•  » » » » » 5 months ago, # ^ |   0 Yeah I guess!
 » 5 months ago, # |   +15 Can someone please tell their solution for G using Flows?
•  » » 5 months ago, # ^ |   +13 I solved G with Simplex. I don't have any proof that the answer will always be an integer though. I guess I just got lucky that it was in this case.
•  » » » 5 months ago, # ^ |   +11 Same here.I think it can be converted into an equivalent mincost maxflow
•  » » » » 5 months ago, # ^ |   0 Yes it can.The linear dual of this problem is actually maximizing the sum of flows along all edges — not just the source. And from each node with negative $c_v$ you need to send $-c_v$ units of flow more than goes into this node and each node with positive $c_v$ needs to receive $c_v$ flow more than it sends.
•  » » » 5 months ago, # ^ |   +20 The incidence matrix of a non-empty directed acyclic graph is totally unimodular matrix. A linear problem whose matrix is totally unimodular matrix always has integer optimal solution. In this problem, the matrix is the incidence matrix of DAG.
 » 5 months ago, # | ← Rev. 3 →   0 I see some people solving E without merge sort tree or fenwick tree . Can someone explain their approach . I know Merge sort tree is the most standard approach for counting inversions but in this case string will have max 26 distinct characters . Can this constraint be used as an advantage to solve in more simpler way ?
•  » » 5 months ago, # ^ | ← Rev. 3 →   +3 These are the 2 approaches I know for calculating inversions in an array other than merge sort and Fenwick tree(If it's relevant to the current discussion). DIVIDE_AND_CONQUER_SOLUTION #include using namespace std ; #define int long long int ans =0,b[61] ; void dp(vector&a,int i1){ int c=0,n=a.size(),ans1=0; if(n<2||i1<0) return ; for(int i=0;i>i1)&1) ; if(!((a[i]>>i1)&1LL)) ans1+=c ; } b[i1]+=ans1 ;vectora1,a2 ; for(int x:a){ if((x>>i1)&1LL) a1.push_back(x); else a2.push_back(x); } dp(a1,i1-1);dp(a2,i1-1) ; } void solve(){ int n ;ans=0 ; cin >> n ; vectora(n); for(int &x:a) cin >> x ; dp(a,61) ; for(int i=0;i<61;i++) ans+=b[i] ; memset(b,0,sizeof(b)) ; cout << ans <<'\n' ; } signed main(){ int t ;cin >> t ; while(t--) solve() ; }  PBDS_SOLUTION#include #include #include using namespace __gnu_pbds; using namespace std ; #define ar array template using oset =tree, rb_tree_tag,tree_order_statistics_node_update> ; void solve(){ int n ,ans=0 ; cin >> n ; vectora(n); for(int &x:a) cin >> x ; oset>s ; for(int i=0;i> t ; while(t--) solve() ; } 
•  » » » 5 months ago, # ^ |   0 You can also do coordinate compression + segment tree / BIT. Note that if it's a permutation (or something you can transform to a permutation easily) you do not need coordinate compression.
•  » » » 5 months ago, # ^ |   0 Can you explain the divide and conquer solution?? Thanks
 » 5 months ago, # |   +11 I have an alternative solution to G:First assign to each node the number given by the following greedy algorithm: Process all nodes in order of topological sort starting from the leaves and assign 0 to a leaf and maximum of values of all the children plus 1 if it's not a leaf.But this greedy will be not optimal in cases like this: There are four nodes, and there is an edge from node 1 to node 2, another from node 2 to node 3, and another to node 1 to node 4. Here the optimal solution is 2 1 0 1 while the greedy gives 2 1 0 0. But this can be improved with the following greedy brute force-like algorithm:Select some subset of nodes and try to sum 1 to all nodes in this subset, check if it not ruins anything and if the profit(change to the total answer if we sum 1 to all nodes in this subset) is positive, then if the profit is positive and we can apply the operation we will apply them.We can do this step to all 2^n subsets, and when it's finished repeat them n — 3 times(because it may be optimal add more than 1 to some nodes). The overall complexity is 2^n*n^3 but in practice is much lower, i have passed the time limits without many optimizations.However, i have no idea of why it works, so, if anyone can prove it or hack me, i would be really grateful.
•  » » 5 months ago, # ^ |   +8 I had the same idea. It's correct because you're effectively emulating the simplex algorithm for linear programming.
•  » » » 5 months ago, # ^ |   +9 Wow, I didn't knew about simplex algorithm, i will find it in google, thanks.
 » 5 months ago, # |   0 Can someone point whats wrong in my submission for D? https://codeforces.com/contest/1430/submission/95385972
•  » » 5 months ago, # ^ |   +4 your solution will fail in these kind cases: 01110 expected output: 3 your output: 2
 » 5 months ago, # |   0 Here is another BIT solution for problem E, which I found more intuitive as a speedup on the naive O($n^2$) solution.Explanation:In the naive solution, you would like to iterate in the reverse direction on the reversed string $t$, at each position check which character should be there, and then try to move the closest character to its position. That would take $i-j$ swaps to move the character form position $i$ to $j$. But when we do this swap, the positions of the elements after it get messy so if we did it in a dumb way, we would need O(n) to maintain the new positions.To get around this, we can use a BIT. Store 1 in all positions [0,n-1]. Now, when we query on any position, we would get its one based index in the string. The next observation is that we are always fixing the suffix of the string, so when we swap, we are effectively deleting it from out original string. To simulate this, we can make its value in the BIT 0, and now we can once again find every character's position in O(logn). This way, we could speed it up to O(nlogn)
 » 5 months ago, # |   0 Can someone explain why in the editorial code for D we have deleted++ at the end of the while loop?
•  » » 5 months ago, # ^ |   0 Same doubt.
•  » » 5 months ago, # ^ |   0 Since for let's say any consecutive block of length in particular 3 that is it maybe like 1110.. then the cnt will remain same for only 2 times starting from the beginning of the block but we are deleting the whole block so the score gets increased only by one while the deleted has to go 3 so first time before the loop while pop from the queue and second time while matching the 2nd and the 3rd character and finally to eliminate the 3rd character we make deleted++ , i hope it helps!
 » 5 months ago, # |   0 my solution to D herewhich uses a simple visited array to keep track of deleted numbers by the first operation
•  » » 5 months ago, # ^ |   0 can you explain it, i didn't understood D
•  » » » 5 months ago, # ^ | ← Rev. 2 →   0 You see the second operation always strips away the first continuous segment of the string, so to maximize operations we need to delete using first operations in a way in which the number of continuous segments doesn't decrease a lotFor example in 1011 if you remove 0 you'll be left with 111 which is one segment but if you remove the last one you'll get 101 which has three continuous segments So what we wanna do here is in first operation if possible choose a position which has the same value at its adjacent, because deleting it won't decrease the number of segments such as in 1101 deleting first one will result in 101 which still has 3 segments So in my code I use a visited array to keep track of what positions I deleted with my first operation I try to find what is the first position which is the same as the number after it, and I visit it, after that I use the j variable and traverse it until the end of the first continuous segment, now in next iteration i need to choose another position so it needs to start from i+1 but also after j so i updated it accordinglyIn the end i might run out but we might still have j left this will happen in cases like 1010101 In these cases we can simple cut one by one
•  » » » » 5 months ago, # ^ |   +1 Thank you so much sir, now i got it
 » 5 months ago, # | ← Rev. 4 →   0 The second paragraph of editorial of problem D it's written, "It's never optimal to delete a character that's different from both adjacent characters". I think it will be " always " instead of " never ".
•  » » 5 months ago, # ^ |   0 not really, you wanna delete characters which are same to the adjacent guys so you don't end up reducing the number of operations for example in 1011 if you delete 0 it will become 111 and will be reduced in just one operation
•  » » » 5 months ago, # ^ |   0 oh. I got it now. Thank you very much
 » 5 months ago, # |   +8 I request all the people contributing to the editorial to not use their templates while giving solutions. It would be very helpful to see whole for loop written instead of just seeing inc(l,r) because we have to search through your templates where that is, and then we have to put that code along with your other code and then see it in continuation with that code. It would be easier for better coders to see through it, but for noobs like me, it is just confusing.
 » 5 months ago, # |   0 Can anybody provide me with proof as to why choosing the two biggest numbers in problem C always leads to the smallest number remaining at last? It was intuitive but why does that greedy actually works? Thanks in advance!
•  » » 5 months ago, # ^ |   +3 If you look at the sum of the entire array, you'll see that after n-1 operations this sum is the number itself, which we have to minimize At each operation we are removing (a + b) /2From the sum, so to minimize the sum you'll choose the largest 2 numbers
 » 5 months ago, # |   0 For problem E (String Reversal), can anyone tell me how the answer is 30 for test case 3?
 » 5 months ago, # |   0 can someone please explain to me the solution to problem D. I understood the concept behind the problem, but I am having trouble understanding the code. Thanks.
 » 5 months ago, # |   0 I think I have a better solution for F in implement,if the n is 5000 , we can use HashMap or other "almost" O(1) datastruct to replace map.Let's get down to our business,we just naive denote dp[i][j] is we finish i task and we left j bullets , we cost the minmum bullets , and normal transtation which should consider if we finish the i in advance or R[i]!=L[i+1] we can refresh the dp[i+1][k] the Code: https://codeforces.com/contest/1430/submission/95434956
 » 5 months ago, # |   -12 Does anyone have flow solution for problem 'G'?
•  » » 5 months ago, # ^ | ← Rev. 2 →   0
 » 5 months ago, # |   0 D was a very good greedy, thanks, but why shouldn't we delete the first character if no 2 adjacent characters are equal ?
•  » » 5 months ago, # ^ |   0 You can delete the first Character if no 2 adjacent are equal 95567422
 » 5 months ago, # |   0 Extremely simple O(1) soln for A 95195199
 » 5 months ago, # |   +3 It may sound stupid but anyone can explain or proof why problem E "so the first character a in the original string is the first character a in the resulting string, the second character a in the original string is the second character a in the resulting string, and so on."will give the optimal solution?
 » 5 months ago, # | ← Rev. 2 →   0 Easy implementetion of C: 95793125
 » 5 months ago, # |   0 I was trying to debug my Solution for E for the past hour. I realized that I declared the vector as char, instead of int -_- (95816212)I love how bugs in Competitive Programming makes you want to kill yourself!
 » 5 months ago, # |   0 Stop writing "it's easy to see that" in tutorials. Makes me feel dumb.
•  » » 4 months ago, # ^ | ← Rev. 2 →   +1 Yep, apparently if you are good at something, you have to tell others that it is easy, in order to increase your ego.
 » 5 months ago, # |   0 in problem E i cant visualize how counting inversions is the final ans,can anyone help me visualize. Thanks in advance;)
 » 5 months ago, # |   0 I am not sure if this question has been asked, I am not finding it. I wanted to ask, "Why the rating of the questions has not been updated yet ?"
 » 5 months ago, # |   0 Hello. Can anyone please explain what is wrong in this code? It is for problem D. Any help would be highly appreciated. Thank You. Problem: 1430D - String Deletion Code#include using namespace std; #define MOD 1000000007 #define int long long #define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl "\n" int32_t main() { fast; int t = 1; cin >> t; while(t--) { int n; cin >> n; string s; cin >> s; deque dq; int ct = 1; char curr = s[0]; for(int i=1;i 0) dq.push_back(b); if(!dq.empty()) dq.pop_front(); } else dq.pop_front(); ans++; } cout << ans << endl; } } 
•  » » 4 months ago, # ^ |   0 Give the input as 10001 and your code would give the output as 2. The current answer should be 3.10001->1001->001 001->01->1 1->empty
 » 4 months ago, # | ← Rev. 2 →   0 please help me with D problem, i tried a lot of tests but still don't know where i was wrong :(, thanks for your care , link submit: https://codeforces.com/contest/1430/submission/96215681
 » 4 months ago, # |   0 can someone help me with D
 » 4 months ago, # |   0 Can someone explain E?
•  » » 4 months ago, # ^ | ← Rev. 2 →   0 I don't know if you already solved, but i'll try to explain in a different way from the editorial.Let's call t the inverted string s, and let's define an array p of n positions where:p[i] = position of the character of s that is in position i of t.It can be seen that if p = [n, n-1, n-2, ..., 3,2,1] we would have the string s inverted.Example: s = "abcad" t = "dacba" p = [5,4,3,2,1] It can also be seen that the number of inversions in this array is equal to the number of operations required to place all letters in their initial positions.But here we are faced with a problem, we can see that the number of inversions of this array is maximum (read the article in the link to understand), so we need to somehow try to minimize it to the maximum, how to do this?Since some characters in the string s can be repeated, let's take advantage of this.Example: s = "abcad" t = "dacba" p = [5,4,3,2,1] p' = [5,1,3,2,4] Note that in the example above the two arrays p create the same string t, but p' needs a smaller number of operations to be ordered (it has less inversions). This is because the 'a' appears twice in s. After that, it is possible to notice that if there are repeated letters, their positions in p must be in ascending order in the order in which they appear in s, thus the number of p inversions will always be minimized.The final answer will be the number of inversions in p.My solution (don't forget to use long long): 97206684
 » 4 months ago, # |   0 E solution with merge-tree https://pastebin.com/M0QhbX3F
 » 4 months ago, # | ← Rev. 3 →   0 nice contest
 » 3 months ago, # | ← Rev. 2 →   0 In B how do we know that lower bound of (a+b)/2 has to be taken? It is not even mentioned!
 » 6 weeks ago, # |   0 what is dp array in the solution code, seems very different from the tutorial