c语言sscanf函数的用法是什么
273
2022-08-31
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 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
发表评论
暂时没有评论,来抢沙发吧~