Sonya and Robots CodeForces - 1004C(思维)

C. 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 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 (ppqq), where she will give pp to the first robot and qq to the second one. Pairs (pipiqiqi) and (pjpjqjqj) 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 (1111), (1133), (1144), (1155), (4411), (4433), (5511), (5533), and (5544).

In the second example, Sonya can give pairs (1111), (1122), (1133), (2211), (2222), (2233), and (3322)


题意:给出一个长为n的数列(数字可以重复),表示在i号位置的数字为ai。现在,在数列的最左和最右端有一个报数机器人,最左端的机器人向右移动,最右端的机器人向左移动,每移动到一个位置时,机器人就会将这个位置的数字报出来。现在,每个机器人都有一个编号,如果机器人报出来的数字和这个编号相同的话,机器人就会停止移动。问,这两个机器人的编号有多少种情况,可以使这两个机器人不发生碰撞。


思路:要使机器人不发生碰撞,那么,在机器人相遇之前,必须停止移动。我们只需从1号位置开始检查,判断1号位置后面有多少个不同的数字,然后再从2号位置开始检查,判断2号位置后面有多少个不同的数字,依此类推,最后将所有的结果加起来就可以了(注意,从i号位置检查后面的数时,需要先判读这个位置的数是否以及出现过)。 如果直接暴力检查的话,肯定会tle,所以,我们先预处理出每个位置后面不同数字的个数,然后之接累加。


#include "iostream"
#include "cstring"
using namespace std;
const int Max=1e5+10;
int a[Max],vis[Max],sum[Max];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=n;i>=1;i--){//预处理
        if(vis[a[i]]) sum[i]=sum[i+1];//求出i位置后面不同数字的个数
        else sum[i]=sum[i+1]+1,vis[a[i]]=1;
    }
    memset(vis,0, sizeof(vis));
    long long ans=0;
    for(int i=1;i<n;i++){//从i位置开始检查,如果这个数没出现过,那么ans加上这个位置后面的不同数字的个数
        if(!vis[a[i]]) ans+=sum[i+1],vis[a[i]]=1;
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80961309
今日推荐