Beauty Values
题目描述
Gromah and LZR have entered the second level. There is a sequence a1,a2,a3……an on the wall.
There is also a note board saying “the beauty value of a sequence is the number of different elements in the sequence”.
LZR soon comes up with the password of this level, which is the sum of the beauty values of all successive subintervals of the sequence on the wall.
Please help them determine the password!
输入描述
The first line contains one positive integer n , denoting the length of the sequence.
The second line contains n positive integers a1,a2,a3……an, denoting the sequence.
1 ≤ ai ≤ n ≤ 105
输出描述
Print a non-negative integer in a single line, denoting the answer.
输入
4
1 2 1 3
输出
18
说明
The beauty values of subintervals [1,1], [2,2], [3,3], [4,4] are all 1
.
The beauty values of subintervals [1,2], [1,3], [2,3], [3,4] are all 2
.
The beauty values of subintervals [1,4], [2,4] are all 3
.
As a result, the sum of all beauty values are 1×4+2×4+3×2=18.
题意
所有子串的中每个个子串的元素种类之和,贪就完事。
思路
计算一下每一个元素在子串中一共占了多少
eg:
母串:1213
子串1类:
子串2类:
子串3类:
子串4类:
统计:
总结:
所有元素和之前的与此元素相同的元素下标只差 与 此元素为开头的子串个数 的 乘机 之 和。
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ll n, m, i, j, t, w;
while (cin >> n) {
map<ll, ll>be, en;
for (i = 1, j = 0; cin >> t, i <= n; i++) {
be[t] = en[t]; en[t] = i;
j += ((en[t] - be[t]) * (n - i + 1));
}
cout << j << endl;
}
}