Sonya and Robots(CodeForces 1004C)


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 pipjpi≠pj or qiqjqi≠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 (1n1051≤n≤105) — the number of numbers in a row.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1051≤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
input
Copy
5
1 5 4 1 3
output
Copy
9
input
Copy
7
1 2 1 1 1 3 2
output
Copy
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).

题解:1-n:是每个机器人的排列顺序 ,然后组合对中(a, b),b的位置不能在a的前面; 然后求这样的组合对一共有多少个; 逆向求解,dp[i],表示i以后有多少个不同的数,注意int 会爆,用long long 解决

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<iomanip>
 8 #include<map>
 9 #include<stack>
10 #include<vector>
11 #include<queue>
12 #include<set>
13 #include<utility>
14 #include<list>
15 #include<algorithm>
16 #include <ctime>
17 #define max(a,b)   (a>b?a:b)
18 #define min(a,b)   (a<b?a:b)
19 #define swap(a,b)  (a=a+b,b=a-b,a=a-b)
20 #define memset(a,v)  memset(a,v,sizeof(a))
21 #define X (sqrt(5)+1)/2.0
22 #define maxn 320007
23 #define N 200005
24 #define INF 0x3f3f3f3f
25 #define PI acos(-1)
26 #define lowbit(x) (x&(-x))
27 #define read(x) scanf("%d",&x)
28 #define put(x) printf("%d\n",x)
29 #define memset(x,y) memset(x,y,sizeof(x))
30 #define Debug(x) cout<<x<<" "<<endl
31 #define lson i << 1,l,m
32 #define rson i << 1 | 1,m + 1,r
33 #define mod 1000000009
34 #define e  2.718281828459045
35 #define eps 1.0e-8
36 #define ll long long
37 using namespace std;
38 
39 
40 
41 ll dp[maxn];
42 int a[maxn], vis[maxn], vis2[maxn];
43 void init()
44 {
45     memset(dp, 0);
46     memset(a, 0);
47     memset(vis, 0);
48     memset(vis2, 0);
49 }
50 int main()
51 {
52     int n;
53     while (cin >> n)
54     {
55         init();
56         for (int i = 1; i <= n; i++)
57         {
58             cin >> a[i];
59         }
60         dp[n + 1] = 0;
61         for (int i = n; i >= 1; i--)//用数组下标处理,保证每个数只出现一次。
62         {
63             if (vis[a[i]] != 0)
64             {
65                 dp[i] = dp[i + 1];
66             }
67             else
68             {
69                 dp[i] = dp[i + 1] + 1;
70                 vis[a[i]] = 1;
71             }
72         }
73         ll ans = 0;
74         /*for(int i=1;i<=n;i++)
75             cout<<dp[a[i]]<<endl;*/
76         for (int i = 1; i <= n; i++)//将所有可能的条件找出
77         {
78             if (!vis2[a[i]])
79             {
80                 ans += dp[i + 1];
81                 vis2[a[i]] = 1;
82             }
83         }
84         cout << ans << endl;
85     }
86     return 0;
87 }
View Code
 

猜你喜欢

转载自www.cnblogs.com/baiyi-destroyer/p/9453148.html