This question is from amritapuri prelimnary for regional qualification...... Obviously the contest is over now..........
Given a sequence S of '0' and '1' of length N, a subsequence of S is obtained by omitting some elements of S without changing the order of the remaining elements of S.
Given a subarray of a sequence S starting from some position 'a' and ending at some position 'b' (1 <= a <= b <= N), both inclusive, denoted by S(a,b), you have report the length of longest non-decreasing subsequence of S(a,b). By non-decreasing, we mean that a '0' never appears after a '1' in the subsequence.
Input (STDIN): The first line contains T, the number of test cases. Then T test case follow, each test case being in the following format. The first line contains N, the length of sequence S. The next line contains a string of 0's and 1's of length exactly N. The next line contains Q, which means that you have to answer Q queries. The next Q lines contain a pair of integers a,b (1 <= a <= b <= N), meaning that you have to report the length of longest non decreasing subsequence of S(a,b). There is no blank line between test cases.
Output (STDOUT): Output Q lines per test case, which is the answer for each query. Do not print a blank line between test cases. Constraints:
1 <= T <= 10 1 <= N <= 100,000 1 <= Q <= 100,000 Sample Input:
1 13 0011101001110 6 1 13 4 13 1 9 6 9 3 3 6 10
Sample Output:
9 6 6 3 1 4
Plz tell me the algorithm..........
Can you give example or sample test ??
and what express the Q,T ?
sorry I'm too late , I come back after I become blue :)
it's interesting problem, let me explain my idea:
first thing let me define each "01" in text S as node, notice that the longest non-decreasing subsequence necessarily contains ONE node
let's select one of these nodes then notice that if you want to calculate length of longest non-decreasing subsequence that contain this node you do it by calculating (number of zeros on the left of this node + number of ones on the right of this node)
so let's search for all nodes in string S and donate node[i] is the index of the i'th node
and for each node you can calculate length of the longest non-decreasing subsequence that contain the node in O(1) time (considering whole string not only in range a,b)
i'll donate length[i] is the length of longest non-decreasing subsequence that contain the i'th node
now you have got the nodes with the length of each one
now when you answer a query just consider the nodes that being in the range
select the node that has the biggest length[i] this node is the node of longest non-decreasing subsequence in range (a,b) since all nodes will decrease its length[i] with the same number
note: to select the node that has the biggest length[i] effictively you need to use suitable data structer
this is my code (but I code it just to work with one test case you can easily edit it if you want):
UPD: commenting some debug lines
thanks...:)
You are welcome!
I hope that you can understand me because my English is bad and I can't express what I want very well
ur english is awesome.....
thanks,
i think this should work. correct me if i am wrong..