### chokudai's blog

By chokudai, history, 4 years ago,

We will hold AtCoder Beginner Contest 151.

The point values will be 100-200-300-400-500-600.

We are looking forward to your participation!

• +12

| Write comment?
 » 4 years ago, # |   -8 Hope that it's rated!!
 » 4 years ago, # |   0 How to solve F?
•  » » 4 years ago, # ^ | ← Rev. 2 →   0 Check the comment section of this blog https://codeforces.com/blog/entry/49934
•  » » 4 years ago, # ^ |   +6 you can learn about Ternary_searchMy Code
 » 4 years ago, # |   +5 Hi, alex9801. You solved F in 2 minutes and 30 seconds!Also, I just want to say that your code was written by me! :)
•  » » 4 years ago, # ^ |   +19 Someone told me to check out atcoder for some quality problems lol
•  » » 4 years ago, # ^ |   +21 Strongly disliked this problem. You either copy-paste the solution or spend all the time googling formulas and end up far behind.
•  » » » 4 years ago, # ^ |   +3 I agree, but let's look at the bright side : We got to learn something new and possibly useful
•  » » » » 4 years ago, # ^ |   0 Agreed. I actually happened to know the O(n^4) algorithm (who didn't?), but this contest helped me find simple closed formula for the circumcenter.
 » 4 years ago, # |   0 F-Enclose All seemed to be binary search on real numbers, but I could not figure out how to solve keeping both x and y co-ordinate into account. Can anyone explain his/her approach towards the problem?
•  » » 4 years ago, # ^ | ← Rev. 2 →   0 I copied this. Explanation
•  » » 4 years ago, # ^ | ← Rev. 2 →   +10 Smallest enclosing circle is either one of followings: having two points as diameter. circumcircle of three points. So, checking all possiblity, you can get O(n^4) solution.
•  » » » 4 years ago, # ^ | ← Rev. 2 →   0 Is there a mathematical approach towards validating this assumption?
•  » » » » 4 years ago, # ^ |   0 I cannot fully understand what do you mean. If you are finding formula of circumcenter, check this link https://en.wikipedia.org/wiki/Circumscribed_circle#Triangles
•  » » » 4 years ago, # ^ |   0 My solution is $O(n)$: link(tbh i forgot proof)
•  » » » 4 years ago, # ^ |   0 Can u please give the proof of above. How do the two conditions lead to the smallest enclosing circle.
•  » » » » 4 years ago, # ^ |   0 If either two points not diameter, or one point is on a circle, you can adjust center little bit, resulting larger circle. More explicitly, denote center as $C$, points as $P_i$ one point on circle: move $C$ toward $P_1$, and keep radius $CP_1$ until your circle meet other point. two points on circle: move $C$ along line perpendicular with $P_1P_2$, keep radius $CP_1 = CP_2$ until it cannot be moved further or meet other point.
•  » » 4 years ago, # ^ |   0 I used two ternary searches. Take two potential x-coordinates on outer ternary search and for each of them calculate the best radius using nested ternary search over y-coordinates.
•  » » » 4 years ago, # ^ |   0 I didn't go for this method because I thought the circle might not be unique, just found I was wrong about the uniqueness of the circle.
 » 4 years ago, # |   0 How to solve E?
•  » » 4 years ago, # ^ |   0 you can count the contribution of each element as 1. minimum and 2. maximum.
•  » » 4 years ago, # ^ |   +9 Sort all the values and calculate contribution of each element in the final sum. If the position is $i$, then contribution is $a[i]*(C_{k-1}^{i} - C_{k - 1}^{n-i-1})$
•  » » 4 years ago, # ^ | ← Rev. 2 →   0 It can be solved by just fixing the min and max elements and rest can be chosen arbitrarily from elements having values in between them. 1. Sort the array. 2. Calculate prefix sums. 3. After fixing the min and max elements in the array, there can be k-2 to n-2 elements in between the min and max elements. So loop over them and multiply the value with (no. of elements in between)C(k-2). Note: - C means Combination. - Keep % 1e9 + 7 into account as well.
•  » » 4 years ago, # ^ |   +1 Sort the array, now notice that, you can choose all elements from k to n as the maximum number. Now if you choose i th number as the maximum, then you can take k — 1 numbers which are smaller or equal to i th number. so i th number will appear ncr(i — 1, k — 1) times as maximum.For minimum, we can choose numbers from 1 to n — k + 1, suppose you choose i th number as minimum, the you can take k — 1 numbers which are greater or equal to i th element. So i th number will appear ncr(n — i, k — 1) times as minimum.Then our answer is max_sum — min_sum
 » 4 years ago, # | ← Rev. 2 →   0 Why does this TLE in D. I did bfs from all the points. https://atcoder.jp/contests/abc151/submissions/9478877
•  » » 4 years ago, # ^ |   +3 I think that you have to set vis[ x ][ y ]=true right after you push it into the queue, not when you pop it.
•  » » 4 years ago, # ^ | ← Rev. 3 →   0 You should calculate all distances using floyd-warshal, which runs in O(n^3).example solutionEdit: Turns out that a BFS from all points is actually faster. Since complexity is O(N^2 * N*V) there should be cases where it is slower indeed. However, not for the testcases given in this contest.
•  » » » 4 years ago, # ^ |   0 I guess bfs from all vertixes has O(n^2) complexity, because we have linear number of total edges. (Each vertex has no more than 4 edges, so there are no more than 2*n edges)
•  » » » » 4 years ago, # ^ |   0 I think the BFS has to be done N^2 times, and each time we have to travel at most all edges.
•  » » 4 years ago, # ^ |   +1 You can also just use bfs instead of dfs.Maybe due to recurssive calls you are getting TLE.My submission
•  » » » 4 years ago, # ^ |   0 Lol no i just named it dfs it's bfs only i didn't write vis[nx][ny] = true in the solution which made it exponential .
•  » » » » 4 years ago, # ^ |   0 Yeah, that has happened with me before.It's a good practice to write vis = true when we are pushing into the queue, not when we are popping from the queue.
 » 4 years ago, # | ← Rev. 2 →   0 Can someone help in telling the DFS approach to problem D? Why this is wrong
 » 4 years ago, # |   0 https://atcoder.jp/contests/abc151/submissions/9466136why got TLE in D.
•  » » 4 years ago, # ^ |   0 Why are you doing dfs, we need to find minimum distance from each point
•  » » » 4 years ago, # ^ |   0 yes but dfs approach also gonna work. TLE is because of recursion?
•  » » » » 4 years ago, # ^ | ← Rev. 2 →   +5 dfs is really slow for shortest paths (in worst case, it is exponential in complexity). You should use bfs instead.However, problem complexity is low enough that it can be cheesed with FW.
•  » » » » » 4 years ago, # ^ |   -10 DFS is EXPONENTIAL in worst case.. Seriously dude??
•  » » » » » » 4 years ago, # ^ |   +8 Allow me to correct myself: in the problem of finding shortest paths, if you want a correct answer, DFS is exponential in worst case.As a graph traversal algorithm, it is not exponential, but this is specifically in context of the shortest-path problem, where it does indeed either give WA or is exponential in worst case, and I apologise for the unclear statement.
•  » » » » » » » 4 years ago, # ^ |   0 yea..within context now
•  » » 4 years ago, # ^ |   -8 for(int j = 0;j < h; ++j) Shouldn't it be j < w?
•  » » » 4 years ago, # ^ |   0 oh my bad, but still TLE https://atcoder.jp/contests/abc151/submissions/9478877
•  » » » » 4 years ago, # ^ |   0 you're not doing bfs correctly, you have to mark node as visited earlier: https://atcoder.jp/contests/abc151/submissions/9479162
 » 4 years ago, # | ← Rev. 4 →   0 deleted
 » 4 years ago, # | ← Rev. 2 →   -17 weak test cases for problem C and wrong answer in editorialit not cover the case when WA for that question is present but contestant not submit any problem show "AC".
•  » » 4 years ago, # ^ |   0 What do u mean by weak test cases ? I forgot to check for case when WA is present and AC not present and got wrong answer . After I check that case I got AC. Also the editorial to the problem covers that case . Don't spread wrong information for no reason.
•  » » » 4 years ago, # ^ |   -20 Brother , there is case like8 5 1 WA 1 WA 1 WA 1 WA 1 WAtry this...it is not coveredi think answer shoud be 5
•  » » » » 4 years ago, # ^ |   0 Answer should be 0,0 since no problem solved, no penalties.
•  » » » » » 4 years ago, # ^ |   -16 panalty should be 5 ,i guess...for 5 wrong questions
•  » » » » » » 4 years ago, # ^ |   0 Read the problem properly.
 » 4 years ago, # |   +2 Geothermal Please make the editorial for this contest, like you always do.
 » 4 years ago, # | ← Rev. 2 →   0 Can someone tell me why this has WA for Problem E in a few cases? https://atcoder.jp/contests/abc151/submissions/9466294I sum the contribution of elements that can be max, and the contribution of elements that can be min, and then subtract them in the end (S_max - S_min)
•  » » 4 years ago, # ^ |   +3 Well I figured out what was wrong with it, and it turns out it could be one of those differences in compilers/configuration and handling of data types and so? In my code, I had #define mod 1000000007, when I change it to a declaration of a long long variable ll mod = 1000000007; the same exact code passes, here.It's weird, I've used this in many problems before without problem, so I am suspecting, something is off with AtCoder's settings, I could be wrong though. BTW, I did try declaring it as an int variable, but that doesn't pass. In either case, can anyone clarify or shed some light on this issue?RaunaqRoxx This looks like it's what's wrong with your code as well.
•  » » » 4 years ago, # ^ |   +4 If you use #define mod 1000000007 or int mod = 1000000007, you will get an integer overflow in cout << (mxS - mnS + mod) % mod << '\n'; if mxS = 10^9 and mnS = -10^9. So you have to use #define mod 1000000007ll or ll mod = 1000000007.
•  » » » » 4 years ago, # ^ |   0 Yes, that is correct, but in my code mnS is always 0 or positive, so this expression cout << (mxS - mnS + mod) % mod << '\n'; shouldn't overflow.
•  » » » » » 4 years ago, # ^ |   +2 mnS is not always 0 or positive. a[i] can be negative, according to problem constraint.
•  » » » » » » 4 years ago, # ^ |   0 Oh, that's true .. my bad. I stand corrected, Thanks!
•  » » » 4 years ago, # ^ |   0 Really nice observation but this is not the problem with my code, I made that change and submitted, still the verdict hasn't changed, I have stress tested my solution on very large test cases and could not find a fault, does anybody have any idea how can we view the test cases?
 » 4 years ago, # |   0 Here is my submission for problem D why it is getting TLE. I have seen similar soln passing
•  » » 4 years ago, # ^ |   0
•  » » » 4 years ago, # ^ | ← Rev. 2 →   0 Got AC, but why is this so ?
•  » » » » 4 years ago, # ^ |   0 You should push to the q only if vis[x][y]==false.
•  » » » » » 4 years ago, # ^ |   0 In my this submission i have check if(isValid() && notvisted) then i am inserting into the queue.
•  » » » » » » 4 years ago, # ^ |   0 You will insert some cells into the queue multiple times
•  » » » » » » 4 years ago, # ^ |   0 You should mark a vertices as visited when you push it to the queue, not after popping it.This is your code after I made the change. https://atcoder.jp/contests/abc151/submissions/9482721
 » 4 years ago, # |   0 I will be really glad if someone can provide a test case where my submission for E is going wrong. https://atcoder.jp/contests/abc151/submissions/9484300 It's getting WA at 4/18 test cases but I cannot find any counter cases. I am doing close to what is given in the editorial.
•  » » 4 years ago, # ^ |   +3 You have to take care while taking mod of negative values. In this case array can have negative values.
•  » » » 4 years ago, # ^ |   0 Thank you so much. I didn't read the constraints properly.
 » 4 years ago, # | ← Rev. 2 →   0 I tried this gradient descent-like approach basically I check 8 directions of current point and go to the one with minimum radius (centered on that point) and break when the current point is smaller than all the 8 directions. I loop this several times with increased accuracy (double dumb). Can anyone explain why I get WA? I think its precision problem but I looped a lot of times. Any suggestions of improvements are also appreciated. code#include #include #include using namespace std; int n; double x[50], y[50]; double rad(double xx, double yy){ double res = (xx - x[0]) * (xx - x[0]) + (yy - y[0]) * (yy - y[0]); for (int i = 1; i < n; i++){ res = max(res, (xx - x[i]) * (xx - x[i]) + (yy - y[i]) * (yy - y[i])); } return res; } int dx[8] = {-1, -1 -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; int main(){ scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf %lf", &x[i], &y[i]); double cx = (*max_element(x, x + n) - *min_element(x, x + n)) / 2.0; double cy = (*max_element(y, y + n) - *min_element(y, y + n)) / 2.0; double now = rad(cx, cy), dumb = 0.001; for (int i = 0; i < 10; i++){ while (true){ double ang = 0, minn = 9999999999; int dir; for (int i = 0; i < 8; i++){ double d = rad(cx + dumb * dx[i], cy + dumb * dy[i]); if (d < minn){ minn = d; dir = i; } } cx += dumb * dx[dir]; cy += dumb * dy[dir]; if (now <= minn){ break; } now = minn; } dumb *= 0.001; } printf("%.12lf\n", sqrt(now)); } 
 » 4 years ago, # | ← Rev. 2 →   0 My Solution for problem DExplanation : I used 2 BFS to calculate the longest path in the matrix. The first BFS (function name bfs1) gives me the coordinates of the road block farthest away from the start coordinates. The second BFS (function name bfs2) takes those coordinates (which we got from bfs1) and calculates the max distance.More info. about this double-BFS method : Longest path in an undirected treeResult : I passed all the test cases except for one (i.e. hand_04 test file). I don't know where I went wrong. Guys please help finding me this corner case or if my approach is wrong please let me know.
•  » » 4 years ago, # ^ |   0 It works only for tree
•  » » » 4 years ago, # ^ |   0 Thank you mip182, I figured out the solution by doing BFS for each free path but can you tell me an efficient approach if the same problem had huge constraints because in that case this multiple BFS approach will fail.
•  » » » » 4 years ago, # ^ |   0 The question "find longest path between any two nodes" has complexity n^3.
•  » » » » » 4 years ago, # ^ |   0 Can you please explain or attach some links?
•  » » » » » » 4 years ago, # ^ |   0 In fact you can solve this in $O(n^ 2)$. All you need is iterate over vertex and calculate distances to all other vertices using BFS and then relax answer with maximum of distances.
•  » » » » » » 4 years ago, # ^ |   0 Floyd-Warshal would be a useful start point.
 » 4 years ago, # |   0 fpr E，why do i get WA when i calculate maxium but get accepted when I calculate minus first?
 » 4 years ago, # |   0 for E，why do i get WA when i calculate maxim but get accepted when I calculate minus first?
 » 4 years ago, # | ← Rev. 2 →   0 my solution Why do i get WA on C. I increment penalty for each WA for a problem untill it gets AC. Can someone please give me a sample case where it fails.
•  » » 4 years ago, # ^ |   0 Penalties for a problem don't count if it hasn't been ACed.A sample:$2$ $1$$1$ $\text{WA}$Answer should be $0$ $0$.