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.

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.

Output

For each case, print the answer in one line.

Sample Input

  1. 3
  2. 5
  3. 1 2 3 4 5
  4. 3
  5. 2 3 3
  6. 4
  7. 2 3 3 2

Sample Output

  1. 105
  2. 21
  3. 38

题意:

定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和

题解:

按先后顺序将每个元素加入队列 然后计算

例:如果array【1,2,3】

1:    1              dp = 1;

2:    1 2

2          dp = 1+2*2;

3:    1 2 3

     2 3

     3    dp = 5+3*3;

一个不错的解释:

  1. 如果要求的是一段序列中连续子序列的个数,那么如果定义d[i]为以i结尾的连续子序列的个数,d[i]=d[i-1]+1;
  2. 我们定义d[i]为以i结尾的连续子序列的和,那么如果不重复d[i]=d[i-1]+a*i;
  3. ,如果重复的话,假设1 2 3 4 5 6 7。。。。。i,如果在第j位,那么(i i-1),(ii-2),(ii-3)。。。。(ij+1)这些连续子序列的值可以加上a的值;
  4. ij),(i,j-1),(i,j-2),(i,1),这些值都会包含重复的ij位置上的值,因为只需要算一次,所以不需要给这些以i
  5. 结尾的子序列加上a,这些子序列的个数,总共有j个,所以我们只需要用一个数组A标记上A[a]=i;那么d[i]=d[i-1]+a+(i-1-A[a])*a;
  6. 如果a之前没有出现过,那么A[a]等于0;如果a之前出现过,减去包含重复值的子序列的个数,也就是A[a]。

代码:

  1. #include <bits/stdc++.h>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <vector>
  8. #include <map>
  9. #include <set>
  10. #include <bitset>
  11. #include <queue>
  12. #include <deque>
  13. #include <stack>
  14. #include <iomanip>
  15. #include <cstdlib>
  16. #include <string>
  17. using namespace std;
  18. #define is_lower(c) (c>='a' && c<='z')
  19. #define is_upper(c) (c>='A' && c<='Z')
  20. #define is_alpha(c) (is_lower(c) || is_upper(c))
  21. #define is_digit(c) (c>='0' && c<='9')
  22. #define min(a,b) ((a)<(b)?(a):(b))
  23. #define max(a,b) ((a)>(b)?(a):(b))
  24. #define IO ios::sync_with_stdio(0);\
  25. cin.tie();\
  26. cout.tie();
  27. #define For(i,a,b) for(int i = a; i <= b; i++)
  28. typedef long long ll;
  29. typedef unsigned long long ull;
  30. typedef pair<int,int> pii;
  31. typedef pair<ll,ll> pll;
  32. typedef vector<int> vi;
  33. const ll inf=0x3f3f3f3f;
  34. ;
  35. const ll inf_ll=(ll)1e18;
  36. const ll maxn=100005LL;
  37. const ll mod=1000000007LL;
  38. +;
  39. int ans [N];int main()
  40. {
  41. IO
  42. int T;
  43. cin>>T;
  44. while(T--)
  45. {
  46. int n;
  47. memset(ans,,sizeof(ans));
  48. cin>>n;
  49. ;
  50. ll res = ,dp = ;
  51. For(i,,n){
  52. int x;
  53. cin>>x;
  54. dp += x*(i-ans[x]);
  55. res += dp;
  56. ans[x] = i;
  57. }
  58. cout<<res<<endl;
  59. }
  60. ;
  61. }

浙江省第十二届省赛 Beauty of Array(思维题)的更多相关文章

  1. 浙江省第十二届省赛 B - Team Formation

    Description For an upcoming programming contest, Edward, the headmaster of Marjar University, is for ...

  2. 湖南省第十二届省赛:Parenthesis

    Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions. The i-t ...

  3. 2015 浙江省赛 Beauty of Array (思维题)

    Beauty of Array Edward has an array A with N integers. He defines the beauty of an array as the summ ...

  4. Beauty of Array(思维)

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

  5. HZNU第十二届校赛赛后补题

    愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...

  6. 第十二届湖南省赛G - Parenthesis (树状数组维护)

    Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...

  7. 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题

    Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...

  8. 第十二届湖南省赛 A - 2016 ( 数学,同余转换)

    给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量:       1. 1≤a≤n,1≤b≤m;   2. a×b 是 2016 的倍数.   Input   输入包含不超过 30 ...

  9. Little Sub and Traveling(杭师大第十二届校赛E题) 欧拉回路

    题目传送门 题目大意: 从0出发,每次只能跳到(i*2)%n或者(i*2+1)%n,求字典序最大的哈密顿回路. 思路: 首先n为奇数时无解,先来证明这一点. 先假设n为奇数,若要回到原点,则必定有一步 ...

随机推荐

  1. 怎么获取textarea中选中文字

    textarea设置select="saveSelectionText()" //保存选中内容 saveSelectionText: function () { var focus ...

  2. javasisst & JAVA8

    今天在服务器上启动tomcat7的时候,提示如下异常: java.io.IOException: invalid constant type: 15 具体看是javasisst抛出来的. 系统运行环境 ...

  3. 【bzoj2809】[Apio2012]dispatching 贪心+可并堆

    题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...

  4. Educational Codeforces Round 53 Div. 2翻车记

    A:差点开场懵逼.只要有相邻两位不同就可以作为答案. #include<iostream> #include<cstdio> #include<cmath> #in ...

  5. NOIP临考经验【转】

    NOIP临考经验 1.提前15分钟入场,此时静坐调整心态,适当的深呼吸 2.打开编辑器并调整为自己喜欢的界面 3.熟悉文件目录,写好准确无误的代码模板 4.压缩包或许还不能解压,但是文件名已经可以知道 ...

  6. JDBC连接数据库的过程

    以连接MySQL为例: (1)加载MySQL数据库连接的驱动程序.到MySQL官网下载该驱动程序jar包,然后把包复制到WEB-INF/lib目录下,则JDBC会调用Class.forName()方法 ...

  7. hdu3068最长回文(Manacher算法)

    简单来说这是个很水的东西.有点dp的思想吧.推荐两个博客,很详细. http://blog.csdn.net/xingyeyongheng/article/details/9310555 http:/ ...

  8. [ZJOI2006]物流运输 DP 最短路

    ---题面--- 题解: 设f[i]表示到第i天的代价,cost[i][j]表示第i天到第j天采取同一种方案的最小代价.那么转移就很明显了,直接$n^2$枚举即可. 所以问题就变成了怎么获取cost数 ...

  9. 适用于iview的表格转Excel插件

    在网上找的一个表格转excel插件,经过修改后使其适用于iview中的table组件 let idTmr; const getExplorer = () => { let explorer = ...

  10. Vue根据URL传参来控制全局 console.log 的开关

    如果你的项目中console.log了很多信息,但是发到生产环境上又不想打印这些信息,这时候就需要设置一个全局变量,如:debug, 用正则匹配一下参数: const getQueryStr = (n ...