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

题解:1-n:是每个机器人的排列顺序 ,然后组合对中(a, b),b的位置不能在a的前面; 然后求这样的组合对一共有多少个; 逆向求解,dp[i],表示i以后有多少个不同的数,注意int 会爆,用long long 解决。

代码如下:

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<utility>
#include<list>
#include<algorithm>
#include <ctime>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define memset(a,v) memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0
#define maxn 320007
#define N 200005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e-8
#define ll long long
using namespace std; ll dp[maxn];
int a[maxn], vis[maxn], vis2[maxn];
void init()
{
memset(dp, );
memset(a, );
memset(vis, );
memset(vis2, );
}
int main()
{
int n;
while (cin >> n)
{
init();
for (int i = ; i <= n; i++)
{
cin >> a[i];
}
dp[n + ] = ;
for (int i = n; i >= ; i--)//用数组下标处理,保证每个数只出现一次。
{
if (vis[a[i]] != )
{
dp[i] = dp[i + ];
}
else
{
dp[i] = dp[i + ] + ;
vis[a[i]] = ;
}
}
ll ans = ;
/*for(int i=1;i<=n;i++)
cout<<dp[a[i]]<<endl;*/
for (int i = ; i <= n; i++)//将所有可能的条件找出
{
if (!vis2[a[i]])
{
ans += dp[i + ];
vis2[a[i]] = ;
}
}
cout << ans << endl;
}
return ;
}

Sonya and Robots(CodeForces 1004C)的更多相关文章

  1. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  2. Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】

    B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...

  3. (CodeForces 558C) CodeForces 558C

    题目链接:http://codeforces.com/problemset/problem/558/C 题意:给出n个数,让你通过下面两种操作,把它们转换为同一个数.求最少的操作数. 1.ai = a ...

  4. [题解]Yet Another Subarray Problem-DP 、思维(codeforces 1197D)

    题目链接:https://codeforces.com/problemset/problem/1197/D 题意: 给你一个序列,求一个子序列 a[l]~a[r] 使得该子序列的 sum(l,r)-k ...

  5. 【Codeforces】【图论】【数量】【哈密顿路径】Fake bullions (CodeForces - 804F)

    题意 有n个黑帮(gang),每个黑帮有siz[i]个人,黑帮与黑帮之间有有向边,并形成了一个竞赛完全图(即去除方向后正好为一个无向完全图).在很多年前,有一些人参与了一次大型抢劫,参与抢劫的人都获得 ...

  6. Maximum Sum of Digits(CodeForces 1060B)

    Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...

  7. 【日常训练】Help Victoria the Wise(Codeforces 99C)

    题意与分析 这题意思是这样的:在正方体的六面镶嵌给定颜色的宝石(相同颜色不区分),然后问最多有几种彼此不等价(即各种旋转过后看起来一致)的方案. 其实可以乱搞,因为范围只有720.求出全排列,然后每个 ...

  8. 【日常训练】Help Far Away Kingdom(Codeforces 99A)

    题意与分析 题意很简单,但是注意到小数可能有一千位,作为一周java选手的我选择了java解决. 这里的分析会归纳一些必要的Java API:(待补) 代码 /* * ACM Code => c ...

  9. Palindrome Degree(CodeForces 7D)—— hash求回文

    学了kmp之后又学了hash来搞字符串.这东西很巧妙,且听娓娓道来. 这题的题意是:一个字符串如果是回文的,那么k值加1,如果前一半的串也是回文,k值再加1,以此类推,算出其k值.打个比方abaaba ...

随机推荐

  1. 深入理解Java虚拟机3-chap4-5-斗之气10段

    一.虚拟机性能监控与故障处理 1.JDK的命令行工具:对jdk/lib/tools.jar的薄包装,Linux下可能是Shell编写,执行类似于Linux中的命令 2.可视化工具JConsole 打开 ...

  2. 《linux就该这么学》第三节课 第二节命令笔记

    命令笔记 (随笔原创,借鉴请修改) linux系统中一切都是文件 2.4  系统状态的命令:  ifconfig   :    查看系统网卡信息,包括网卡名称,ip地址,掩码,mac地址,收到数据包大 ...

  3. docker构建本地仓库后,无法登陆解决办法(CentOS/Ubuntu)

    docker版本为:Server Version: 1.12.6 从dockerhub上下载最新的registry镜像. 首先.构建registry 1.下载registry镜像 docker pul ...

  4. [macOS] PHP双版本,5.6跟7.1

    转过来的,原文看这里,https://www.symfony.fi/page/how-to-run-both-php-5-6-and-php-7-x-with-homebrew-on-os-x-wit ...

  5. 把ArrayList集合中的字符串内容写到文本文件中

    list列表数据导出到指定路径文本文档中 public  String getSDCardPath() { String sdCard = Environment.getExternalStorage ...

  6. Access restriction: The type 'Unsafe' is not API

    错误:Access restriction: The type 'Unsafe' is not API Eclipse中有一种叫做存取限制的机制,来防止你错误使用那些非共享的API.通常来说,Ecli ...

  7. php 数组数字 补零

    $hour_list = range(0,24); foreach($hour_list as $key=>$val){ $hour_list[$key] = str_pad($val, 2, ...

  8. Linux下执行Oracle的sql脚本

    (1)  启动监听: Root用户登录后,输入: $su – oracle 回车(Oracle为Oracle数据库安装用户,必须有横杠: - ) 启动监听: $lsnrctl start --启动 $ ...

  9. FL studio钢琴卷工具简介

    FL studio中的钢琴卷工具在业内各编曲软件中享有当之无愧的声誉.钢琴卷是一款将音符和自动数据发送到钢琴卷的频道相关联的插件.而钢琴卷的这个操作运行过程被称为“排序”. 下面给大家介绍讲解钢琴卷中 ...

  10. mongodb+nodejs

    不能只看mongodb官网文档https://docs.mongodb.com/manual/reference/method/db.collection.findOne/,都是同步接口 要看node ...