ZOJ 3872 Beauty of Array (思维 dp思想)
来源:互联网 发布:华为端口聚合配置 编辑:程序博客网 时间:2024/06/05 01:06
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
351 2 3 4 532 3 342 3 3 2
Sample Output
1052138
题目描述: 求一段序列中连续子序列的和,如果这段连续子序列中有重复的话,(the summation of all distinct integers in the arra),这个序列中不相同的数的和,也就是说只计算一次。
思路:如果要求的是一段序列中连续子序列的个数,那么如果定义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]。
这么水的题,我一定是因为没有吃饭的原因才没有秒掉,嗯,一定是。。
其实就是给你一个数组问你所有可能的子串的和是什么,这个可以dp啊。。。对于每个i, 他的起始点可以是i-1,i-2,i-3,他就是由前面那个数字的每个区间+a[i]就好了。。。这题要求没有重复的,那就记下他之前出现的位置, 当前这个数,对前面那个位置前的数都不再加贡献就好了。。好水,一定是我很饿的原因没有秒掉。。一定是。。
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int maxn = 1e6 + 5;int a[maxn], pre[maxn];int main(){ int n, t; scanf("%d", &t); while(t--) { scanf("%d", &n); memset(pre, 0, sizeof(pre)); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); long long ans = 0, sum = 0; for(int i = 1; i <= n; i++) { sum = sum + (i-pre[a[i]])*a[i]; ans += sum; pre[a[i]] = i; } printf("%lld\n", ans); } return 0;}
- ZOJ 3872 Beauty of Array (思维 dp思想)
- ZOJ 3872 Beauty of Array( DP思想 )
- zoj--3872--Beauty of Array(思维)
- [dp] zoj 3872 Beauty of Array
- ZOJ 3872 Beauty of Array【dp】
- [dp] zoj 3872 Beauty of Array
- ZOJ 3872 Beauty of Array(DP)
- ZOJ 3872-Beauty of Array(递推||DP)
- ZOJ 3872 Beauty of Array(数学)
- ZOJ 3872 Beauty of Array(模拟)
- ZOJ 3872 Beauty of Array (技巧)
- ZOJ 3872Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array
- ZOJ 3872 Beauty of Array(数学啊)
- compileSdkVersion、targetSdkVersion、Support库的作用与他们之间的关系
- FormatMessage函数
- ASP.NET MVC 常用扩展点:过滤器、模型绑定等
- .msi 版本 和 .zip 版本的区别
- smtp发无附件邮件
- ZOJ 3872 Beauty of Array (思维 dp思想)
- pistache(C++ REST 工具) 使用
- maven应用(二)
- 剑指offer-31.整数中1出现的次数(从1到n整数中1出现的次数)
- PHP接口类interface使用方法
- hdu 5978 · To begin or not to begin【概率题】【手速题】
- 单源最短路径
- Map集合及HashMap源码分析
- 二分贪心 G