ZOJ 3872 Beauty of Array【无重复连续子序列的贡献和/规律/DP】
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
3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2
Sample Output
105
21
38
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define mod(x) ((x)%M)
#define pi acos(-1.0)
#define rep(i,x,n) for(int i=(x); i<(n); i++)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6+10;
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[2]={-1,1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
LL mapp[1100000],ans,sum;
int a[110000];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
sum=0;
memset(mapp,-1,sizeof(mapp));
for(int i=0; i<n; i++)
{
scanf("%d",a+i);
if(i==0)ans=a[i];
else ans+=(i-mapp[a[i]])*a[i];
mapp[a[i]]=i;
sum += ans;
}
printf("%lld\n",sum);
}
return 0;
}
/*
【题意】
我们定义一个序列的贡献是它里面所有不相同的数字之和,然后给出一个数字序列,求其所有连续子序列的贡献和。
【类型】计数DP
【分析】
假设当前给出序列为: 2 3 3 2
以第一个 2 结尾的子串有一个,当前贡献为 1*2=2
以第一个 3 结尾的子串有两个,当前贡献为 2+2*3=8
以第二个 3 结尾的子串有三个,因为 3 已经在第二个位置出现过,因此对于包含前一个 3 的所有子串的贡献都已经计算过了,当前贡献为 8+(3-2)*3=11
以第二个 2 结尾的子串有四个,因为 2 已经在第一个位置出现过,因此对于包含前一个 2 的所有子串的贡献也计算过了,当前贡献为 11+(4-1)*2=17
最终的结果是所有计算的数值之和。
【时间复杂度&&优化】
【trick】
【数据】
*/
ZOJ 3872 Beauty of Array【无重复连续子序列的贡献和/规律/DP】的更多相关文章
- ZOJ 3872 Beauty of Array
/** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...
- DP ZOJ 3872 Beauty of Array
题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...
- ZOJ 3872 Beauty of Array 连续子序列求和
Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...
- ZOJ 3872 Beauty of Array DP 15年浙江省赛D题
也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...
- ZOJ 3872 Beauty of Array (The 12th Zhejiang Provincial Collegiate Programming Contest )
对于没有题目积累和clever mind的我来说,想解这道题还是非常困难的,也根本没有想到用dp. from: http://blog.csdn.net/u013050857/article/deta ...
- Zoj 3842 Beauty of Array
Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the be ...
- lintcode-397-最长上升连续子序列
397-最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 注意事 ...
- 动态规划(Dynamic Programming, DP)---- 最大连续子序列和
动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想,简单来使,动态规划是将一个复杂的问题分解成若干个子问题,或者说若干个阶段,下一个阶段通过上一个阶段的结 ...
- ACM-DP之最大连续子序列——hdu1231
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
随机推荐
- 应用Hash函数
本文系转载,转自:http://www.blogjava.net/jinfei0627/articles/219543.html 应用Hash函数(java描述) 作者:冲处宇宙 时间:2007.1. ...
- Java注释@interface的用法【转】 --好文章 很好理解
java用 @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类. @Override,@Deprecated,@SuppressWarnings ...
- mysql无法通过ip地址链接
用BitNami搭建个站点(内含mysql数据库服务.phpmyadmin和Apache web Service), 用localhost或127.0.0.1及用户名密码连接没有问题.但是本机或远程通 ...
- CODE FESTIVAL 2017 qual B C - 3 Steps
Score : 500 points Problem Statement Rng has a connected undirected graph with N vertices. Currently ...
- PHP is_null,empty以及isset,unset的区别
1.empty 判断一个变量是否为“空”.null.false.00.0.’0′.』.为以上值的变量在检测時都将返回true. 2.isset 判断一个变量是否已经设置.0.00.’0′.』.’ ‘. ...
- WinRAR分割超大文件
在自己的硬盘上有一个比较大的文件,想把它从网上通过E-Mail发送给朋友时,却发现对方的收信服务器不能够支持那么大的文件……,这时即使用ZIP等压缩软件也无济于事,因为该文件本身已经被压缩过了.于是许 ...
- React的单向数据流与组件间的沟通
今天来给大家总结下React的单向数据流与组件间的沟通. 首先,我认为使用React的最大好处在于:功能组件化,遵守前端可维护的原则. 先介绍单向数据流吧. React单向数据流: React是单向数 ...
- kaggle比赛流程(转)
一.比赛概述 不同比赛有不同的任务,分类.回归.推荐.排序等.比赛开始后训练集和测试集就会开放下载. 比赛通常持续 2 ~ 3 个月,每个队伍每天可以提交的次数有限,通常为 5 次. 比赛结束前一周是 ...
- Part2-HttpClient官方教程-Chapter5-流利的API
5.1. 易于使用的Facade API 使用之前注意引入相应Jar包或者Maven依赖 <dependency> <groupId>org.apache.httpcompon ...
- linux 服务简介
Linux服务(Linux services)对于每个应用Linux的用户来说都很重要.关闭不需要的服务,可以让Linux运行的更高效,但并不是所有的Linux服务都可以关闭.今天安装了一次CentO ...