Sonya and Robots CodeForces - 1004C (思维题)

Sonya and Robots
time limit per test
1 second
memory limit per test
256 megabytes
input:
standard input
output:
standard output

Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.

Sonya has drawn 𝑛n numbers in a row, 𝑎𝑖ai is located in the 𝑖i-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 (𝑝p, 𝑞q), where she will give 𝑝p to the first robot and 𝑞q to the second one. Pairs (𝑝𝑖pi, 𝑞𝑖qi) and (𝑝𝑗pj, 𝑞𝑗qj) are different if 𝑝𝑖𝑝𝑗pi≠pj or 𝑞𝑖𝑞𝑗qi≠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 𝑛n (1𝑛1051≤n≤105) — the number of numbers in a row.

The second line contains 𝑛n integers 𝑎1,𝑎2,,𝑎𝑛a1,a2,…,an (1𝑎𝑖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

Input
5
1 5 4 1 3
Output
9
Input
7
1 2 1 1 1 3 2
Output
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).

题意

给左右机器人各一个数字p和q,左边机器人往右走,右边机器人往左走,遇到对应的数字就停下,问要使他们不相遇有多少种p,q。

思路

记录每一个位置的后面有几个不同的数字,之后只需要从前往后遍历一遍把所需要的值加上就好了

 

 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;

#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e5+10;


int main()
{
    int a[maxn];
    int b[maxn];   //b[i]表示i-n之间有多少个不重复的shu zi
    int vis[maxn];
    int n;
    ll sum=0;
    int cnt=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=n;i>0;i--)
    {
        if(vis[a[i]]==0)
            vis[a[i]]=1,cnt++;
        b[i]=cnt;
    }
    memset(vis,0,sizeof vis);
    for(int i=1;i<n;i++)
        if(vis[a[i]]==0)
            sum+=b[i+1],vis[a[i]]=1;
    printf("%lld\n",sum);
    
}
View Code

 

posted @ 2018-11-04 22:18  千摆渡Qbd  阅读(202)  评论(0编辑  收藏  举报