您的位置:首页 > 其它

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

2016-05-01 15:46 281 查看
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 array
A.

<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 contains
N 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

3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2

<h4< body="">

Sample Output

105
21
38


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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: