We will hold AtCoder Beginner Contest 151.

- Contest URL: https://atcoder.jp/contests/abc151
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20200112T2100&p1=248
- Duration: 100 minutes
- Number of Tasks: 6
- Writer: beet, DEGwer, kort0n, kyopro_friends, satashun, smeke
- Rated range: ~ 1999

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

We are looking forward to your participation!

Hope that it's rated!!

Yes, It's

rated.Hope that it's rated!!

How to solve F?

Check the comment section of this blog https://codeforces.com/blog/entry/49934

I use SA to solve this problem which it's a random algorithm.

CODE:https://atcoder.jp/contests/abc151/submissions/9474015

you can learn about Ternary_search

My Code

Hi, alex9801. You solved F in 2 minutes and 30 seconds!

Also, I just want to say that your code was written by me! :)

submission: https://atcoder.jp/contests/abc151/submissions/9442898

github: https://github.com/koosaga/DeobureoMinkyuParty/blob/master/teamnote.tex#L1535

Someone told me to check out atcoder for some quality problems lol

ABCs contain classical most of the time. Try ARC and AGC.

Strongly disliked this problem. You either copy-paste the solution or spend all the time googling formulas and end up far behind.

I agree, but let's look at the bright side : We got to learn something new and possibly useful

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.

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?

I copied this. Explanation

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.

Is there a mathematical approach towards validating this assumption?

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

My solution is $$$O(n)$$$: link(tbh i forgot proof)

Can u please give the proof of above. How do the two conditions lead to the smallest enclosing circle.

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.

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.

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.

Can you please explain your idea, of how two ternary search would work ?

How to solve E?

you can count the contribution of each element as 1. minimum and 2. maximum.

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})$$$

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.

Check the contribution of each element in each combination. Sort the array, for min iterate from the beginning and for max iterate from the end till we can get k-1 elements from the right and left side respectively.

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

Why does this TLE in D. I did bfs from all the points. https://atcoder.jp/contests/abc151/submissions/9478877

I think that you have to set vis[ x ][ y ]=true right after you push it into the queue, not when you pop it.

what, i set x and y true before i pop not after.

You should calculate all distances using floyd-warshal, which runs in O(n^3).

example solution

Edit: 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.

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)

I think the BFS has to be done N^2 times, and each time we have to travel at most all edges.

You can also just use bfs instead of dfs.

Maybe due to recurssive calls you are getting TLE.

My submission

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 .

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.

BFS for all points will give TLE because of the case 20 20 with all points. A way to make it run faster is to save distances. This way if you compute bfs for u->w passing through u->v, there is no need to compute again the distance u,v. This way for me runs way below the time limit. Code: https://atcoder.jp/contests/abc151/submissions/9460670

No bfs from all points dosen't give TLE https://atcoder.jp/contests/abc151/submissions/9479162

Can someone help in telling the DFS approach to problem D? Why this is wrong

https://atcoder.jp/contests/abc151/submissions/9466136

why got TLE in D.

Why are you doing dfs, we need to find minimum distance from each point

yes but dfs approach also gonna work. TLE is because of recursion?

dfs will not give u shortest distance thus your answer will be wrong anyway. Tho my bfs is also Tleing no idea why

yes but answer is right and also dfs can give shortest distance but worst time complexity

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.

DFS is EXPONENTIAL in worst case.. Seriously dude??

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.

yea..within context now

Shouldn't it be

`j < w`

?oh my bad, but still TLE https://atcoder.jp/contests/abc151/submissions/9478877

you're not doing bfs correctly, you have to mark node as visited earlier: https://atcoder.jp/contests/abc151/submissions/9479162

oh thanks a lot got it now

https://atcoder.jp/contests/abc151/submissions/9473024

why i got runtime error at E.

How to solve D? I used FloydWarshall and kept the max distance for every pair of routes and give me WA :( Submission

Constraints were very low. Do bfs from each non-block point and get the max.

I got it... I was assigning the edges to the nodes badly, F for me xd Code

What in #16 test in problem D? For each vertex calculate min distance to another vertex. And in all distances I chose max Code

weak test cases for problem C and wrong answer in editorial

it not cover the case when WA for that question is present but contestant not submit any problem show "AC".

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.

Brother , there is case like

8 5 1 WA 1 WA 1 WA 1 WA 1 WA

try this...it is not covered

i think answer shoud be 5

Answer should be 0,0 since no problem solved, no penalties.

panalty should be 5 ,i guess...for 5 wrong questions

Read the problem properly.

okay okay thanks

Geothermal Please make the editorial for this contest, like you always do.

Can someone tell me why this has WA for Problem E in a few cases? https://atcoder.jp/contests/abc151/submissions/9466294

I 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`

)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.

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`

.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.`mnS`

is not always 0 or positive.`a[i]`

can be negative, according to problem constraint.Oh, that's true .. my bad. I stand corrected, Thanks!

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?

Here is my submission for problem D why it is getting TLE. I have seen similar soln passing

Check this https://codeforces.com/blog/entry/73021?#comment-572800

Got AC, but why is this so ?

You should push to the q only if vis[x][y]==false.

In my this submission i have check if(isValid() && notvisted) then i am inserting into the queue.

You will insert some cells into the queue multiple times

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

Can u pls tell why i am getting 4 WA . Submission

`Used dijksta for every a[i][j]='.'`

ThanksI 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.

You have to take care while taking mod of negative values. In this case array can have negative values.

Thank you so much. I didn't read the constraints properly.

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.

codewhat's wrong with my code?(https://atcoder.jp/contests/abc151/submissions/9485843)

Can anyone explain me the problem D with the given sample example. I didnt get the output. :)

ThankYou :)

https://atcoder.jp/contests/abc151/submissions/9473024

what is wrong in my approach?

My Solution for problem D

Explanation :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 pleasehelp finding me this corner caseorif my approach is wrong please let me know.It works only for tree

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.

The question "find longest path between any two nodes" has complexity n^3.

Can you please explain or attach some links?

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.

Floyd-Warshal would be a useful start point.

Anyone explain E? how to approach this question

fpr E，why do i get WA when i calculate maxium but get accepted when I calculate minus first?

for E，why do i get WA when i calculate maxim but get accepted when I calculate minus first?

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.

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$$$.

Can sb tell me why I am getting 4 WA for problem D My submission