My first problem solution

Life for the first time to write explanations

Better reading experience, please click here

After analyzing simple questions, we found this problem is to ask how much of the original series in the sub-arithmetic series.

After the data range observed that, given the scope of the present subject maximum height. To know the amount given data range is likely to occur in the positive solution of the complex in the (time or space), so we try to rely on the two above. Because do not much problem, so I want to learn from several templates questions only done inside some ideas to solve this problem. This is the first article in this konjac solution to a problem, describes the thinking process of the front, the front part may be more long-winded, you may want to look directly at $ AC $ practices can see the back part.

Status Definitions

Since we can consider subject dp i is defined before the selection of the composition of elements in the sequence (the LCS $ $) or to the end of the sequence of i (the LIS $ $) a measure (such as the LCS $ $ length) of a character is $ f [i] $, of course, the subject may be due to constrained $ j $ (such as capacity backpack), so it becomes the definition of $ f [i] [j] $ state we want. In this problem, I first Reflection $ F [i] is $ before the i-th element selected number of columns can be composed of arithmetic, because it looks for $ i-1 <i $, $ f [i] $ composition when a part is the number of sub-series arithmetic $ f [i-1] $ , that is, before the i-th element must not vote for $ elements of $ i, the chances must contain the i-th element and how it is a result it? To calculate this, we may want to know the details of tolerance and the number of the end of the various sub-arithmetic $ f [i-1] $ inside the column, and to see whether the composition according to the arithmetic sequence where $ a [i] $ a, our before seeing the simple transfer of dp inconsistent temporarily abandoned this idea.

With just the experience we have found might define $ f [i] $ as at the end of paragraph i the number of arithmetic series is better, at least we know the end of the arithmetic sequence, known f $ J $ of case, you can find $ a [i] -a [j ] $, although this time did not know that arithmetic $ f [j] $ express tolerance when the number of columns Zeyang, but we progressed a little more than just a little.

We do not know the definition of $ f in the second state [j] $ tolerances in, we may also need something else. Suddenly thought dp general is a lot of space, and this state is defined only need to open an array of 1000, a little empty ah! Is something less? Recall the beginning to say, the title gives the maximum number is not more than 20,000, this amount is the beginning, although I pay attention, but the early thinking ignored. This stuff, or affect the time complexity, space complexity affect either! Given that we just can not dislodge $ f [j] $ inside the tolerance, then, it is not tolerance can be artificially set about it? Consider adding a layer of constraints defined $ F [i] [k] is $ end with i-th element, and the number of sub-arithmetic tolerance column number k . This time, if we know $ f [j] [k] (j <i) $, like seeking $ f [i] [k] $ , it is determined only $ a [i] -a [j ] and k $ equality just fine. So far, we have very heart of the spectrum!

First thought transfer equation

Speaking of the above, in the case of all the known J $ $ a $ f [j] [k] (j <i) $, it can find $ f [i] [k] $, probably seemed such as continuously summed $ f [i] [k] in a case where the tolerance is determined to meet the requirements of $, is expressed by the formula:

$f[i][k]=\sum_{j}f[j][k]$,其中$j<i$且$a[i]-a[j]=k$

This point of view, enumerate $ i, j, k $, there will be $ complexity of O (n ^ 2k) $, the timeout is for sure. However, it seems that you can re-optimization, after all, this is what I can think of is most likely thinking of a positive solution. Notes that, to meet the $ a [i] -a [j] == k $ can be summed, then a possible optimization is: We only enumerate $ k = a [i] -a [j] $ circumstances, That tolerance is now complete, j $ determined by $ i. Then transfer equation became the following circumstances:

$f[i][a[i]-a[j]]+=f[j][a[i]-a[j]]$,其中$j<i$

Noted that a [i] -a [j] is not a non-negative, so to add a number, such as 20,000, for example, the maximum height of the input data:

$ F [i] [a [i] -a [j] + maxheight] + = f [j] [a [i] -a [j] + maxheight] $, where $ 1 <= j <i $ (assuming first a numerical subscript 1)

With this, it is the core of solving the problem seems to have been interpreted out. (However, this equation is still wrong)

Transfer equation once again thinking and details of the deal

dp has a light transfer equation, and many times to write bad code is one of the reasons initialization and boundary treatment, and the other is a cyclic order. Here the author is limited, often circumvent this problem with the memory of the search, so no problem to discuss the order cycle, only to talk about the border deal with the problem, initiate, hope to give readers some inspiration.

Now "the transfer equation" in hand, let's take a few tentative count number, look right. For example, the sequence 1,2,3

$ F [1] [0] = 1 $, which is obvious, what seems to be a manual initialization sub-sub

$ F [1] [1] =? $, This is a bit muddled, but it feels it should be 0, first put off, the other $ f [1] [] $ 0 are considered it.

$ F [2] [0] = 1 $, or manually initialize? It seems a bit cumbersome hey.

$ F [2] [1] + = f [1] [1]? $ Quite right ah! $ F [1] [1] = 0 $, and our $ f [2] [1] $ 0 is calculated, but apparently according to the sample should be 1 ah! why? I started thinking, $ f [i] [a [i] -a [j] + maxheight] $ except that all $ f [j] [a [i] -a [j] + maxheight] $ and but The fact is not. Taking into account $ a [i] $ and $ a [j] $ tolerance in $ a [i] -a [j] $ case, which can form two arithmetic, its degradation (i.e. in case $ f [j] [a [i] -a [j] + maxheight] $) should be only $ a [j] $, and apparently only $ a [j] $ is not included in the case where $ f [j] [a [i] -a [j] + maxheight] $ in. So, we have to manually +1 to make up for only $ a [i] and a [j] $ composition of arithmetic progression. Therefore, the modified equation is:

$ F [i] [a [i] -a [j] + maxheight] + = (f [j] [a [i] -a [j] + maxheight] +1) $, where $ 1 <= j <i $ (assuming the first number of the subscript 1), + 1 to compensate for only $ a [i] and a [j] $ composition of arithmetic progression.

Since the boundary between two elements of a problem, a number that would not be wrong ah? If we are beginning to $ f $ all initialized to $ 0 $ words, and to avoid awry, $ I $ from $ 2 $ start to n cyclic (comparative particular), is indeed the discard of all single elements of arithmetic sequence of number , but let's initialize become simple. As compensation, after enumeration $ i, k $ all $ f [i] [k] $ sum, plus $ to $ n-element single arithmetic sequence ( also modulus ), is the answer. In this way, we manually simulate found the boundary problem, and then amended the transfer equation, and determine the initialization method, then write the code is particularly emboldened it!

Small optimization

Enumerating $ i, k $ all $ f [i] [k] $ summation, there will be $ O (nk) $ complexity of the program is short board. Since we only need to add the case of the composition of the arithmetic sequence of those possible tolerances, so no need to traverse all the tolerances, but only on the side dp edge operator answers dp time, complexity $ O (n ^ 2) $ For details, please look at the code.
code show as below:

#include <bits/stdc++.h>
#define ll long long
#define N 1009
#define V 20008
#define mod 998244353
using namespace std;
ll n,a[N],f[N][2*V],maxh=0,ans=0;
int main(){
    scanf("%lld",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        maxh=max(maxh,a[i]);
    }
    for(int i=2;i<=n;i++){
        for(int j=1;j<i;j++){
            f[i][a[i]-a[j]+maxh]=(f[i][a[i]-a[j]+maxh]+f[j][a[i]-a[j]+maxh]+1)%mod;
            //解释上式为何有+1:这个1指的是a[j]和a[i]这俩元素组成序列的情况,
            //在f[j][a[i]-a[j]]中仅有a[j]并不满足公差条件,所以要单独加上这个 
            ans=(ans+f[j][a[i]-a[j]+maxh]+1)%mod; 
            //我们不是用f[i][a[i]-a[j]+maxh]算的,而是直接加的f[j][a[i]-a[j]+maxh]+1
            //f数组仅用作dp,如果最后再算ans会慢 
        }
    }
    ans=(ans+n)%mod;
    printf("%lld\n",ans);
    return 0;
} 

Guess you like

Origin www.cnblogs.com/BUAA-Wander/p/12535968.html