ZOJ - 3872 Beauty of Array (技巧&模拟)好题

来源:互联网 发布:商城数据库建表 编辑:程序博客网 时间:2024/06/06 01:27
ZOJ - 3872
Beauty of Array
Time Limit:                                                        2000MS                        Memory Limit: 65536KB 64bit IO Format:                            %lld & %llu                       

SubmitStatus

Description

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 arrayA.

<h4< body="">

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 containsN positive integers separated by spaces. Every integer is no larger than 1000000.

<h4< body="">

Output

For each case, print the answer in one line.

<h4< body="">

Sample Input

351 2 3 4 532 3 342 3 3 2
<h4< body="">

Sample Output

1052138

Hint

Source

The 12th Zhejiang Provincial Collegiate Programming Contest
//题意:一个序列的漂亮值的定义为:这个序列中所有不重复的数的和。
给你一个含有n个数的序列,让你找出这个序列的所有连续子序列的漂亮值的和。
//思路:
是一道挺好的题,比赛是队友花了好长时间终于把它A了。
因为n的值很大,所以不可能用两重for循环(肯定会TML),所以就想着用一重for实现。
用sum[i]数组存放前i个数的所有可能的组合的情况的和,用pre[x]数组标记x最后一次出现的位置,通过它来计算x会被累加几次,有了这个思路就可以了。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstring>#include<algorithm>#include<set>#include<vector>using namespace std;long long sum[100000+100],pre[100000+100];int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);memset(sum,0,sizeof(sum));memset(pre,0,sizeof(pre));long long ans=0;for(long long i=1;i<=n;i++){long long x;scanf("%lld",&x);sum[i]=sum[i-1]+(i-pre[x])*x;pre[x]=i;ans+=sum[i];}printf("%lld\n",ans);}return 0;}

0 0
原创粉丝点击