您的位置:首页 > 其它

ZOJ14省赛3872——DP——Beauty of Array

2015-04-26 15:39 489 查看
140-The12thZhejiangProvincialCollegiateProgrammingContest-D
BeautyofArrayTimeLimit:2SecondsMemoryLimit:65536KB
EdwardhasanarrayAwithNintegers.Hedefinesthebeautyofanarrayasthesummationofalldistinctintegersinthearray.NowEdwardwantstoknowthesummationofthebeautyofallcontiguoussubarrayofthearrayA.

Input

Therearemultipletestcases.ThefirstlineofinputcontainsanintegerTindicatingthenumberoftestcases.Foreachtestcase:

ThefirstlinecontainsanintegerN(1<=N<=100000),whichindicatesthesizeofthearray.ThenextlinecontainsNpositiveintegersseparatedbyspaces.Everyintegerisnolargerthan1000000.

Output

Foreachcase,printtheanswerinoneline.

SampleInput

3
5
12345
3
233
4
2332

SampleOutput

105
21
38
大意:给你一组数,让你输出这组数中所有集合的和(如果集合中有两个或两个以上的数,那么只算一个)
dp指包括自己的集合总数
sum指结果


#include<cstdio>
#include<cstring>
#include<algorithm>
usingnamespacestd;
inta[100001];
intmain()
{
intn,m,T;
scanf("%d",&T);
while(T--){
longlongsum,dp;
scanf("%d",&n);
sum=dp=0;
memset(a,0,sizeof(a));
for(inti=1;i<=n;i++){
scanf("%d",&m);
dp=(i-a[m])*m+dp;
sum+=dp;
a[m]=i;//m前面所有的数都不可行
}
printf("%d\n",sum);
}
return0;
}


ViewCode


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