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. SpringBoot 入门笔记

    1. Spring 4.3中引入了: @GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping 2. @RequestMapp ...

  2. Kubernetes Kube-proxy

    Kube-proxy主要负责Service的实现. kube-proxy enables the Kubernetes service abstraction by maintaining netwo ...

  3. 安装配置Apollo-Prota web中心平台

    首先要求2g以上内存哈,JDK1.8 搭建数据库 创建表 apollo-build-scripts-master 整个阿波罗环境包 使用一个命令启动整个阿波罗服务环境 创建两个数据库 分别为:apol ...

  4. Start and Use the Database Engine Tuning Advisor

    https://docs.microsoft.com/en-us/sql/relational-databases/performance/start-and-use-the-database-eng ...

  5. Nginad广告生成代码分析

    大家都知道实时竞价的广告一般会在一个iframe中,这个iframe会有一个复杂的src.那么这个iframe是如何生成的? 这里分析NginAd作为exchange时,如何让媒体网站通过引用一段ad ...

  6. hadoop 伪分布模式环境搭建

    一 安装JDK 下载JDK      jdk-8u112-linux-i586.tar.gz 解压JDK     hadoop@ubuntu:/soft$ tar -zxvf jdk-8u112-li ...

  7. IPC 进程间通信

    linux下进程间通信的几种主要手段简介: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它 ...

  8. CEF3.2623使用记录:windows编译

    CEF3.2623使用记录:windows编译 1:cef3.2623下载地址 2623是cef3最后一个支持xp系统的版本,且可以支持html的audio标签,可以用作对html音频的处理下载地址为 ...

  9. js字符串和数组的相互转化

    一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); 二 ...

  10. SARG

    SARG (Searchable Arguments)操作,因为它通常是指一个特定的匹配,一个值得范围内的匹配或者两个以上条件的AND连接.   中文名 SARG 全    称 Searchable ...