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 ...
随机推荐
- 获取整个页面HTML的信息以及注意事项
两种方式: 1.从每个节点获取 var a = '<!DOCTYPE html><html lang="zh-cn">'; var z = "&l ...
- LeetCode OJ:Excel Sheet Column Number(表格列数)
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- jsp中的session
浏览器和服务器的异常通话 常用方法 setAttribute(String key,Object value);//设置值 getAttribute(String key); //取值 Invalid ...
- matlab批量转化img到gray
path = 'D:\宝贝儿数据集\COIL-3D\coil-100\'; save_path = 'D:\宝贝儿数据集\COIL-3D\coil-100-gray\'; file=dir([path ...
- Leetcode 912. Sort an Array
class Solution: def sortArray(self, nums: List[int]) -> List[int]: return sorted(nums)
- ActiveMQ面试题
什么是activemq activeMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信. activemq的作用以及原 ...
- C# 自定义exe引用的dll路径
MSDN原文:https://msdn.microsoft.com/library/twy1dw1e(v=vs.100).aspx <runtime> 的 <assemblyBind ...
- utf_8 和 GBK 不搭出现乱码(转)
今天调数据的时候发现调出来的一直是乱码,用了各种方法都未解决,简单的调数据用了好长时间,最后突然发现了一个问题 页面的 <meta http-equiv="Content-Type&q ...
- bzoj 4556 字符串 —— 后缀数组+主席树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4556 就是找一个 rk 在一段区间内的前驱和后继: 由于 LCP 还有区间长度的限制,所以可 ...