For every contiguous block, subtract the maximum increasing-subsequence sum from the block sum, then report the best value and how many shortest blocks achieve it.
Hard8Dynamic programmingPrefix sumSegment treeGreedyNo attempts yetTime limit2sMemory limit512 MBYou are given a sequence A=A0,A1,…,AN−1 of N integers. Subsequences, increasing subsequences, and contiguous subsequences are defined as follows.
For A=[2,1,3], the subsequences are [],[2],[1],[3],[2,1],[2,3],[1,3],[2,1,3], the contiguous subsequences are [2],[1],[3],[2,1],[1,3],[2,1,3], and the increasing subsequences are [2],[1],[3],[2,3],[1,3].
Three functions are defined on a sequence.
The goodness g of the sequence is g=maxf(l,r) over 0≤l≤r<N. That is, g is the largest f(l,r) among all contiguous subsequences of A.
Let the integer m be the smallest length of a contiguous subsequence with f(l,r)=g.
Given A, find g first, then count the contiguous subsequences with r−l+1=m and f(l,r)=g.
The first line contains an integer N (1≤N≤200,000). The second line contains A0,A1,…,AN−1 separated by spaces. (−40≤Ai≤40)
Print two integers on one line, separated by a space. The first integer is g for the given sequence. The second integer is the number of contiguous subsequences with r−l+1=m and f(l,r)=g.