If *n* is even, then the answer is *n* / 2, otherwise the answer is (*n* - 1) / 2 - *n* = - (*n* + 1) / 2.

Hint of this problem is presented in its statement. " where is equal to 1 if some *a*_{i} = 1, otherwise it is equal to 0."

To solve this problem, do 3 following steps:

- Assign all
*a*_{ij}(1 ≤*i*≤*m*, 1 ≤*j*≤*n*) equals to 1. - If some
*b*_{ij}= 0, then do assignments:*a*_{ik}=*a*_{tj}= 0 (1 ≤*k*≤*n*, 1 ≤*t*≤*m*) (that means, assign all elements in row*i*and column*j*of matrix*a*to 0). - Then we have matrix
*a*which need to find. Just check whether from matrix*a*, can we produce matrix*b*. If not, the answer is obviously "NO".

Complexity: We can implement this algorithm in *O*(*m* * *n*), but it's not neccesary since 1 ≤ *m*, *n* ≤ 100.

486C - Palindrome Transformation

Assuming that cursor's position is in the first half of string(*i*.*e* 1 ≤ *p* ≤ *n* / 2) (if it's not, just reverse the string, and change *p* to *n* - *p* + 1, then the answer will not change).

It is not hard to see that, in optimal solution, we only need to move our cusor in the first half of the string only, and the number of "turn" is at most once.

Therefore, we have below algorithm:

Find the largest index

*r*before*p*in the first half of the string (*p*≤*r*≤*n*/ 2) such that*s*_{r}different to*s*_{n - r + 1}. (*s*_{i}denote*i*_{th}character of our input string*s*)Find the smallest index

*l*after*p*(1 ≤*l*≤*p*) such that*s*_{l}different to*s*_{n - l + 1}.In optimal solution, we move our cusor from

*p*to*l*and then from*l*to*r*, or move from*p*to*r*and then from*r*to*l*. While moving, we also change character of string simultaneously (if needed) (by press up/down arrow keys).

Be careful with some corner case(for example, does'nt exist such *l* or *r* discribed above).

Complexity: *O*(*n*).

Firstly, we solve the case

*d*= + ∞. In this case, we can forget all*a*_{i}since they doesn't play a role anymore. Consider the tree is rooted at node 1. Let*F*_{i}be the number of valid sets contain node*i*and several other nodes in subtree of*i*("several" here means 0 or more). We can easily calculate*F*_{i}through*F*_{j}where*j*is directed child node of*i*: . Complexity:*O*(*n*).General case:

*d*≥ 0. For each node*i*, we count the number of valid sets contain node*i*and some other node*j*such that*a*_{i}≤*a*_{j}≤*a*_{i}+*d*(that means, node*i*have the smallest value*a*in the set). How? Start DFS from node*i*, visit only nodes*j*such that*a*_{i}≤*a*_{j}≤*a*_{i}+*d*. Then all nodes have visited form another tree. Just apply case*d*= + ∞ for this new tree. We have to count*n*times, each time take*O*(*n*), so the overall complexity is*O*(*n*^{2}). (Be craeful with duplicate counting)

Here is my code.

LIS = Longest increasing subsequence.

#### Solution 1 (Most of participant's solutions):

- Let
*F*1_{i}be the length of LIS ending exactly at*a*_{i}of sequence {*a*_{1},*a*_{2}, ...,*a*_{i}} and*D*1_{i}is the number of such that LIS.

- Let
*F*2_{i}be the length of LIS beginning exactly at*a*_{i}of sequence {*a*_{i},*a*_{i + 1}, ...,*a*_{n}} and*D*2_{i}is the number of such that LIS.

// Calculates

*F*1_{i}and*F*2_{i}is familiar task, so I will not dig into this. For those who have'nt known it yet, this link may be useful)// We can calculate

*D*1_{i}and*D*2_{i}by using advanced data structure, like BIT or Segment tree.*l*= length of LIS of {*a*_{1},*a*_{2}, ...,*a*_{n}} =*max*{*F*1_{i}} =*max*{*F*2_{j}}.*m*= number of LIS of {*a*_{1},*a*_{2}, ...,*a*_{n}} =Let

*C*_{i}be the number of LIS of {*a*_{1},*a*_{2}, ...,*a*_{n}} that*a*_{i}belongs to. Index*i*must in group:1) if

*C*_{i}= 02) if 0 ≤

*C*_{i}<*m*3) if

*C*_{i}=*m*How to calculate

*C*_{i}? If (*F*1_{i}+*F*2_{i}- 1 <*l*) then*C*_{i}= 0, else*C*_{i}=*D*1_{i}**D*2_{i}. Done!

We have an additional issue. The number of LIS of {*a*_{1}, *a*_{2}, ..., *a*_{n}} maybe very large! *D*1_{i}, *D*2_{i} and *m* maybe exceed 64-bits integer. Hence, we need to store *D*1_{i}, *D*2_{i} and *m* after modulo some integer number, call it *p*.

Usually, we will choose *p* is a prime, like 10^{9} + 7 or 10^{9} + 9. It's not hard to generate a test such that if you choose *p* = 10^{9} + 7 or *p* = 10^{9} + 9, your solution will lead to Wrong answer. But I have romeved such that tests, because the data tests is weak, even *p* is not a prime can pass all tests.

#### Solution 2:

// Some notation is re-defined.

Let

*F*1_{i}be the length of LIS ending exactly at*a*_{i}of sequence {*a*_{1},*a*_{2}, ...,*a*_{i}}.Let

*F*2_{i}be the length of LIS beginning exactly at*a*_{i}of sequence {*a*_{i},*a*_{i + 1}, ...,*a*_{n}}.*l*= length of LIS of {*a*_{1},*a*_{2}, ...,*a*_{n}} =*max*{*F*1_{i}} =*max*{*F*2_{j}}.Let

*F*_{i}be the length of LIS of sequence {*a*_{1},*a*_{2}, ...,*a*_{i - 1},*a*_{i + 1}, ...,*a*_{n}} (i.e the length of LIS of initial sequence*a*after removing element*a*_{i}).Index

*i*must in group:1) if

*F*1_{i}+*F*2_{i}- 1 <*l*, otherwise:2) if

*F*_{i}=*l*3) if

*F*_{i}=*l*- 1How to caculate

*F*_{i}? We have:*F*_{i}=*max*{*F*1_{u}+*F*2_{v}} among 1 ≤*u*<*i*<*v*≤*n*such that*a*_{u}<*a*_{v}. From this formula, we can use Segment tree to calculate*F*_{i}. Due to limitation of my English, it is really hard to write exactly how. I will post my code soon.

Complexity of both above solution: *O*(*nlogn*).

Really quick editorial! Thank you. The problems are very interesting :)

Are you talking about quick sort ? i think merge sort is faster .

/* */

it seems to be easy to prepare such a quick editorial for that easy problems.

Yeah.It was so easy, so you solved 2 problems? Do not comment other's work if you are not able to do something like this.I can bet you won't host a codeforces round in your miserable life.

There're too many unrated contestant in the top, I think they come from Div1, that make my rank low :(

As you say; they were UNRATED.

do you mean topological sort by "top" ?

I'm trying my code about problem C(failed to submit it on time to be rated for it though) on cofeforces.com but the result my file produces on the website and the result it produces on my computer is VERY different, while on my computer it shows the right result(trying the example at the problem page, and the result is 6), but the same code at codeforces.com gives me a huge, riddiculous number which is 272695054. I don't know there else to look at(I'm new here) can you help me? Submission:8662047 GCC compiler version: Ubuntu 4.8.2-19ubuntu1

I found that the problem was about the scanning, fixed this and other error and now it runs flawlessly, took me more than 3 hours to do this though(runtime errors everywhere), I wonder how can you guys be so fast...

Here is one more solution for E. It looks like your first solution but is easier and deterministic.

First, calculate

F1_{i}andF2_{i}for all i. Obviously, ifF1_{i}+F2_{i}- 1 ≠lthan answer foriis 1, otherwise it is 2 or 3.Then, assume there are two elements

iandjwith answer different from 1 such thatF1_{i}=F1_{j}. You can prove that in this case you can replace i-th element with j-th one in any LIS containing i-th element. Thus the answer foriis 2 (and forjit is 2 too, of course). It can also be shown that ifF1_{i}is unique among allF1 then the answer foriis 3.So the algorithm is the following:

F1_{i}+F2_{i}- 1 ≠lthen the answer foriis 1F1_{i}is unique (among positions where we didn't assign 1 yet) then the answer foriis 3iis 2What about this case:

a= {1, 3, 4, 2} =>F_{1}= {1, 2, 3, 2}. We haveF_{2}=F_{4}(this is 1-based indexing), but in fact, the answer for index 2 is 3, the answer for index 4 is 2.If

F1_{i}+F2_{i}- 1 ≠lthen we ignore this number in latter computations.And in your example the answer for index 4 is 1 because the only LIS is {1, 3, 4}.

Oh, I get it! What a great and simple solution! Thanks!

do you mean "computational geometry" with "latter computations" ?

Do you mean "a stupid joke" by posting stupid jokes?

Such an elegant solution. :)

To check for 3-rd type we can construct first and last (lexicographically) sequences of maximum length and elements in intersection will have 3-rd type. They easy built from F1 and F2.

You have written "you can replace i-th element with j-th one in any LIS containing i-th element" but in case of [4, 5, 2, 3, 8] here f1[2] = f1[4] = f2[2] = f2[4] = 2 and f1[2]+f2[2]-1==3 but here we cant replace 5 with 3 in LIS [4 5 8] and neither 3 with 5 in [2 3 8]

Thanks!!! Amazing solution. Very simple and fast.

I didn't guess why we always can replace i-th element with j-th, but still I have an idea to show that if F1i = F1j then answer for i and j is 2. Let me write it here: suppose answer for i and j is 3, this means that they both participate in every LIS, now suppose ai < aj then this would mean that F1j = F1i + (# elements between them + 1) so these two values can't be equal. For ai > aj there is the same logic. I think I didn't got anything wrong here.

Thanku so much sir

waiting for D solution. :D

My solution for problem D. http://pastie.org/9712317

Was your solution successful? I did the same stuff, it works fine on the samples, but fails on 5th test. Any ideas why could that be?

is it possible to do string matching with graphs?

2 things about problem B:

1) assign all elements in row i and column j of matrix a to 0, not to 1 (typo);

2) what is the n*m solution?

Despite this, thank you for a very interesting round:)

1) Thanks! Fixed!

2) We can use additional array. For example:

R_{i}= 0 means that we assign all elements of rowiof matrixato 0, otherwiseR_{i}= 1;C_{j}= 0 means that we assign all elements of columnjof matrixato 0, otherwiseC_{j}= 1. Thenb_{ij}=R_{i}ORC_{j}.We can assign all elements in matrix A initially to 1.Then keep substituting the rows/columns of A where the same row/column in B has atleast 1 zero.This will create the A matrix and then check if it can form A. P.S.: I would also like to know the O(n*m) solution.

2) n*m solution can be done by simply keeping markers on which rows/columns you must mark with zeroes and then keeping track of which rows/columns are fully zeroed. However

O(n*m*(n+m))still works fine.do you mean "matrix exponantiation" with matrix ? if yes , press 8.

Here's my deterministic and simple solution for E.

F1i + F2i -1 =/= l===> the answer foriis 1Now how to make difference between 2 and 3? Well if some i is of type 2, it means that removing it will leave some existing subsequence of length

l. Let's find the elements in that optimal subsequence (after removal of Ai) thatAilies between. Suppose those two areAkandAp, k<i<p.If Ak<Ai<Ap then we could expand it, therefore this is false. So there is either Ak>=Ai or Ap<=Ai.

From this the following follows to be true:

If there is some Ak such that k<i, Ak>=Ai and

(F1k+F2k-1)=l, then i is of type 2. Similarly, if there is some Ap such that p>i, Ap<=Ai and(F1p+F2p-1)=l, then i is of type 2.Both can be checked in O(NlogN) with segment tree.

Nice!

for type 2 or 3 checking.

Lets ignore all those "nodes" that never belong to the LIS. Use a map "ma" for counting the number of different nodes i that have F1i. that is:

for(i = 0; i < n; i++){ if(res[i] == '1') continue; ma[f1[i]]++; }

then, for every i check if ma[f1[i]] == 1. If it is true, every LIS in the array will have that node in the sequence at that level. Sorry about my english.

Very nice solution ! :D Also note that all the processes described by you can be done just using binary indexed trees. ( http://codeforces.com/contest/486/submission/8697188 )

Anything done with BIT can be done with segment tree...8676667

But not everything done using segment tree can be done using BIT :P

Editorials should have contain bit more details. Overall...thank you. :)

do you mean binary indexed tree with "bit" ?

here is a solution for E without segment tree or BIT: 8657105

Hi,

Could you please explain how your solution works?

I am unable to completely understand it but it looks nice.

Thanks.

You can look at Exercise 15-4.6 in Cormen "Introduction to Algorithms" book.

Thanks, but I I finally understood the solution. :P

I did the same solution. I recommend use std::lower_bound\std::upper_bound to do a binary search over a sorted range. 8763721

Could you explain the main idea of the solution D, please?

For problem D I'm using the following logic :- I'm applying separate DFS on all the nodes, With the discovery of every node ( except the ones on which DFS had already been applied ) on whose addition the difference between minimum and maximum does not exceed 'D' , I'm adding 1 to the answer. Can anyone tell me why this logic wont work? Code :- 8665299

First, your min/max values change on different branches of the tree when you DFS. Second, you only count the number of nodes, while the statement asks for the number of sets.

Why do i have to find the values for d=inf ? i don't understand that . This is my submission , but im still unable to handle all the cases.8709569

Why Mr.Vuong makes this contest ?

Link to the code for problem D doesnt work

Thanks! Fixed.

D — Valid Sets "Firstly, we solve the case d = 0. " It should be "d=inf"

Thanks! You are right.

I have another solution for E:

Let f[i] be the length of a LIS ending at a[i], and g[i] be the length of a LIS beginning at a[i], It's easy to see that L[i] = f[i] + g[i] — 1 is the length of a LIS containing a[i].

The length of LIS of A should be m = max{L[i]},

1/ An index i is belong to the 1st class iff L[i] < m

2/ An index i is belong to 2nd class iff L[i] = m and there exists ANOTHER index j so that (L[j] = m) and (f[j] = f[i]). Because a[i] and a[j] can never belong to the same LIS of A.

3/ The remaining index(es) will be assigned into 3rd class.

The computation of f[.] and g[.] takes O(n log n) time, and the classification takes O(n) times using counting technique

8dm651

can any anyone explain how is the following relation F(i)=prod(F(j)+1) true? where F(i) be the number of valid sets contain node i as root. F(j) is a node in subtree of i.

If we have only one child with

`F(j)=x`

for the root node, in how many ways can we make a tree containing root.(this is same as`F(i)`

). Surely`x+1`

as we can select child in x ways and not select in 1 way.When children increases we simply multiply

`F(j)+1`

for every child we have.How to calculate F(j) for child? (Just do it recursively.) As we reach leaf node its

`F(i) will be 1`

.At each child j, you have the option to select

`(0,1,2....F(j))`

valid sets from subtree rooted at j. Hence`F(i) = prod(F(j)+1)`

It's the same as the proof of finding no of divisors of any number using prime factorization where,

if n = a^x * b^y * c^z, no of divisors = (x+1)(y+1)(z+1). Proof

Try to relate this to the no of divisors problem, you will find your answer :)

great explanation !!! thanks..

Can you explain how we can avoid duplicate counting? Thanks.

My solution for problem E. :) http://codeforces.com/contest/486/submission/8692548

In problem E. S[i] is "length of LIS start exactly at i", E[i] is "length of LIS end exactly at i" If (S[i] + E[i] — 1 == l). A[i] belong to a LIS. Otherwise index i must in Group 1. I put A[i] in to n layer (L), n = max{S[i]} = max{E[i]}, without any member of Group 1. A[x] belong to L[i] if (S[x] == i) (or E[x] == i, anyway) If L[i] include 1 element, index of this element belong to Group 3, because we can't make a LIS without it. Otherwise, index of these element belong to Group 2.

I haven't code it yet but I think my solution more simple them 2 solution of

laoriu, but if anything wrong, tell me pls :).I cant understand how to caculate Fi using segment tree? Can anyone tell me ?

I don't see any segment tree used in calculating Fi. You are referring to

`D-Valid Sets`

, right?We just used

`DFS`

to do our work.Can you give link to the solution which used it?i forgot it. segment tree is in E solution. in solution 2, laoriu says: "we can use Segment tree to calculate Fi" but how ?

Can you please tell me how one can think of solution to problem OR matrix in this(tutorial) way? I mean to say is there any specific algo related to it or its pure thinking skill.

Just pure thinking. The insight of this problem is the property of bitwise OR.

problem C is giving correct output(i.e. 6) for first test on my system,but codeforces is showing output '0' for same test.Plz help. http://codeforces.com/contest/486/submission/11666147

http://ideone.com/ahdnVP

this is my code for solution 2 :)

Can someone tell me how to solve problem E using the way mentioned in the editorial? I have already solved after reading ifsmirnov comment but I want to solve the problem with the way mentioned in the editorial because I am weak at segment trees , can someone explain or link their solution? thanks!

486-A

sum=(((n*(n+1))/2)-(x*(2*a+((x-1)*d)))/2);why this case failed?? Input 3037000499 Participant's output 9223372035336275558 Jury's answer -1518500250what do you mean with a ?

Editorial to problem D is love.(^_^)

can anyone prove me the answer of problem A ?

can anyone please explain the equation of problem D? especially why we have add one to all f[v] before multiplying with f[u]?