ZOJ3872:Beauty of Array(浙江省赛2015)

xiaoxiao2021-02-28  70

Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A. 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);     } }

第一次发表博客,就快大二了,现在还是个非常菜的菜鸟,如有说的不对的地方,请各位大神多多指教。
转载请注明原文地址: https://www.6miu.com/read-80176.html

最新回复(0)