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. Complex social network Partition for Balanced Subnetworks---Hao Lan Zhang,Jiming Liu,Chunyu Feng,Chaoyi Pang,Tongliang Li,Jing He阅读

    摘要:Abstract—Complex social network analysis methods have been applied extensively in various domains ...

  2. php记录代码执行时间

    $t1 = microtime(true); // ... 执行代码 ... $t2 = microtime(true); echo '耗时'.round($t2-$t1,3).'秒'; 简单说一下. ...

  3. 批量反编译class文件

    首先得进入jad的路径中,一般都放在jdk的安装目录的bin中 进入到该目录中,否则无法使用jad命令. jad -o -r -d F:\src -s java F:\classes\**\*.cla ...

  4. java:file文件类

    public class FileDemo { public static File file; public static void main(String args[]) { String s = ...

  5. XSS 跨站攻击

    未整理完 介绍 XSS (Cross site Scripting),跨站脚本攻击,为了区分层叠样式表css,所以叫了XSS. 对于跨站脚本攻击,黑客界共识是:跨站脚本攻击是新型的"缓冲区溢 ...

  6. Mysql 高级部分

    MySQL 高级部分   (1)索引(index)..................................................................... 1 (2) ...

  7. (链接)xcode7,ios9 部分兼容设置

    xcode7,ios9 部分兼容设置 http://www.cnblogs.com/loganv/p/4647833.html

  8. vue项目中引入element-ui时,如何更改主题色

    在我们做项目时,我们经常会遇到切换主题色的功能,下面我们就来说一下通过颜色选择器我们就能改变项目的主题颜色 代码如下: 颜色选择器所在组件: top-theme.vue内容如下: <templa ...

  9. nginx中关于并发数的问题worker_connections,worker_processes

    我认为,要搞清楚这个公式是否正确,以及如何计算的,那首先要对nginx的各个配置说明有清晰的认识: 从用户的角度,http 1.1协议下,由于浏览器默认使用两个并发连接,因此计算方法: nginx作为 ...

  10. golang实现模拟键盘按键

    公司前段时间要我写个小项目需要可以局域网内一个ipad控制另一台pc上的键盘输入,github上找了找,居然有个robotgo库这么神级的存在,感觉go的库真是越来越多了,虽然大部分都是第三方的.ht ...