标签:view tle 更新 margin padding pre 等于 从后往前 一个
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,因为位置越靠前,配对的个数越多
1 #include <iostream> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstring> 5 #include<stack> 6 #include<queue> 7 #include<deque> 8 #include<map> 9 #include<algorithm> 10 #define PI acos(-1.0) 11 using namespace std; 12 typedef long long ll; 13 map<int,int>::iterator it; 14 int str[111234]; 15 int ans[123114]; 16 int m,n; 17 int main() 18 { 19 scanf("%d",&m); 20 map<int,int>mp; 21 mp.clear(); 22 for(int i=1;i<=m;i++) 23 { 24 scanf("%d",&str[i]); 25 } 26 for(int i=m;i>=1;i--) 27 { 28 ans[i]=mp.size(); 29 if(mp.count(str[i]))//更新操作,若该元素第二次出现,上一次的ans赋值为0 30 { 31 ans[mp[str[i]]]=0; 32 } 33 mp[str[i]]=i; 34 } 35 ans[m+1]=‘\0‘; 36 ll sum=0; 37 for(int i=1;i<=m;i++) 38 sum+=ans[i]; 39 printf("%lld\n",sum); 40 return 0; 41 42 }
Codeforces Round #495 (Div. 2)(1004C)
标签:view tle 更新 margin padding pre 等于 从后往前 一个
原文地址:https://www.cnblogs.com/moomcake/p/9457127.html