ZOJ 3872 Beauty of Array( DP思想 )
2016-04-21 21:38
483 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872
Beauty of Array
Time Limit: 2 Seconds Memory Limit: 65536 KB
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.
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.
Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest
题意:
给定一串数字,求所有连续的子序列的和,和为其中所有元素和(出现多次只算一次)。、
PS:
我们可以这样想:
因为要区别于不同的数
,可以看成序列里的数是一个一个加进去的,每次加入一个数,
统计前面序列里第一次出现新加入的这个数的位置;
这样每次加入的数的贡献度只会是这个数字之前出现的位置后面的数字!
//输入 1 2 3
//dp 1 5 14
//sum 1 6 20
//a[i] 1 2 3
详细点击
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
LL a[100010];
int main()
{
int t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
LL sum = 0, dp = 0;
int x;
for(int i = 1; i <= n; ++i)
{
scanf("%d",&x);
dp = (i-a[x])*x+dp;
sum += dp;
a[x] = i;
}
printf("%lld\n",sum);
}
return 0;
}
Beauty of Array
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest
题意:
给定一串数字,求所有连续的子序列的和,和为其中所有元素和(出现多次只算一次)。、
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 <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
LL a[100010];
int main()
{
int t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
LL sum = 0, dp = 0;
int x;
for(int i = 1; i <= n; ++i)
{
scanf("%d",&x);
dp = (i-a[x])*x+dp;
sum += dp;
a[x] = i;
}
printf("%lld\n",sum);
}
return 0;
}
相关文章推荐
- CodeForces - 557A Ilya and Diplomas (模拟)水
- 自定义控件 创建复合控件
- 专题二 第七道题和第八道题
- 消失的字符串——c语言函数中的数据存储方式以及字符串实现
- ucontext-人人都可以实现的简单协程库
- 测试作业
- cmd命令行交互执行python引入模块时提示ImportError: No module named xxx
- 结合asp使用JQuery框架下的Ajax实例
- QT环境搭建及常见错误
- Libgdx 之Stage 舞台类
- Sharepoint2013 AD组用户不同步
- repeater留言板[转]
- Web前端初步——IDE工具选择和emment插件
- waitKey()
- CodeForces - 554C Kyoya and Colored Balls (组合数学&逆元模板)
- 关于C语言的问卷调查
- solr源码导入eclipse中
- FPGA视觉从入门到放弃——Canny算子
- CMS设计-组件化
- 冲刺第一天(补发)