Codeforce 1004C
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
5
1 5 4 1 3
9
7
1 2 1 1 1 3 2
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的更多相关文章
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- Codeforce 水题报告(2)
又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...
- codeforce 375_2_b_c
codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...
- codeforce 367dev2_c dp
codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...
- 三维dp&codeforce 369_2_C
三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...
- 强连通分量&hdu_1269&Codeforce 369D
强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...
- 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D
[树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...
- 解题报告:codeforce 7C Line
codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...
随机推荐
- mybatis分页插件使用注意
之前的项目用的数据库是mysql,在pom.xml引入这一个就能分页了. 后来又开了一个项目,使用的是oracle数据库,我写分页的时候发现不能实现,在网上找到资料说是必须要5.0以上的.我就导了这依 ...
- Linux下用c语言实现whereis.
简单的一个whereis的实现,代码如下: #include <stdio.h> #include <errno.h> #include <dirent.h> #i ...
- Visual Studio Nuget还原步骤
vs2013是在这里还原: NuGet套件还原步骤(以vs2012为例) 下载别人的范例,出现由于Nuget套件不存在而无法启动时:效果如下图: 步骤如下:1.点击 项目->启用NuGet程序包 ...
- go语言通过反射获取和设置结构体字段值的方法
本文实例讲述了go语言通过反射获取和设置结构体字段值的方法.分享给大家供大家参考.具体实现方法如下: type MyStruct struct { N int } n := MyStruct{ 1 } ...
- Linux 工具套件 —— binutils、readelf
readelf:Linux 下专门针对 ELF 文件格式的解析器: 0. binutils GNU Binutils gnu binutils 一套二进制工具的集合,主要包含:ld(gnu linke ...
- MbrFix 问题
删除Windows/Linux双系统下的Linux系统 参考博客 注意:官网上的 MbrFix.exe 下载或许太慢. CSDN下载 问题 1.看到博客下面的评论写道: 为什么我的到第四步的时候会提示 ...
- 深入理解java虚拟机-第二章
第2章 Java内存区域与内存溢出异常 运行数据区域 1.程序计数器(Program Counter Register) 是一块较小的内存空间,它可以看作是当前线程所执行的字节码的行号指示器. 2.J ...
- python之random库
random库是用于产生并运用随机数的标准库 1. random库函数 (1)random.seed(a) 设置随机种子数,可以是浮点数或整数,如果不设置的话,则random库默认以系统时间产生当作随 ...
- block的基本使用
block用来保存一段代码 block的标志:^ block跟函数很像: 1. 可以保存代码 2. 有返回值 3. 有形参 4. 调用方式一样 定义bolock变量 例1: void (^myBloc ...
- tensorflow学习笔记(三十九):双向rnn
tensorflow 双向 rnn 如何在tensorflow中实现双向rnn 单层双向rnn 单层双向rnn (cs224d) tensorflow中已经提供了双向rnn的接口,它就是tf.nn.b ...