CodeForces 1004C

  题目链接:http://codeforces.com/problemset/problem/1004/C

                                      C. Sonya and Robots

                                                                                 time limit per test

                                                                                1 second

                                                                                memory limit per test

                                                                                256 megabytes

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 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 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 nn (1<=n<=10^5) — the number of numbers in a row.

The second line contains nn integers a1,a2,…,an (1<=a_i<=10^5) — 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. 1-n:是每个机器人的排列顺序 
  3. 然后组合对中(a, b),b的位置不能在a的前面; 
  4. 然后求这样的组合对一共有多少个; 
  5. 解题思路: 
  6. 逆向,sum[i],表示i以后有多少个不同的数 
  7. int 会爆,用long long 解决。
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
int a[100005];
LL sum[100005];
int v1[100005];
int v2[100005];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&a[i]);
        v1[a[i]]++;
    }
    for(int i=n;i>=1;--i)
    {
        v2[a[i]]++;
        if(v2[a[i]]>=2)//这里sum[]表示的当前不同的数,加上本身
            sum[i]=sum[i+1];
        else
            sum[i]=sum[i+1]+1;
    }
    LL ans=0;//一定要用 long long + printf 输出
    for(int i=1;i<=n;++i)
    {
        if(v1[a[i]]>0)
        {
            ans=ans+sum[i+1];//所以计算时,下一个位置表示的才是其后面呢不同的种类数
            v1[a[i]]=0;
        }
    }
    printf("%lld\n",ans);
    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值