Tutorial is loading...

Official Implementation: http://ideone.com/jsXSst

===

Tutorial is loading...

Official Implementation: http://ideone.com/YD7s4S

**NOTE: If you get TLE, use faster input methods. For example, use scanf instead of cin.**

===

Tutorial is loading...

Official Implementation: http://ideone.com/B9pjyi

**NOTE: If you get TLE, use faster input methods. For example, use scanf instead of cin.**

===

Tutorial is loading...

Official Implementation: http://ideone.com/nlPVW0

===

Tutorial is loading...

Official Implementation: http://ideone.com/cwP55b

===

Tutorial is loading...

Official Implementation: http://ideone.com/FfnJyp

===

Feel free to ask any questions below. It's hard to write an editorial that satisfies the need of everyone. I hope you'll enjoy solving the problems :)

Thanks for providing Editorial so quickly!!

`Suppose this is not optimal, and k’ + c (c ≤ 0) roads can be shut down.`

It should be c >= 0Yes, it's incorrect. I'll fix it soon. Thanks!

In problem C does this statement means it is a tree. "There are also n - 1 wires connecting the banks." Please why it is tree ?

A graph with

nnodes andn- 1 edgescanbe a tree, but it is not guaranteed to be. For example, there could be 1-2, 2-4, 1-4 edges (4 nodes). To be a tree, the graph (that hasn- 1 edges) must also be connected (i.e. from every node we can travel to any other nodes using the edges).The condition "It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength" given in the input section infers that the graph is connected, and thus a tree.

"A graph with n nodes and n - 1 edges can be a tree, but it is not guaranteed to be." Yeah I had that doubt. Thanks for the clarification.

This is the main part in solution. But I understand the remaining part of your ideone solution. would you mind explain this part why we are doing this ?

if(x == 0)

Consider

xas the count ofm+ 2 andyas the count ofm+ 1 (after adding 2 to all banks' strengths).If there is no

m+ 2, then the answer is eitherm+ 1 orm. If there is still nom+ 1, then the answer ism.Sorry Again I can't get it.

if(a[pos] == maxval) x--, y++; (in the minus section inside for loop j variable)

if his neighbour's position is maxval so his nieghbour's value will increase by 1 we are doing x--, but why we are doing y++. (his neighbour's value will be maxval+1)

Because for neighboring nodes, if its original strength is

a_{i}, then it will be hacked with strength at leasta_{i}+ 1 (that is,m+ 1, in this case), so we will need to add 1 toy.Thanks a lot. Now I understood. Your responses were quick.

Hey !

The answer for the above case should be 8 but your code is giving 9.

Are you talking about problem Bank Hacking?

Why are there so many numbers in the second line? There are 11 numbers, but shouldn't there be 9?

Got the error thanks anyways.

In C,

`each bank’s strength can be increased at most twice`

Isn't it, each bank's strength can be increased at most [ sum of adjacent node's degrees ] times. eg: consider the star graph. However the overall total updates will not exceed 2 * ( n — 1 ).

Hmm, why is it 2*degree times? Can you provide me a sample test case?

a complete binary tree of depth 3 where the root is 1, and strength of each node is it's index number. The bottom nodes will be removed first, updating the root once. Now we severe the edges that connect the bottom most nodes to the tree. Now the bottommost nodes will be removed one by one and root will again be updated.

But you can't remove 2 bottommost nodes consecutively at first, because no bottommost node is neighboring to a hacked node (because hacked node is also at depth = 3). When you hack node at depth = 3, you'll have to continue hacking node at depth = 2.

Missed that condition. My bad :| With this constraint, the problem is simpler than what I was solving :|

If there are 4 nodes, 1,2,3,4 and edges are (1,2),(2,3),(2,4). If we hack in sequence 3,2,4,1, then 1st node's value will increase by 3. Please, clear my doubt.

Node 1 and node 4 are not semi-neighboring when you hack node 4. Node 2 must be online in order for node 1 and node 4 to be semi-neighboring (refer to the definition).

Thanks !!! big mistake in reading problem statement. :(

No. Max update is 2

Problem E can be solved in as well using the dp which was used in IOI 2016's Task Aliens. 26284310

UPDas mentioned below 26298863.Also you can instead of storing benefit of both geniuses assume that one of their benefit ended in that prefix, so original solution will be O(N * P) or with lambda optimization O(N * log(P) * K). It's awesome!

Can you explain the lambda optimization or give a link where it is described? Is that one that is described in blog named "Incredibly beautiful dp optimization from N^3 to N^2logN"?

yes , also check out editorial for ioi 2016's last problem , that has a pretty good explanation of this technique.

I have two questions about your code, thanks in advance if you can help clarify my doubts:

Thanks again. :)

Would you mind elaborating what a and b in dp[i][j][a][b] in problem E are? I don't get what you mean by this benefit.

I will update the blog with further description. For now, please look at 26284531 first.

If only I was allowed to see it :)

Then this: http://ideone.com/cwP55b

can u pls give me an small example when greedily selecting the position which gives the maximum correct at that instant is incorrect.

my solution which implements this in O(npk)

15 2 6

2 1 7

6 4 9 10 12 14 15

Answer is 6, but yours outputs 5.

Thanks a lot i had tried really hard to come up with examples where my greedy approach would fail but somehow i was blind to this obvious implication.

Where are the official solutions ?

They have been posted. Sorry for the delay.

My solution in C that considers two starting vertices : any vertex with maximal value and vertex that has the most neighbors with maximal value passes the tests, can anyone prove correctness of such solution?

Can someone please elaborate more on C?

I didn't get the part after each node's strength is incremented by 2.

Official Implementations are not available!

It says "You are not allowed."

I've posted them on ideone. Please refresh the page.

C alternate solution:

code

For "MAX appears only once" case it's enough to check whether there is MAX-1, which is not a neighbour of MAX.

yeah that's better

can someone please explains:

`Suppose this is not optimal, and k’ + c (c ≤ 0) roads can be shut down. The tree will break into k’ + c + 1 components`

in problem D?It is a typo, that must be

c≥ 0. This is just to prove that shutting downk' - 1 roads we got from performing BFS is optimal. (That is, you can't shut downk' roads.) You can imagine a tree. Once you cut one edge, there are two components. If you cut two edges, there are three components. So if you cutk' +cedges, there will bek' +c+ 1 components.Add tag to problem E. Now its empty. Interesting hard problem.

i used problem D with dfs...And got wa on 6....I don't know why? here is my code :26286839 please help me....

Last night,i also used dfs and got wrong answer. I found that my Dfs solution will give wrong answer on this test case.

Image of the test case

In my dfs visit order, i first vist the right police node and will not visit "visited-node" again. So when i start at the left police node, i can't visit the X-Blue node ==> wrong answer.

That is my case. Hope it help :D

I used dfs with cities containing police stations as starting vertexes while keeping track of distance of the current node from current police station during dfs. I removed a road if that road connects to the node whose distance is greater than d from the current police station. I also removed the road if that road connects with other police station because if from a city we can reach to more than one police stations in different cities with distance less or equal to d then there is no need for the extra road that leads to more police stations from that city.

But still getting the wrong answer on test #6. Any help would be appreciated....

My Submission

Thnx...

Ur method leaves some city isolated, and they don't connect any police station.So was mine.I have a simple example of such condition:With d equals 3, start from left, shut down 3 roads, finally leaves a city without police station isolated.

Picture

Thanks. Got it. But still can't figure out a way.... Will try.

For Problem D

"With this method, you can see that exactly k’ - 1 roads will be shut down"

Can anyone explain this statement

Imagine all cities being associated with their closest police station, and collapse this cluster of police station with it's associated cities into one node. This is always possible as the problem states that each city can go to at least one police station with distance <= d.

Now, we have k nodes in the graph each representing one cluster. Remember that originally, we had a tree. So even after collapsing, these k nodes are still connected to each other and form a tree.

Now we know that there exists exactly one path joining each of these k nodes, and we just have to delete these paths, as there is no need to join two clusters, since every city is already associated with one police station. And, for k nodes in a tree, we have exactly k-1 edges.

Why Problem C can be solved within O(N)? cannot understand the standard solution's algorithm.. Someone can help me?

i did it using dynamic programming,i used the fact that the answer can be max,max+1,max+2 If there is only 1 node having max value and the nodes containing value max — 1 are the neighbours of this node then the answer is max else it is max+1. If there are many max nodes are there then we need to find maximum distance between two max nodes , if maximum distance is > 2 then the answer will be max+2 else max+1. to find maximum distance between two max nodes i used the dp.and its running time is O(n).

"Why? It is because each bank’s strength can be increased at most twice, once by a neighboring bank, and once by a semi-neighboring bank".I don't understand why only once by a neighbor or semi neighbor since a node can have multiple neighbors .... can someone kindly explain what i am missing ? Thanks .

Because the given graph is a tree and a tree can not contain any cycle. And you can only hack the neighbors of a already hacked bank.(except first one). Hope it helps

Is problem C solvable using DFS?

Yeah

26292418

Can u explain it ?

One of my friends had a small problem while solving the C on the last contest. He used cin.sync_with_stdio(false); and cout.sync_with_stdio(false); and he got runtime error. We used this things and had no problem but this time got runtime error.. Is it a special rule for this ? Or where does this problem come from?

Where did editorial for problem C disappear? :)

It's back now. I reformatted the text.

DIV2-C: Got TLE in testcase 35. can anyone help me in analysing the time complexity of this submission Thank you in advance :)

Try using printf/scanf

Sorry! I don't understand why "y" is incremented here if(a[pos] == maxval) x--, y++;

Because they are neighboring, so its value is to be decreased by 1. From

maxval, it would becomemaxval- 1.xshould be decreased andyshould be increased.On the other hand, if

a[pos] =maxval- 1 you do not add anything, because it becomesmaxval- 2 but we keep track ofmaxvalandmaxval- 1 only.Just to add to it,

Notice that when a[pos] == maxval, it means that there exists a neighbor that is maxval.

In this case, we don't want y == 0. Because if that happens, then

If you look at the code above if a[pos] = maxval, y > 0, so that min(res, maxval); does not happen.

I think.

Here's my solution to

Cwhich doesn't need to iterate through all the banks to start with:What is a counterexample to the greedy solution of D? I don't understand why this doesn't work.

Consider this example

6 2 2

1 5

1 2

2 3

3 4

3 5

3 6

If you traverse greedily and delete parent of nodes greater than d distance or nodes with station on it,

After starting from node 1, you will end up deleting edges (3,4) (3,5) (3,6). But nodes 4 and 6 are reachable from node 5.

In your example, d = 2 or d = 4?

yeah, corrected that.

Yes I saw that this doesn't work, but assume that you always pass the minimum distance (-1 if none exists) to a police station in your subtrees to your parent when you moving up the dfs tree. Now you only cut edges if the distances has exceeded d. So in your case we would have cut (1,2) when coming back from 2 to 1. And we cut as well if we arrive at a police station moving back up and some police station is present in the subtree.

I have something wrong with problem D. My two submission (1 in-contest, 1 practice, but both are the same) got TLE on different test cases, although it provided the output.

I think all TLE solutions on Codeforces show output like this, but in fact it is the jury's answer.

Problem C can be solved with the segment tree. It's really faster than multiset and data structures like it. Maybe it a bit harder in implementation, but we get so far from TLE instead of multiset. Maybe, someone can suggest more easier way? ( I know about O(N) solution, but interesting in O(NlogN) with data structures ). Solution with segment tree ( 700 ms ) — http://codeforces.com/contest/796/submission/26302092 Solution with multiset ( 1800 ms ) — http://codeforces.com/contest/796/submission/26299257

As for Div2-C, I cannot quite understand how the maximum strength can be m + 2.

Number of Vertices : 7

Please refer to the example above. If we were to hack the banks in order of 2, 6, and 7, wouldn't bank #1 have strength + 3 ? From my understanding, the process is as follows;

Please correct my thoughts.

I am definitely an idiot to miss reading below problem statement;

Bank x is neighboring to some offline bank.

Thank you for the interesting problem zoomswk !

I used greedy approach for problem E. I got WA on #12, the input is big, so I can't check by hand, where my algorithm went wrong. Probably there is a mistake in my theorem about the algorithm's correctness. Can someone point out why is the approach wrong, or tell, I have only failed in implementation?

Algorithm: We store 3 boolean arrays, the answers first and second genius got right, and the answers we have already copied. We check the maximum copiable answer number (we can do this in O(n) by pushing a window of size k from 1-n), then we copy the answers which produces the maximum copiable answers. We do this p times, so every peek we get the maximum number of answers correct. Total runtime is O(np).

It looks like this: 10000111100001.

So, your algorithm will take the middle segment first, and will be unable to cover the two remaining questions. But the optimal answer takes 2 segments [1, 7] and [8, 14].

Oh, indeed, now I see. Thank you.

What is so special about testcase 13 in problem D? My code seems to do the same thing as you wrote :/ http://codeforces.com/contest/796/submission/26274912

notice that your code never goes into this segment:

`if (D[a] < d)`

because`0 < 0`

is false.Can someone explain state transitions for problem E in detail?

Problem C:

What is the differance b/w map and hashmap?

Also, from editorial "However, each operation involves the map data structure, so the overall runtime is O(nlogn)" How map access adds log(n)? Isn't map access is a constant time operation O(1)?

C++ map and Java TreeMap are usually implemented with a balanced tree (like Red-Black) with each operation being O(log n) while C++ unordered_map and Java HashMap use a hash table with amortized O(1) operations.

Thus n operations on a map/TreeMap take O(n log n) time but only O(n) amortized for unordered_map/HashMap.

I think

O(1) is just average, not amortized. Amortized complexity has to be guaranteed. In the worst case, unordered_map and HashMap work inO(n) per operation.Waw problem E solution is pretty dense

Hello, for problem D your algo gives wrong output for following input(at least my implementation did :( )

the output should be 1 2. But mine give 1 3. (thus leaving node 5 3 miles away!)

The problem is, If we traverse those cities with police stations as given in input city 3 is marked visited by city 1. And when city 4 comes to city 3 it finds it already visited and cut the road(This will leave city 5). How can i fix it?

If someone is interested in source code it can be found 26301147

You need to run BFS

simultaneouslyfrom all cities with a police station. See the provided code and ask if you don't understand.Thank you. got AC :)

You probably should have mentioned that in the editorial you know. :)

Thanks for the useful editorial.

In the Official Implementation of Problem C,

the // plus part (lines 44-51) could have been replaced with two lines:

In line (28): int x1 = x, y1 = y;

In line (43): x = x1, y = y1;

The following verifies the correctness of the observation:

http://codeforces.com/contest/796/submission/26337076

And the following is a C++ class implementation:

http://codeforces.com/contest/796/submission/26321000

Best Regards

Sure, that works too. Thanks for pointing out. I just copied-pasted from the minus part and changed the signs, so I didn't notice this. XD

So at first I didn't really understand why we need to clear out DP[next_layer][j][a][b] to a negative instead of just 0's at the end of each i iteration. Is this because DP[i][j][a][b] is not really "valid" for the case where i + a < k or i + b < k when j >= 1?

Mainly because it's already wrong for cases like

dp[layer][0][k- 1][k- 1], so the next questions can use the invalid glance. So I just set it to a huge negative value to be sure.In official implement of problem D.

can someone please explain why set

`v[pos] = 1`

hereinstead of set

`v[way[pos][i].first] = 1`

whenis met?

It's a matter of style. I'm just used to implementing this version of BFS. :)

But I get WA if I modify your code

Sorry for the late reply. It shutdowns all roads connected to cities with multiple police stations. You have to modify the algorithm to push to the queue only once per each city with police stations.

Problem E In your code, you didn't calculate dp[i][j][k-1][k-1].

Why is it right?

It's never better to start looking at both answer sheets at the same time. In the optimal strategy, if there is the answer on both answer sheets, you can just start looking at one of them.

Now I have deeper understanding of the state of DP. Thank you very much~

Could you tell something about how to find the definition of the state in DP in problem E? I find it rather difficult.

I can only recommend you to solve more DP problems. I found it hard to define states when I was new too, but things become better as I have more experience. :)

can someone explain problem E clearly ??

Which part don't you understand? DP formalization, time optimization, or memory optimization?

index a and b :/

As can be seen in my code, when you decide to start looking at some question, you'll have

a(orb) set tok- 1. That's because once you start looking, the nextk- 1 questions can be looked at at no cost (you pay only for the indexi). Hence,a(orb) means the number of the next consecutive questions that can be looked at for free, as the benefit of the recent glances.I am having some trouble trying to upsolve D. I don't know if it is my approach that is flawed or the code. My idea is start at a station then DFS to of a distance of 2*d+1. If I find a station while doing this I immediately backtrack and make a cut halfway between the two stations if there are an even number of nodes between them or closer to the original station if there are an odd number of nodes between them. Then I store the cut and stop exploring the part of the branch below the cut. I repeat until I either make k-1 cuts or have started a DFS from each station. My code http://codeforces.com/contest/796/submission/26412721 works until case 7 which is so big I can't figure out exactly where it goes wrong. If anyone could point out an error in the approach, code or generate a smaller test case that breaks it that would be appreciated.

10 3 3 1 6 7 1 2 2 3 3 4 4 5 4 10 10 9 9 8 8 7 10 6

City 5's nearest police station will be 4 kilos away with your algorithm.

Thanks that should help debugging.

Great example I now see why the approach can't work. The DFS can break a link between nodes that are far away before it breaks links between nodes that are closer.

In problom D. example 5; There are 300000 plice stations,but the answer is not 300000-1. I don't know why.Could you help me?

Some police stations are in the same cities. In the editorial,

k' is used to denote the number of cities that have at least one police station in them.in problem c, If there are 5 nodes,with strength 10,1,1,1,10 and edges are (1,2),(2,3),(3,4), (4,5). If we hack in sequence 1,5,2,3,4 The answer for the above case should be 10 but your code is giving 12.Please why it is wrong

Condition 2 is not satisfied when you hack bank 5.

thanks!

Couple things with the solution for Problem C that I am confused about.

Can someone now verify my arguments?

Yes.

I didn't think of it. You can create dummy variables if you want.

We assume that hacking each bank

ineeds strengtha_{i}+ 2, but this is not true for the bank you start with and its neighboring banks, so we have to update the strengths required to hack those banks required accordingly. Hacking semi-neighbors costa_{i}+ 2 anyway so we don't have to update them.In the solution for Problem E, is the switch between curr and prev a result of which 3-D table has the previous values? As in, do the previous values alternate between being stored in i = 0 and i = 1?

Yes and yes.

for problem D: the variable d is unuseful?

I'm not sure but alternative (but more complex) solutions seem to exist, taking variable

dinto account. However, it is useless for the described solution :DOh~Thanks (Useless..I am so weak in English.

"It is because each bank’s strength can be increased at most twice, once by a neighboring bank, and once by a semi-neighboring bank." I don't understand this. Each bank may have more than one neighboring bank, so the bank's strength could be increased multiple times, more than 2 times at least.

Nvm, got it. :P

Can anyone help me for the solution of E pls? I think it should have F[i][j] which represents for maximum correct answers she gets in first i questions with j times glacing. I know it's wrong, but I can't find why it's wrong.

This is my code. I have pre[i][j][3] array for pre-calculating in segment l-r, how much answers she can get (pre[i][j][0] for both copy from 2 geniuses, pre[i][j][1] for the only the first one, pre[i][j][2] for only the second one).

Pls help me!! Thanks so much

Try this case:

It looks like this:

The answer is 14 but yours gets 13. I had that wrong approach before!

Thanks so much for your help :P.

I solved E with O(n^2). At first my approach was O(npk) (not O(np(k^2)) ) but after I saw the nice trick used in the editorial to improve the order I reached O(n^2) approach. any way this is my code