Ideas for reference from: http://blog.csdn.net/doris1104/article/details/51126910 thanks
Find out the pattern of the last ball adjacent to the front ball (to satisfy the left greater than the right) and 2^ two ball spacing related
-----> The closer the number of neighbors.
1#include <cstdio>2#include <cstring>3#include <cstdlib>4 5#include <string>6#include <algorithm>7#include <Set>8#include <map>9#include <vector>Ten#include <stack> One#include <queue> A#include <cmath> - -#include <iostream> the using namespacestd; - -typedefLong Longll; - Const intMoD =1000000007; +ll ans[100005],two[100005],num[100005],sum[100005]; - + /* A Num[a]: Used to save the number of times a needs to be subtracted at num[tmp] = (Num[tmp] + two[i-1])% MoD; the closer the number of neighbors, the more you subtract if the two numbers are the same - - *sum[i] = (Sum[i-1] + two[i])% MoD; - Situation and - - Ans[i] = (2*ans[i-1] + sum[i-2]-num[tmp] + MoD)% MoD; in The previous situation and the addition of this - Both ends can be inserted to Previous conditions + */ - the voidinit () { *two[0] = two[1] =1; $ for(inti =2; I <100005; i + +){Panax NotoginsengTwo[i] = (2*two[i-1])%MoD; - } thesum[0] =1; + for(inti =1; I <100005; i + +){ ASum[i] = (sum[i-1] + two[i])%MoD; the } + - } $ $ intMain () { - - intT; the init (); -scanf"%d",&T);Wuyi while(t--){ the intN; -scanf"%d",&n); Wumemset (ans,0,sizeof(ans)); -memset (NUM,0,sizeof(num)); About for(inti =1, TMP; I <= N; i + +){ $scanf"%d",&tmp); -Ans[i] = (2*ans[i-1] + sum[i-2]-num[tmp] + MoD)%MoD; -NUM[TMP] = (Num[tmp] + two[i-1]) %MoD; - } Aprintf"%lld\n", (ans[n]*2)%MoD); + } the - return 0; $}
Zoj 3929 Deque and Balls (recursive to think)