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