Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.
Sonya has drawn nn numbers in a row, aiai is located in the ii-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position.
Sonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one.
For example, if the numbers [1,5,4,1,3][1,5,4,1,3] are written, and Sonya gives the number 11 to the first robot and the number 44 to the second one, the first robot will stop in the 11-st position while the second one in the 33-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number 44 to the first robot and the number 55 to the second one, they will meet since the first robot will stop in the 33-rd position while the second one is in the 22-nd position.
Sonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot.
Sonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs (pp, qq), where she will give pp to the first robot and qq to the second one. Pairs (pipi, qiqi) and (pjpj, qjqj) are different if pi≠pjpi≠pj or qi≠qjqi≠qj.
Unfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet.
Input
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of numbers in a row.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1051≤ai≤105) — the numbers in a row.
Output
Print one number — the number of possible pairs that Sonya can give to robots so that they will not meet.
Examples
5
1 5 4 1 3
9
7
1 2 1 1 1 3 2
7
Note
In the first example, Sonya can give pairs (11, 11), (11, 33), (11, 44), (11, 55), (44, 11), (44, 33), (55, 11), (55, 33), and (55, 44).
In the second example, Sonya can give pairs (11, 11), (11, 22), (11, 33), (22, 11), (22, 22), (22, 33), and (33, 22).
一般的暴力思想是两重循环,从序列中的第一个数开始,判度这个数之后有几个不重复的数。这样会超时。
利用 STL 中的 set 来简化暴力,首先将从计算第一个数可以和哪些数进行匹配,改为从最后一个数开始计算。
表达能力有限,解释不清楚,还是看代码吧。
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<stack> 9 #include<deque> 10 #include<map> 11 #include<set> 12 #include<iostream> 13 using namespace std; 14 typedef long long LL; 15 const double pi=acos(-1.0); 16 const double e=exp(1); 17 const int N = 10; 18 19 20 LL con[100009]; 21 LL init[100009]; 22 LL check[100009]; 23 set< LL > qq; 24 int main() 25 { 26 LL n,i,p,j; 27 LL ans=0; 28 scanf("%lld",&n); 29 for(i=1;i<=n;i++) 30 { 31 scanf("%lld",&init[i]); 32 con[init[i]]=qq.size(); 33 qq.insert(init[i]); 34 } 35 for(i=1;i<=n;i++) 36 { 37 //printf("%d ",con[init[i]]); 38 if(check[init[i]]!=-1) 39 { 40 ans+=con[init[i]]; 41 check[init[i]]=-1; 42 } 43 else 44 ; 45 } 46 printf("%lld ",ans); 47 return 0; 48 }