Description

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.

Sample Input

 

Input
5
1 5 4 1 3
Output
9
Input
7
1 2 1 1 1 3 2
Output
7

Sample Output

 

Hint

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个数,每个数只能与自己后面的数配对,相同的配对只算一个,求配对的数量.

分析:我们可以倒着往前压,把数放进map(去重操作)中,从后往前放入容器中,ans[i]就等于容器中元素的个数,并且我们用mp[str[i]]=i,记录该元素第一次出现的位置以方便后面的更新,若该元素第二次出现,上一次出现的位置ans[pos]=0,因为位置越靠前,配对的个数越多

 #include <iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<algorithm>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
map<int,int>::iterator it;
int str[];
int ans[];
int m,n;
int main()
{
scanf("%d",&m);
map<int,int>mp;
mp.clear();
for(int i=;i<=m;i++)
{
scanf("%d",&str[i]);
}
for(int i=m;i>=;i--)
{
ans[i]=mp.size();
if(mp.count(str[i]))//更新操作,若该元素第二次出现,上一次的ans赋值为0
{
ans[mp[str[i]]]=;
}
mp[str[i]]=i;
}
ans[m+]='\0';
ll sum=;
for(int i=;i<=m;i++)
sum+=ans[i];
printf("%lld\n",sum);
return ; }

Codeforce 1004C的更多相关文章

  1. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  2. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  3. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  4. codeforce 375_2_b_c

    codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...

  5. codeforce 367dev2_c dp

    codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...

  6. 三维dp&codeforce 369_2_C

    三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...

  7. 强连通分量&hdu_1269&Codeforce 369D

    强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...

  8. 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D

    [树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...

  9. 解题报告:codeforce 7C Line

    codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...

随机推荐

  1. 剑指offer--35.数组中只出现一次的数字

    时间限制:1秒 空间限制:32768K 热度指数:198150 本题知识点: 数组 题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. class ...

  2. Visual Studio 2008常见问题

    1.asp.net在什么软件上运行?学习asp往往需要测试asp程序,电脑不能直接测试,需要装IIS才能运行,但装IIS要么需要安装盘,要么需要安装包,而且设置也很复搜索杂.这里给大学推荐两个替代II ...

  3. PostgreSQL流复制记录

    参考了别人的部分,添加了自己在实践中的内容,仅做记录. 1.同步流复制中 主机操作 1.1postgresql.conf wal_level = hot_standby # 这个是设置主为wal的主机 ...

  4. Could not find a valid gem 'rails' (>= 0), here is why

    很长一段时间之前 Ruby Rails入门--windows下搭建Ruby Rails Web开发环境 ,由于后来将Ruby的安装文件从 C 盘移动到了 D 盘,也修改了 Path 环境变量,ruby ...

  5. Dijkstra算法(带路径模板)

    个人心得:Dijkstra算法是贪心思想的一种延伸,注意路径pre,pre数组表示此时最短路径中的前一个顶点.每次更新到目的点时更新: 从源点出发,更新路径,然后找出此时最短的点,然后以这个点为头,看 ...

  6. Linux 服务器的那些性能参数指标

    一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤其当你的程序非正常工作的时候, ...

  7. 12C 对表分区维护的增强

    Oracle Database 12c对表分区变化比较多,共分为下面几点 1.在线移动分区:通过MOVE ONLINE关键字实现在线分区移动.移动过程中,对表和被移动的分区可以执行查询操作, DML语 ...

  8. Java 引用类型变量的声明和使用

    引用类型变量的声明和使用 (1)把类名当作是一种类型来声明变量,这种变量叫引用类型变量.如:People people; (2)引用类型变量保存对象的“引用”,即对象的地址. (3)对象的创建 new ...

  9. 善待Erlang 代码 -- 巧用 user_default

    这是一篇水文 ----------------------------------------------------- 很好用的一个技巧 http://www.erlang.org/doc/man/ ...

  10. java.sql.SQLException: 无法转换为内部表示

    1. 我出这个错代码:wfef.setRow_num(rs.getInt(rs.getInt(12))); 2. 分析:rs.getInt(12)  得到的是int类型,rs.getInt(rs.ge ...