cf1004C Sonya and Robots

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]

are written, and Sonya gives the number 1 to the first robot and the number 4 to the second one, the first robot will stop in the 1-st position while the second one in the 3-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 4 to the first robot and the number 5 to the second one, they will meet since the first robot will stop in the 3-rd position while the second one is in the 2

-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 pipj or qiqj

.

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(1n105) — the number of numbers in a row.

The second line contains nintegers a1,a2,,an (1ai105) — 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 (1, 1), (1, 3), (1, 4), (1, 5), (4, 1), (4, 3), (5, 1), (5, 3), and (5, 4).

In the second example, Sonya can give pairs (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), and (3, 2).



题意:给你一个数列,在数列的最左边有一个机器人(1号),在最右边有一个机器人(2号)。我们分别给这两个机器人给一个数字,当他们在数列中寻找到这个数字的时候他们就会停下来,然后要求他们在相遇之前就停下来了。求给机器人的数字的方案数,机器人接受到的数字一定是存在的。


解题思路:用树状数组来维护一下当最后一次到达x时,在他前面不同数的个数。有个小坑。


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
#define ll long long
int head[maxn],en[maxn],c[maxn],n;
int lowbit(int x){
	return x&(-x);
}
void add(int x){
	while(x<=n){
		c[x]++;
		x+=lowbit(x);
	}
}
int query(int x){
	int ans=0;
	while(x>=1){
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
void init(){
	memset(head,-1,sizeof(head));
	memset(en,-1,sizeof(en));
	memset(c,0,sizeof(c));
}
void solve(){
	scanf("%d",&n);
	int i,j,m,ma=0;
	for(i=1;i<=n;i++){
		scanf("%d",&m);
		if(head[m]==-1) head[m]=en[m]=i;
		else en[m]=i;
		if(m>ma) ma=m;
	}
	for(i=1;i<=ma;i++) if(head[i]!=-1) add(head[i]);
	ll ans=0;
	for(i=1;i<=ma;i++){
		if(head[i]!=-1){
			ans+=query(en[i]-1);
		}
	}
	printf("%I64d\n",ans);
}
int main(){
	init();
	solve();
	return 0;
}

试着习惯模块化敲代码。

猜你喜欢

转载自blog.csdn.net/bao___zi/article/details/80949319