Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.
Output
For each case, print the answer in one line.
Sample Input
3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2
Sample Output
105
21
38
此题属于序列dp和LIS有异曲同工之妙。(看了题解才恍然大悟,唉)
下面给出AC代码
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
int pre[1000005];
LL dp[100005];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
LL sum = 0,num;
memset(pre,0,sizeof(pre));
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&num);
dp[i]=dp[i-1]+num*(i-pre[num]);///int * int = int 结果可能溢出 此题数据比较水 num 不改成long long 也能过
pre[num]=i;
}
for(int i=1;i<=n;i++) sum+=dp[i];
printf("%lld\n",sum);
}
}