ZOJ 3872 Beauty of Array (技巧)
来源:互联网 发布:python数据分析 pdf 编辑:程序博客网 时间:2024/06/05 08:16
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
题意
我们定义一个序列的贡献是它里面所有不相同的数字之和,然后给出一个数字序列,求其所有连续子序列的贡献和。
思路
假设当前给出序列为: 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
最终的结果是所有计算的数值之和。
AC 代码
#include<iostream>#include<cstdio>#include<cmath>#include<map>#include<cstring>#include<algorithm>#include<set>using namespace std;#define eps (1e-7)typedef long long LL;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;}
0 0
- 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 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(数学啊)
- ZOJ 3872-Beauty of Array(递推||DP)
- ZOJ 3872 Beauty of Array( DP思想 )
- ZOJ-3872Beauty of Array (动态规划)
- ZOJ 3872 Beauty of Array (思维 dp思想)
- ZOJ 3872 — Beauty of Array
- [dp] zoj 3872 Beauty of Array
- c语言中全局变量在多个文件中使用
- 蓝桥杯 打印十字图
- 回文数回文字符串判断
- FIFO在FPGA中应用的一些知识点
- 蓝桥 大臣的旅费
- ZOJ 3872 Beauty of Array (技巧)
- 已解决:Ubuntu安装sogou输入法后出现两个图标和输入框
- web开发-Windows系统下使用git for Windows软件-学习笔记六
- CSS hack
- WoWo
- Java学习之StringBuffer和StringBuilder
- windows无法自动将IP协议堆栈绑定到网络适配器
- Range Sum Query
- SpringMVC接收页面表单参数