0
点赞
收藏
分享

微信扫一扫

CodeForces - 1004C Sonya and Robots 印象深刻


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 (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

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).

题意:

给你n个数字,选出来(x,y)满足y的位置大于x,求(x,y)个数。

分析:

这题真是我这一个月以来打比赛印象最深的一道题,开始我的做题路程,一开始自然是简单的暴力枚举,自然超时,然后优化了一下,把连续的数都删掉之剩下一个,如1 2 1 1 1 3 2,变成1 2 1 3 2,在暴力枚举,但还是超时,无奈暂时犯下,最后回来这道题,想到了可以预处理一下,假设1 2 1 1 1   3 2 ,我们简化为1 2 1 3 2 ,我们重开一个数组b[i],表示i+1~n与a[i]可以凑成(x,y)的个数,3 3 2 1 0,我们发现3+3+1为7就是答案,为什么不加2呢,因2对应的是1,在第一个就加完了。为了进一步优化效率,而且还推论了从后往前,从前往后都是一样的,所以在输入的时候就可以预处理。但是我发现一个问题,就不能上出连续的数,你们就不要想,可以看看例子,1 1 2 2 3,可能就傻,所以说直接对原数组优化就行了,本来以为这样就大功告成了,结果答案错误,我的天哪,疯狂的改,还是不对,一直到比赛快结束,赛后看别人题解,一看对呀,还自我感觉多推了一个从后处理也行,优化了部分时间,对照了好久,结果竟然答案要long long 我的天哪,真是,我差点叫气死了。啊啊啊

 

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 100001
#define MOD 1e9+7
#define E 1e-6
#define LL long long
using namespace std;
int a[100005],b[100005];
int vis[100005];
int main()
{
int n;

cin>>n;
long long ans=0;

for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(vis[a[i]]!=0)
{
b[i+1]=b[i];
}
else
{
b[i+1]=b[i]+1;
vis[a[i]]=1;
//cout<<i<<" "<<b[i]<<end
}

}
if(n==2){cout<<1<<endl;return 0;}
memset(vis,0,sizeof(vis));
for(int i=n;i>1;i--)
{
//cout<<b[i]<<vis[b[i]]<<endl;
if(vis[a[i]]==0)
{
ans+=b[i];
vis[a[i]]=1;
}

}
cout<<ans<<endl;
return 0;
}

 

举报

相关推荐

CodeForces 670B Game of Robots

0 条评论