We will hold AtCoder Beginner Contest 139.

- Contest URL: https://atcoder.jp/contests/abc139
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20190901T2100&p1=248
- Duration: 100 minutes
- Number of Tasks: 6
- Writer: drafear, E869120, evima, gazelle, square1001, wo01, yuma000
- Rated range: ~ 1999

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

We are looking forward to your participation!

Why such worse time? There's going to be a football match here in Nanjing, China but because of this contest I can't watch it.

No one gives a fuck about Chinese football matches.

Seems that you're not very experienced in football, Chinese football matches aren't worth watching...

i don't think so, It's just that it's not as fascinating as it is in Europe

I can understand your pain, guy. Chinese football matches are much better than foreign ones, and taking part in such a shitty contest is totally a shame! So all those who says "Chinese football matches aren't worth watching" please go away, far away!!!!!!!!

Website is very slow today

Congratulations to everyone who was able to understand the problem statement of 'B' . First study English, then write problems, I request!

Lesson learnt:- Never give an AtCoder Beginner Contest,because mortals like me still can't understand what was asked .

What a fuck thing! Problem F has a same problem in BZOJ......

I think to publish it after the contest would be better.

As a writer of this contest, please refrain to write some hints about the problem

during the contest.The fact like "this problem can be solved with DP" or "this contest is already published 3 years ago" should be writtenafter the contest.It might cause BAN or some bad thing.F: https://szkopul.edu.pl/problemset/problem/NZSCUwz2ACePsBKuVCIVzrRt/site/?key=statement

Could you please explain a solution?

Edit: https://codeforces.com/blog/entry/69500

And: https://szkopul.edu.pl/problemset/problem/vl-wrtcyzmOumStUcnXGiQTm/site/?key=statement ^^

How to solve the last subtask (n can be 200,000) ?

Check out Geothermal's blog here: https://codeforces.com/blog/entry/69500 (the second solution).

You can use two-pointers to speed it up from $$$\mathcal{O}(n^2)$$$ to $$$\mathcal{O}(n)$$$.

Can anyone please help in finding the error in my code on Problem E? I was getting TLE on last case. I used binary search. https://ideone.com/hiB9kP

Please Help!

As usual, I've posted my solutions, which you can now find at this link.

How to solve F?

I Tried all possible combination of coordinates... At an index either select it or don't. similar to knapsack dp

https://atcoder.jp/contests/abc139/submissions/7287492 — WA

HintMinkowski sum

Can you explain your method with more details?

Does AtCoder run properly on mozilla?Website and questions were not loading in mozilla.

Maybe a connection or cache issue, I had no problem with atcoder and mozilla rather than at the begin of the contest.

That's a very bad thing, to have problems " copy-paste " from other judges, it makes the round a test for googling skills

Not really, nowadays it's almost impossible to know all the problems from all platforms. In Poland, we're spending some time trying to find similar problems using Google. If we cannot find anything, then the task is fine.

How to solve F

Unofficial editorial by me. All solutions here I verified got AC by the judge.

A. (Tenki) ExplanationJust compare.

A. CodeB. (Power Socket) ExplanationSimulate adding power sockets.

B. CodeC. (Lower) ExplanationLet dp[i] be the most hops you could have taken to reach this square. (We can also do this in constant space, but it is not necessary.)

C. CodeD. (ModSum) ExplanationThe largest possible sum is clearly 0 + 1 + 2 + ... + N-1 as it maximizes each addend. But this is achievable by N, 1, 2, ..., N-1.

D. CodeE. (League) ExplanationSimulate each round. We'll maintain

`now`

, the set of all people that might be paired. Whenever we pair some people in a round, we have to add them to`later`

, which considers that they might be paired next round.E. CodeF. (Engines) ExplanationSay the origin is O, and the final destination point is P. Only vectors with a projection that is positive onto OP should be added. These vectors are vectors in the halfplane marked by the line orthogonal to OP. Thus, we should only consider vectors belonging to the same halfplane.

Sort the vectors by angle. We can directly compute the sum of vectors by prefix sums. One of these prefix sums will belong to the correct half plane.

F. CodeFor C, another approach is to let dp[i] be the number of moves you can achieve if you start from i.

imo C and D are swapped :d

I use randomize + greedy + soting and PASSED F!

This is my code

But I don't know how to solve it properly . I think I'm a little lucky

I'm not so lucky like you. QWQ

I also used randomize + greedy, and I got WA.

Just maybe your sorting function is so good.

I actually generated testcase which naive hill climbing algorithm may fail. I think it's the case of chenxiaoyan.

However, I expected that simulated annealing algorithm may pass. Within this constraints, I don't think that there exists a killer case for SA algorithm. SA is so powerful that even solves a problem in AGC.

I am really sorry that problem F was already published to some other contests. I published this because I thought to be both original and high-quality.

While I was already solving 4,000+ problems in competitive programming, there were no instance of this problem, including similar problems. I attempted many times to search "maximum distance vector subset sum"-like keywords in google, but didn't find the same question.

P.S. We created pretty tricky testcase for problem F, so only 1/10 of solutions passed, while we prepared extraordinary 7 sample testcases :)

In problem E, after checking for cycle in the directed graph formed between the matches, I found the length of the longest directed chain in the graph.I got WA with this approach.Can anybody help me where I am wrong please?

Nevermind, It worked, messed up the cycle check back then :( Check out my solution if anyone wants : https://atcoder.jp/contests/abc139/submissions/7293559

I solved it with the same idea but get WA https://paste.ofcode.org/349znw527FjtNqWSTAeDdiM could you tell me why please

Can you please explain the logic behind your approach, thank in you advance

First after we make the directed graph, and check the absence of cycles in it, to find minimum days, you must understand that the graph will be overlaps of some directed chains, and if you take the length of largest such chain, there's no other chain larger than it, and hence in those many days the rest of the chains can be allocated a day easily because at any point of intersection the topologically higher part will be contributing iff it is the longest subchain compared to the other chains and hence day assignment won't be a problem. Although I believe such is the solution, I cannot completely confirm it for any edgy test cases.

rohit_goyal, Thank you for your explanation ... I got some questions. How are you storing the graph (as adj lists or matrix) ? Can you send some links (tutorials, editorials and questions related to this problem) ?

I am storing the graph as adjacency list (instead of using pair as an element in list, I use compressed index for denoting pair (l, r) ). The only tutorial I can provide you is about how to check for cycle in graph : Link But if you want to check out another approach or more detailed solutions, check out this link below: Link

Please elaborate the problem B, what exactly we need to do in this and how to do:)

From what I could comprehend, after you apply a socket wire ( or whatever it was called, having parameter A), the old socket is converted to a multi — socket of size A. So how many minimum we need to apply to at least get B sockets? You can refer the image for A = 3 and transitions: Link : https://ibb.co/bXcss0W See the numbererd nodes?They are our sockets after adding each wire at each step of size A.

Initially, you have only 1 plug which is open. You want "B" open plugs. You have only "A" sized boards (which means each board has A open plugs.) Target is to find the minimum number of "A" sized boards required to get "B" open plugs.

The key idea is Whenever you use a brand new board, you will have to use an empty plug from previous to connect this.

So essentially whenever you add a board you add "A-1" open plugs and not "A". Just simulate this by adding boards until you reach B or more.

My approach for F:

Keep track of the convex region we can reach. This can be described as a set of points. For each incoming vector, apply it to every point on the boundary, and keep only the convex hull of the resulting set of points.

I was concerned this would TLE if the hull got too big, but surprisingly this passed in 2ms. Are there any countertests for such an approach when n = 100? The POI version where n=200,000 seems like it would break this.

Useful conclusion: a convex hull on integral points with $$$x$$$ and $$$y$$$ coordinates both in range $$$[1,A]$$$ contains at most $$$O(\sqrt{A})$$$ points. In this problem, the maximum absolute value you can reach is $$$N \cdot maxX \leq 10^8$$$, so at most $$$O(\sqrt{N \cdot maxX}) \leq 10^4$$$ points.

Thus, even if you brute-force the convex hull after each vector, the total complexity will be $$$O(NP \log P)$$$(Let the size of the hull be $$$P$$$), which can fit the time limit.

Of course this won't work for $$$N=200,000$$$. In fact, we can scan the vectors with a half plane. Update the answer when their is a vector go into/out of the half plane. The total complexity is $$$O(N \log N)$$$(imo).

Maybe you can share your convex hull code here, because I didn't implement it :D

I think the convex hull on integral points with $$$x$$$ and $$$y$$$ coordinates both in range $$$[1, A]$$$ contains at most $$$O(A^{2/3})$$$ points, not $$$O(\sqrt{A})$$$ points.

That's because there are $$$O(n^{2/3})$$$ pair of $$$(x, y)$$$ which is $$$1 \leq x, y \leq O(n^{1/3})$$$ and $$$gcd(x, y) = 1$$$.

Can you link some proof regarding this claim, I am not able to find on google..

That is a very useful conclusion! While the result seems intuitive, is there a constructive proof?

Here's my code from the contest: https://atcoder.jp/contests/abc139/submissions/7268385

See this comment.

Wow, grats for actually thinking of this in contest fairly quickly. I saw it and immediately thought of https://codeforces.com/contest/995/problem/C, remembering that even though there was an elegant solution, randomization + greedy heuristic solved all cases and was hard to break.

Turns out same was true for this one.

The sample code of problem F in official editorial returns 404.

Can Some one please explain E: League and F:Engines solution.

Let's create a directed graph. Where each node is a match played between two players. Now let's add edges to this digraph. For an ith person's required order we add a directed each for each adjacent positions. That is for j in range (1..n-1) add directed edge from (j-1) to j. Also since match played between a&b and b&a is similar we merge these edges into one.

Now we have a directed graph. We just check the topological ordering of this graph. If it's cyclic then we don't have an answer.

Now to find the minimum days: For a node who has not yet finished (is still waiting on some nodes to finish), this node can only finish when the max days taking node it is still waiting for finishes and then it takes 1 more extra day to finish.

Obviously, the nodes which don't wait for others to finish end on day 1. For other nodes, we update them using the previously done. And maintain maximum throughout.

What I have done is create a vector of set where each set in a vector represents the team that played on that particular day and at last output the size of vector (the number of days).

How I did it?Iterate through all the existing sets of vectors, if neither of the two element(i and Aij) are present in that set, add both the elements to that set and if none of the set satifies this condition then add a new set to vector. Also, I have kept a value = temp1 which is equal to the kth vector(the vector in which last pasir of elements were added), this also helps to know if any two cases are contradicting.Can someone please help me out in finding error as I am getting WA in some test cases.Here is my solution:- https://atcoder.jp/contests/abc139/submissions/7296021

In E : first observation was to see, among all the possible matches , which would be the first match . if we say the i th player played the first match with the j th player. then in the column of i , j would be the first element and in the column of j , i would be the first element . Now since on day 1, i th and j th player has played , so they can't play more matches on day 1. so we try to find some more pairs for day 1, if they satisfy the above criteria . we can do this while traversing the first column. after finding all the pairs which played on day 1 , we can start to find pairs who played on day 2. for finding the pairs what i did was to initialize n pointers to 1. if some pair satisfy the criteria , I incremented their respective pointer by 1.when we find no pair , we break out of the loop. at the end, if all pointers are at n , which means all have played , we simply print the number of days , else we print -1 . Hope it helps. for more clarity, you can see my code : Link

What is the time complexity of your solution? To me it looks O(n^3) in case where only one match is played in each day because your while loop will run n^2 and inner loop i O(n) or am I wrong?

Yup ,you are absolutely correct. It is my bad that i didn't calculate the worst time complexity of my solution. On a little thought , it can be optimised by using containers like map or set where finding such a pair is relatively easy.

F was well-known, but I didn't know it.

I hope there will be no well-known problems next round.

well, an well known problem appearing in any contest doesn't destroy the fun of problem solving as long as the problem is elegant/crafty, right? if someone knows the solution beforehand they can just solve it quickly and go rewatch some rick and mortys. On the other hand, it might turn out to be a wonderful contest with cool problems :)

An observation for F: we always choose a vector forming an angle with the previous vector in range (-pi/2; pi/2). Therefore, F can be solved in O(nlogn) by sorting and two pointers.

how can i solve E-league with topological sort? I've figured out that if it contains cycle than answer is not possible. In all other cases we have to output maximum depth of that tree. Is it right approach? My WA solution

in E why numbering of node for (x,y) is min(x,y)*N+max(x,y)?

Assuming you are asking 0-indexed numbering: First of all taking minimum and maximum is to avoid distinction between pairs like (1,2) and (2,1) or you can say we need unordered pairs in the problem. Secondly the numbering is one of possible numbers you can take. This numbering is just indicating, say in a grid, you are at (1,5) and grid size is 6*6, so the assigning numbers from first cell to the required cell, starting with 0,you get number 1*6 + 5(only for 0 based indexing ), so think it like this, at (i, j) cell, you need to move n*i number of cells along rows and then the jth cell is your requirement, hence the number n*i+j.

Thank u :D

Anytime mate!

Why English editorials are not linked on englsh website, they are available on Japanese one?

Problem "D" should be swapped with problem "B" or "C".

Aside from simulating, there is a one liner solution for B:

Spoilerceil((b-a)/(a-1))+1

Because of the following inequality: (n-1)*(A-1) + A >= B

I passed F just by random_shuffle.

https://atcoder.jp/contests/abc139/submissions/7278813

you guys must have some kind of magic wand!

Problem E:

Can someone speed up my solution?

I'm using deque and finding possible matches between players, and combining if it's possible. For some reason it's TLEing in one test :- Submission

Here's my solution code for F. Notice that the solution set of vectors will consist of those vectors which are all in one half of the plane so that they all add positive components toward the final destination vector. So we need to pick contiguous vectors (sorted by angle) with the max difference of angles being 180 degrees. However, we need not worry about the 180-degree difference limit as while checking for all possibilities, the extra vectors will only reduce the answer. The answer remains unaffected. So our task is reduced to finding some set of contiguous vectors so that the final answer is maximized. This is easily done by prefix sum in O(n^2). Make sure to consider the original vectors twice, so that the conditions such as (i, i+1, ..., n, 1, 2 ) are considered while constructing the solution. We pick anywhere from 1 to n vectors to check for a possible solution.

I wonder why use cross product to sort can not pass? But atan2() is ok...

Went over AtCoder Problems and found that the difficulty of D in this contest is extraordinarily low.. Thought I'll give it a try. Surprised of how I came up with the solution instantly. Normally I could only solve problem A, B and C. Even problem C could be tough to me.