### awoo's blog

By awoo, history, 15 months ago, translation,

1809A - Garland

Idea: BledDest

Tutorial
Solution (Neon)

1809B - Points on Plane

Idea: BledDest

Tutorial

1809C - Sum on Subarrays

Idea: BledDest

Tutorial
Solution (BledDest)

1809D - Binary String Sorting

Idea: BledDest

Tutorial
Solution (Neon)

1809E - Two Tanks

Idea: BledDest

Tutorial
Solution (awoo)

1809F - Traveling in Berland

Idea: BledDest

Tutorial (Neon)
Solution (awoo)

1809G - Prediction

Idea: BledDest

Tutorial
Solution (BledDest)
• +139

| Write comment?
 » 15 months ago, # |   +3 What is the purpose behind setting limits such that (na + nb + ab) logn solutions fail E?I have tried to optimize it, i got it down to 2.5s in a mashup with adjusted limits, couldnt get it to pass.The problem was already quite hard enough without the limits, imo n<=1e3 wud be much better.Good contest otherwise though, i liked both C and D
•  » » 15 months ago, # ^ |   +7 If you let $n = 1000$, squeezing $O(nab)$ with some constant optimizations into TL feels very probable
•  » » » 15 months ago, # ^ |   +5 That was just an instance, even n = 5000 allows both my solutions (segment tree/bs + sparse) to pass (i think though it might be tight for seg)Especially sad for us since a slightly different idea and the same complexity passes due to good constant.
•  » » 15 months ago, # ^ |   0 My (na + nb)logn solution seemed to pass pretty comfortably: https://codeforces.com/contest/1809/submission/198874838.
•  » » » 15 months ago, # ^ |   +3 Your solution has a completely different idea (and is similiar to the actual idea i guess) I will explain my solution : If each operation moved the maximum amount it could, the problem was simple, so lets try to simulate each process for a certain starting position till the next time we dont move the maximum amount. This can be done easily with a segment tree.(or bs + sparse table) If we memoize the values, this becomes O(na + nb + ab) logn Reasoning : there are atmost 2*na + 2*nb + ab states because after each operation, because each simulation is run either at a starting position(ab of them) or when atleast one tank is either empty/full (2na + 2nb)
•  » » 15 months ago, # ^ | ← Rev. 2 →   +5 I don’t even know how to optimise my solution more to get it to less than 2 seconds, it’s at 7 seconds mark. the issue is that it feels that it may pass, so we waste time coding it
•  » » » 15 months ago, # ^ |   0 Wait, really? $10^7$ times log operations (also including the fact that this logarithm comes from segment tree, so the constant is big) feels passable in 2 seconds? Can you show any problems where it actually passes under those constraints?
•  » » » » 15 months ago, # ^ |   +13 Yeah it is very tight i agree, 1e7 * 2 * log (1000) * segtree constant = 2e8 * segtree constant, so mathematically I didn’t feel like it’d certainly TLE
•  » » » » 15 months ago, # ^ |   +5 1) you do not need a segment tree, you can use binary search + sparse table 2) the log factor is a logn, which is around 13, so its about 2.5 * 10^8 complexity, not unreasonable 3) my recursive segment tree runs in 4s which is only twice the TL. If i could write iterative segmemt tree, i am pretty sure it would pass
•  » » » » 15 months ago, # ^ |   +5 https://codeforces.com/problemset/problem/1523/H Intended solution complexity : 4e4 * 900 * log in 2s If i am not mistaken thats 3.6e7 log.
 » 15 months ago, # |   -7 I tried every combination for problem C after setting the number 2 and it got accepted. Here is the CODE : 198939113
•  » » 15 months ago, # ^ |   0 Your code is not understandable, Could you explain your idea?
•  » » » 15 months ago, # ^ |   0 Its simple i just take 2 and created an array of just 2 till the no. of positive subarrays in it is <=k then there can be two cases :1) no. of positive subarrays = k this is the best case so we will just put -1000 on the rest of the places.2) No. of positive subarrays>k .Then i just removed a 2 then look for a negative element which satisfy the condition no. of positive subarrays=k by brute force.This will have at max = 30*30*1000=9*10^5 operation for the worst case. which is under the time limit.P.S: i think i just poorly implemented it but yk thats just the first thing which came to my mind and it worked XD
•  » » » » 15 months ago, # ^ |   0 I used a similar strategy (https://codeforces.com/contest/1809/submission/198826221) but you don't need to do a search to work out the negative element. You can simply use:(k — number of positive subarrays)*2 + 1
•  » » 3 months ago, # ^ |   0
 » 15 months ago, # |   0 another solution for task Bhttps://codeforces.com/contest/1809/submission/198975521
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 nic3
 » 15 months ago, # |   0 Can someone help me prove my solution for C? I thought my solution was an elegant linear time solution but it did rely on one claim I was only probably sure was true. My claim was that: for any number 0 <= k <= n*(n+1)/2, and with access to the numbers 1...n, we can always greedily choose the largest n that fits and add it to the sum until we reach k. If this claim is true, then we can solve C using the following strategy: Initialize an array of length n, all 0sIf we "set" index i of the array, then it will create n-i positive subarrays. We set the largest indices that fit into the sum of k.Now we iterate through the array backwards and set the values such that the set indices make every subarray ahead of them positive and the unset indices make every subarray ahead of them negative.Solution: 198783142I didn't prove that we can always make k greedily using the numbers 1...n, but I thought it might be true because it felt similar to the Div 4 G problem.
•  » » 14 months ago, # ^ |   0 "If we "set" index i of the array, then it will create n-i positive subarrays". I'm pretty sure this is not true. For example, array 00100 has exactly 5 positive subarrays. However, I think that you probably wanted to say that if I turn on all the bits from 0 to i-1, then turning i on would add exactly n — i — 1 positive subarrays.
 » 15 months ago, # |   0 Problem E becomes more interesting if instead of requesting all (c,d) pairs we only request q pairs. Limits like n<10^4, a+b<10^18, q<10^6
 » 15 months ago, # |   +34 1809F - Traveling in Berland could be solved in $O(n)$ by using 2 pointersWe can duplicate $a, b$, st $a[i + n] = a[i]$ and $b[i + n] = b[i]$Suppose we are solving for $k$ so our path starts at $k$ and ends in $k + n$For example we have $x, y, z, \dots, last$ st $k \le x \le y \le z \le \dots \le last \le k + n$ and $b[x] = b[y] = b[z] = \dots = b[last] = 1$We can split path into $k \rightarrow x \rightarrow y \rightarrow z \rightarrow \dots \rightarrow last \rightarrow k + n$For points $x, y, z, \dots$ we can use 2 pointers. For example in point $x$ we will buy $\min(len[x \rightarrow y], limit)$ if $limit < len[x \rightarrow y]$ we will buy more in other points with cost 2For $k \rightarrow x$ and $last \rightarrow k + n$ we can compute naively. Total complxity $O(n)$ 198830497
 » 15 months ago, # | ← Rev. 3 →   +29 Graph explanation of E: (consider for all cases of (c, d) with same value of c+d)
•  » » 15 months ago, # ^ | ← Rev. 3 →   +3 Agree that nO(log n) is overkill.I used a similar (the same) idea (199142190) — in gas station i with $b_i=\1$ (let's call it 1-station) you always fill the minimum of full tank, and fuel required to reach the next station j that is 1-station or j is final.You could pre-compute the amount you fill (call it $w_i$) at the 1-station i. Given starting position start, the total you fill at all 1-stations is $\sum(w_i, i \in 1-station) - w_j + FilledAtj$, here j — is the last 1-station on a journey started at start. So it is enough to keep track of the last 1-station on a journey and compute filled amount at this station — easy given that the end of journey is at start.P.S.: Obviously your answer for each journey is 2 * Total Fuel Required — Total Filled At 1-stations.
 » 15 months ago, # |   +3 if anyone solved D using dp, can you pls tell me the approach?
•  » » 15 months ago, # ^ |   0 I just memoized all states using DFS. If we are at a position and it is a 1, we can either continue or delete it and incur the cost. There is a third option of swapping with the number in front of it if it is a 0 and we have no other 1's to the left. If we are at a position and it is a 0, we can delete it and incur the cost. If there are no 1's the left, we can either continue or swap it with the number in front of it. The memoization will look like memo[pos][has_0][swapped] for each state
•  » » 15 months ago, # ^ |   +17 Let $dp_{i, j}$, be the cheapest way to process the first $i$ characters of the string with the following states: $j = 0:$ The modified string is of the form $\dots 0$. $j = 1:$ The modified string is of the form $\dots 01$. $j = 2:$ The modified string is of the form $\dots 11$. You can see the transitions in more detail here: 198854887.Or without array: 198995188.
•  » » » 11 days ago, # ^ |   0 How did you come up with the dp states? Any intuition that helped? Thanks.
»
15 months ago, # |
Rev. 2   -49

I actually didn't like the tutorial for task B

It's way too complicated for beginners

# O(1) Solution

#include <bits/stdc++.h>
using namespace std;
#define int long long

int32_t main() {
int T;
cin >> T;
while(T--) {
int N;
cin >> N;
int r = ceil(sqrt((double)N)/2);
while(4*r*r < N) r++;
int mx = 2*r - 1;

r = ceil((sqrt((double)N)-1)/2);
while(4*r*(r+1)+1 < N) r++;
int mxx = 2*r;

if(N == 1) cout << "0\n";
else cout << min(mx, mxx) << "\n";
}
return 0;
}


# O(logN) Solution

#include <bits/stdc++.h>
using namespace std;
#define int long long

int binarySearchEven(int N) {
int start = 1, end = 1e9, r;
while(start <= end) {
r = start + (end - start)/2;
if(N > 4*r*(r+1)+1) start = r+1;
else end = r-1;
}
return start;
}

int binarySearchOdd(int N) {
int start = 1, end = 1e9, r;
while(start <= end) {
r = start + (end - start)/2;
if(N > 4*r*r) start = r+1;
else end = r-1;
}
return start;
}

int32_t main() {
int T;
cin >> T;

while(T--) {
int N;
cin >> N;

int s = 2*binarySearchOdd(N) - 1;
s = min(s, 2*binarySearchEven(N));

if(N == 1) cout << "0\n";
else cout << s << "\n";
}
return 0;
}

•  » » 15 months ago, # ^ |   0 Try to format the code while commenting. Otherwise it becomes unreadable, and will result in huge downvotes.
•  » » » 15 months ago, # ^ |   0 It appeared in order, but after posting it took a random configuration... Don't know why
•  » » » » 15 months ago, # ^ |   0 There is an option while commenting, if you want to write source code. You can use that.
•  » » » » 15 months ago, # ^ |   0 sqrt() method isn't O(1) . so your O(1) solution is not really O(1).
 » 15 months ago, # | ← Rev. 2 →   0 I solved F after the contest with a solution that is O(n) 198883707, then noticed that each b_i is limited to either 1 or 2. The 1 or 2 constraint is essentially not required and would only slightly simplify the code.
•  » » 15 months ago, # ^ |   0 Can you explain your solution?
•  » » » 15 months ago, # ^ |   0 First detect consecutive sequence of cities with non-descending fuel cost per liter. A round trip started at a city not in the sequence would visit all the cities in order from begin to end. A round trip started at a city within the sequence would visit the suffix cities and later all the prefix cities in the sequence. In either cases, because the fuel price is non-descending, it is always optimal to purchase as much fuel as sufficient to reach the end of the sequence limited to the tank capacity of course.For each such sequence [B,E], we run DP to compute the pre[] and suf[] array where pre[i] is cost to reach i from B and suf[i] is the cost to reach E+1 from city i. Such computation is O(M) where M is the length of the sequence. I will skip more details to compute the pre and suf array. Once we have all the individual sequences handled, for each city i, its round trip cost includes the whole cost of all other sequences it does not belong to, plus pre[i] + suf[i]. It takes O(N) to compute the value for all the cities as answer. Overall the complexity is O(N).
•  » » » » 7 months ago, # ^ |   0 In a non-descending sequence, it's true that you should always purchase enough fuel to reach the end (if possible), however it remains unclear whether you need to buy more. In this problem $b_i \le 2$ so the next sequence must start with 1, so that you will never buy more. But if $b_i \le 2$ doesn't hold, you should buy more if the price in the first city of the next sequence is higher than current.
 » 15 months ago, # |   0 Can anyone explain B's given test case? Why is answer for 975461057789971042 987654321? I am sure it should be 987654320.
•  » » 15 months ago, # ^ | ← Rev. 2 →   0 the answer would have been 987654320 if the input was 975461057789971041 (1 less than the current input)
•  » » 15 months ago, # ^ |   +5 Use 'sqrtl' to find the square root. The input is of the order 10^18 so sqrt will give precision errors. The exact square root of 975461057789971042 is 987654321.000000000506249999, ceil of that minus 1 is the final answer ~ 987654321.
•  » » 15 months ago, # ^ | ← Rev. 2 →   +4 There are 4 * m points (x, y) that satisfies |x|+|y| = m for m >= 1. The diagram below shows points 4 + 12 = 16 points for m = 1 and 3. The total number of points for m = 1, 3, 5, ..., 2 * k — 1 is 4k^2. For k = 493827161, the value is 975461059765279684 which is greater than 975461057789971042, so the answer should be <= 2k-1 = 987654321.  . o o . o o . o . o . o . o . o . o . . o . o . o o . o o . An illustration that 987654320 won't work: if we use m = 0, 2, 4, 6, ..., 987654320, the total number of points is 1 + 4 * (2 + 4 + ... + 987654320) = 1 + 987654320^2 = 975461055814662401 which is one less than the required 975461055814662402 in your example.
 » 15 months ago, # | ← Rev. 2 →   +8 A simple solution for task B is just print " (int) sqrt(n-1)" ;)your have to define int to long long int.
•  » » 15 months ago, # ^ |   +1 It works only on C++17.
•  » » 11 months ago, # ^ |   0 any idea why this is correct? i used the same but am not able to prove it is always correct like they did in the editorial.
 » 15 months ago, # |   0 I have two almost similar logic solution for problem B, one is failing while the other one is working.Could someone help understand what's going wrong here? Also I see that for c++ sqrt(n-1) works while not for JAVA, why is that so?
•  » » 15 months ago, # ^ |   0 there are precision errors. in java , the number is converted to double before taking the root. eg — root 25 can be 4.999 and the int of this would be 4 , so we need to add this line while(x * x < n) x++; so if there is any precision error it would be corrected
 » 15 months ago, # |   0 O(1) solution for B:Solution: If N is a perfect square then the answer is √N — 1 and otherwise it's just √N198856454
•  » » 15 months ago, # ^ |   0 you can just calculate floor(sqrt(N-1))
•  » » » 15 months ago, # ^ |   +3 Yeah, listen to the expert ^^
•  » » » 11 months ago, # ^ |   0 any idea why this is correct? i used the same but am not able to prove it is always correct like they did in the editorial.
 » 15 months ago, # |   0 1809G is similar to 1437F, although the constraint of 1809G is tighter.
 » 15 months ago, # | ← Rev. 2 →   0 In problem D, how do they determine that it is optimal to have at most one delete operation.
 » 15 months ago, # |   0 Perhaps few person remember that EDU rounds may reuse old problems.Actually 1809E - Two Tanks is a resemble of an old task OneDimensionalRobot from SRM 608 authored by rng_58.
 » 15 months ago, # |   0 https://youtu.be/xvyXv1IO7ygVideo sols for A,B,C
 » 15 months ago, # |   0 F can be solve in O(n) instead of O(nlogn). You can preprocess (not sure how to say it, you get the idea) the prefix and suffix of every position with gas price 2, then calculate the answer of any city with gas price 1.The difference between different starting points will be: the concecutive price 2 cities that got seperated by the starting position. Just calculate the offset with prefix and suffix then you get the correct answer.199636012
 » 15 months ago, # |   0 Is D solvable if it was given a permutation instead of a binary string?
 » 14 months ago, # |   0 I get Wa5 because of I mul 1e12 and I dont realize it, terribly
•  » » 14 months ago, # ^ |   0 in problem D
•  » » » 4 days ago, # ^ |   0 .ComplaintFrame { display: inline-block; position: absolute; top: 0; right: -1.4em; } .ComplaintFrame a { text-decoration: none; color: #ff8c00; opacity: 0.5; } .ComplaintFrame a:hover { opacity: 1; } ._ComplaintFrame_popup p, ._ComplaintFrame_popup button { margin-top: 1rem; } ._ComplaintFrame_popup input[type=submit] { padding: 0.25rem 2rem; } ._ComplaintFrame_popup ul { margin-top: 1em !important; margin-bottom: 1em !important; } Can you explain why did this give a WA? I did the same mistake and got AC after using a new variable assigned with $1e12$.WA submission : 265403408 AC submission : 265403860
 » 14 months ago, # |   0 can anyone recommend problems similar to E
 » 12 months ago, # |   0 Alternate solution for C in O(n): https://codeforces.com/contest/1809/submission/209213026We realise we can form an array [2, 2, 2, 2, -x, -1000, -1000 ...], -x will depend on the number of good subarrays you have yet to form from [2,2,2,2]. We try our best to form the most good subarrays at the left side, then once we could not do it anymore, we still have some leftover good subarrays to form. We can then set the value of -x to be such that we will form +leftover more good subarrays.
 » 12 months ago, # |   0 Problem G is totally the same as 1437F - Emotional Fishermen.
 » 11 months ago, # | ← Rev. 2 →   0 Alternate $O(n)$ solution for problem F:Firstly, let us divide the city into maximal continuous segments of type-$1$ and type-$2$ cities (cities having fuel cost 1 and 2 respectively) by considering the starting city to be $1$.Observations: For any type-$1$ city $x$, it is never suboptimal to buy all the fuel we use for crossing it, in that city itself. From observation $(1)$, its easy to prove that it is never suboptimal to arrive in a type-$1$ city with $0$ fuel left. But however, sometimes we need to buy fuel in type-$1$ cities and use that fuel to cross type-$2$ cities. From observations $(1)$ and $(2)$, it's trivial to show that it's never suboptimal to buy extra fuel in a type-$1$ city, only when there is a type-$2$ city next to the type-$1$. (ie, the type $1$ city lies at the end of its segment) When at a type-$1$ city $x$ which lies at the end of its segment, it's optimal to buy $min(k - a_x, \sum{a_i} (\text{over next type-2 segment}))$ extra litres of fuel to use in the next type-$2$ segment. So for a fixed starting point $s$, the maximal segments are defined and constant. For a type-$2$ segment $S$, define : $sub_S$ = extra type-$1$ fuel we buy from the city just to the left of current segment. $sum_S$ = $\sum{a_i}$ over all $i$ which lie in $S$. The answer for a set of maximal segments is $\sum{a_i}(\text{over all type-1 segments}) + \sum{sub_s + 2(sum_s - sub_s)} (\text{over all type-2 segments})$.Now when we iterate over the starting city $s$, at most two terms in this summation change on each iteration (corresponding to the type-$2$ segment we are "splitting" by $s$), these can easily by fixed in $O(1)$.Implementation: link
 » 3 weeks ago, # |   0 Simple O(N) solution to problem C: 262967868