发表文章

[最新] CodeForces 1004C Sonya and Robots

codertcm 3月前 1

Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.

Sonya has drawn n numbers in a row, ai is located in the i-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] are written, and Sonya gives the number 
1 to the first robot and the number 4 to the second one, the first robot will stop in the 1-st position while the second one in the 3-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 4 to the first robot and the number 5 to the second one, they will meet since the first robot will stop in the 3-rd position while the second one is in the 2-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 (p, q), where she will give p to the first robot and q to the second one. Pairs (pi, qi) and (pj, qj) are different if pi≠pj or qi≠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 n (1≤n≤105) — the number of numbers in a row.

The second line contains n integers a1,a2,…,an (1≤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

1 5 4 1 3 
output

input

1 2 1 1 1 3 2 
output

Note 
In the first example, Sonya can give pairs (1, 1), (1, 3), (1, 4), (1, 5), (4, 1), (4, 3), (5, 1), (5, 3), and (5, 4). 
In the second example, Sonya can give pairs (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), and (3, 2).

 

题意:每个数可以和它后面的数组成一个数对,问有多少个这样的不同数对。

暴力:从当前数开始,枚举所有能构成的数对。即计算当前数之后有多少个不同的数。

优化一:以ai开头的数对统计过之后,下次出现ai,就不用统计了,

优化二:用后缀数组记录此时ai之后有多少个不同的数。

代码实现:

可以用set,简化书写。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define N 200000 + 10
using namespace std;
const int intN = 3e5 + 10 ;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };

int GCD(int a,int b)
{
    return b ? GCD(b,a%b) : a;
}


int main()
{
    LL n,i,out=0;
    LL ans[N],s[N],ha[N]={0};
    set<LL>p;
    scanf("%lld",&n);
    for(i=0;i<n;i++)
        scanf("%lld",&s[i]);
    for(i=n-1;i>=0;i--)
    {
        p.insert(s[i]);
        ans[i]=p.size();
    }
    for(i=0;i<n-1;i++)
    {
        if(ha[s[i]]==0)
        {
            out+=ans[i+1];
            ha[s[i]]=1;
        }
    }
    printf("%lld\n",out);
    return 0;
}

 

相关推荐
最新评论 (0)
返回
发表文章
codertcm
文章数
52
评论数
0
注册排名
1304837