Beauty of Array

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.

<h4< dd="">Output

For each case, print the answer in one line.

<h4< dd="">Sample Input

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

<h4< dd="">Sample Output

105
21
38

题意:

给定一串数字,求所有连续的子序列的和,和为其中所有元素和(出现多次只算一次)。、

PS:

我们可以这样想:

因为要区别于不同的数

,可以看成序列里的数是一个一个加进去的,每次加入一个数,

统计前面序列里第一次出现新加入的这个数的位置;

这样每次加入的数的贡献度只会是这个数字之前出现的位置后面的数字!

//输入   1     2     3

//dp      1     5     14

//sum   1     6      20

//a[i]      1     2      3

详细点击

如果要求的是一段序列中连续子序列的个数,那么如果定义d[i]为以i结尾的连续子序列的个数,d[i]=d[i-1]+1;
我们定义d[i]为以i结尾的连续子序列的和,那么如果不重复d[i]=d[i-1]+a*i;
,如果重复的话,假设1 2 3 4 5 6 7。。。。。i,如果在第j位,那么(i i-1),(i,i-2),(i,i-3)。。。。(i,j+1)这些连续子序列的值可以加上a的值;
(i,j),(i,j-1),(i,j-2),(i,1),这些值都会包含重复的i,j位置上的值,因为只需要算一次,所以不需要给这些以i
结尾的子序列加上a,这些子序列的个数,总共有j个,所以我们只需要用一个数组A标记上A[a]=i;那么d[i]=d[i-1]+a+(i-1-A[a])*a;
如果a之前没有出现过,那么A[a]等于0;如果a之前出现过,减去包含重复值的子序列的个数,也就是A[a]。
#include<cstdio>
#include<string.h>
using namespace std;
#define ll long long
const int maxn=;
ll dp[maxn],d[maxn],sum[maxn];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
int n;scanf("%d",&n);
memset(d,,sizeof(d));
ll x;scanf("%lld",&x);
sum[]=dp[]=x;d[x]=;
for(ll i=;i<=n;i++)
{
scanf("%lld",&x);
if(d[x])
{
ll id=d[x];d[x]=i;
sum[i]=sum[i-]+(i-id)*x;
dp[i]=dp[i-]+sum[i];
}
else
{
d[x]=i;
sum[i]=sum[i-]+i*x;
dp[i]=dp[i-]+sum[i];
}
}
printf("%lld\n",dp[n]);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll pre[],a[];
int main()
{
ll t,i,j,n,ans,temp;
cin>>t;
while(t--) {
cin>>n;
ans=;
memset(pre,,sizeof(pre));
for(i=;i<=n;i++) {
cin>>a[i];
temp=(n+-i)*a[i];
ans+=temp;
ans+=(i-pre[a[i]]-)*temp;
pre[a[i]]=i;
}
cout<<ans<<endl;
}
return ;
}
#include <iostream>
using namespace std;
const int maxn=;
int n[maxn]; int main()
{
int t;
cin>>t;
while(t--)
{
fill(n,n+maxn,);
int m;
cin>>m;
long long ans=,dp=;
int t;
for(int i=;i<=m;i++)
{
cin>>t;
dp=(i-n[t])*t+dp;
ans+=dp;
n[t]=i;
}
cout<<ans<<endl;
}
retu

2015 浙江省赛 Beauty of Array (思维题)的更多相关文章

  1. Beauty of Array(思维)

    Beauty of Array Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has an array A with N integ ...

  2. 浙江省第十二届省赛 Beauty of Array(思维题)

    Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...

  3. 2019浙大校赛--G--Postman(简单思维题)

    一个思维水题 题目大意为,一个邮递员要投递N封信,一次从邮局来回只能投递K封.求最短的投递总距离.需注意,最后一次投递后无需返回邮局. 本题思路要点: 1.最后一次投递无需返回邮局,故最后一次投递所行 ...

  4. 2015 浙江省赛 H - May Day Holiday

    H - May Day Holiday As a university advocating self-learning and work-rest balance, Marjar Universit ...

  5. 2015 浙江省赛B Team Formation (技巧,动归)

    Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...

  6. CF949B A Leapfrog in the Array 思维题,推理

    题意: Dima是一名初级程序员. 在他的工作中,他经常不断地重复以下操作:从数组中删除每个第二个元素. 有一天,他对这个问题的解决方案感到厌倦,他提出了以下华丽的算法. 假设有一长度为2n的数组,最 ...

  7. 第十二届浙江省大学生程序设计大赛-Beauty of Array 分类: 比赛 2015-06-26 14:27 12人阅读 评论(0) 收藏

    Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...

  8. ZOJ 3879 Capture the Flag 15年浙江省赛K题

    每年省赛必有的一道模拟题,描述都是非常的长,题目都是蛮好写的... sigh... 比赛的时候没有写出这道题目 :( 题意:首先输入4个数,n,q,p,c代表有n个队伍,q个服务器,每支队伍的初始分数 ...

  9. 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(浙江省赛2015)

      Ace of Aces Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a mysterious organization c ...

随机推荐

  1. 吴恩达深度学习笔记(八) —— ResNets残差网络

    (很好的博客:残差网络ResNet笔记) 主要内容: 一.深层神经网络的优点和缺陷 二.残差网络的引入 三.残差网络的可行性 四.identity block 和 convolutional bloc ...

  2. multi update caused deadlock problem

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/dba45618-1d64-4661-891d-74cab09dddf1/deadlo ...

  3. java 反射机制复习笔记。

    1.java 反射的作用(好处): (1)能够使我们很方便的创建灵活的代码,这些代码可以在运行时装配(运行时获取一个类实例),就不用在组件之间进行源代码链接了,大大提高系统的灵活性和扩展性. (2)与 ...

  4. mysql基础(4)-数据导入

    如何把数据导入(出)mysql 导出 sql语句         select * from 表名 into outfile "详细路径" fields terminated by ...

  5. UOJ180 【UR #12】实验室外的攻防战

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. Codeforces Round #370 (Div. 2) A , B , C 水,水,贪心

    A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Angular表单的本地校验和远程校验

    AngularJS Form 进阶:远程校验和自定义输入项 表单远程校验 HTML代码: <!doctype html> <html ng-app="form-exampl ...

  8. js代理模式

    代理模式的关键是,当客户不方便直接访问一个对象或者不满足需要的时候,提供一个替身对象来控制对这个对象的访问,客户实际上访问的是替身对象.替身对象对请求做出一些处理之后,再把请求转交给本体对象. 代理模 ...

  9. HYSBZ - 2038经典莫队算法题

    无修改的莫队 emmm莫队的几条性质,必须是离线的,复杂度是n*sqrt(n) 就是通过预处理查询区间,然后从(l,r)转移到(ll,rr),这样的复杂度是曼哈顿距离,即abs(l-ll)+abs(r ...

  10. oralce 索引(2)

    B-Tree 索引 本文来自网上整理 来自以下博客内容 http://www.360doc.com/content/13/0712/11/13136648_299364992.shtml; http: ...