codeforces ~ 1004 C Sonya and Robots (dp)

C. Sonya and Robots

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

51 5 4 1 3

output

Copy

9

input

Copy

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

题意:

给你n个数字,有两个不同的机器人,一个在数列的最左端,另一个在数列的最右端,

如果机器人1标号为x,那么他从左往右会在第一个x这里停下来

第二个机器人标号为y,他会从右往左在第一个y这里停下来

求(x,y)这样的一共有多少对,可以使得机器人不相撞

题解:

左边的机器人遇见重复的就不能往后走了

右边的机器人遇见重复的就不能往前走了

那么我们先将前n个数中不重复的表给打出来

那么从右往左走的时候,不同的个数就是我们要求的对数

注意开longlong

代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int a[maxn];
int vis[maxn];
int dp[maxn];
int main(){
  int n;
  while(cin>>n){
    memset(vis,0,sizeof(vis));
    memset(dp,0,sizeof(dp));
    dp[0]=0;
    for(int i=1;i<=n;i++){
      cin>>a[i];
      if(!vis[a[i]]){
        vis[a[i]]=1;
        dp[i]=dp[i-1]+1;
      }else{
        dp[i]=dp[i-1];
      }
    }
    long long ans=0;
    memset(vis,0,sizeof(vis));
    for(int i=n;i>=1;i--){
      if(!vis[a[i]]){
        vis[a[i]]=1;
        ans+=dp[i-1];
      }
    }
    printf("%I64d\n",ans);

  }
}

原文地址:https://www.cnblogs.com/buerdepepeqi/p/9285940.html

时间: 2024-08-03 23:07:18

codeforces ~ 1004 C Sonya and Robots (dp)的相关文章

CodeForces 540D Bad Luck Island 概率dp

CodeForces 540D 应该是简单概率dp,由于写得少显得十分蠢萌 求期望逆推,求概率正推,大概是这么个意思,贴一发留恋 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define db double const int maxn=108; db dp[maxn][maxn][maxn]; int main() { int i,j,n,m,k,p; whi

codeforces 148E Aragorn&#39;s Story 背包DP

Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/148/E Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who

CodeForces 21D Traveling Graph 状压dp+欧拉回路

题目链接:点击打开链接 题意: 给定n个点m条边的无向图 求从1点开始经过每条边至少一次最后回到1点的最小路程 显然就是找一条路径可重复的欧拉回路 思路: 首先对于欧拉回路的结论是:所有点的度数都为偶数 因为所有边至少经过一次,那么可以把题意转换成加最少多少条边使得图满足以上结论 而加的边目的是为了把奇度数转成偶度数,先floyd一下得到任意点间加边的最小花费 dp[i]表示状态i下度数都为偶数的最小花费. 状压dp,把i状态下,所有未选择的点中挑2个奇度数的转移即可. #include <cs

Codeforces 67C Sequence of Balls 编辑距离 dp

题目链接:点击打开链接 有一个交换操作比较特殊,所以记录每个点距离自己最近的那个字符的位置 然后交换就相当于把第一行要交换的2个字符 之间的字符都删掉 把第二行要交换的2个字符 之间的字符都插入第一行的2个字符之间 然后再进行交换. #include <cstdio> #include <cstring> #include<iostream> using namespace std; #define inf 10000000 #define N 4005 #define

Codeforces Round #261 (Div. 2) E (DP)

E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are

codeforces 514E Darth Vader and Tree (dp+快速幂)

codeforces 514E Darth Vader and Tree (dp+快速幂) 题意: 有一棵树,每个节点有n个儿子,给出父亲到每个儿子的距离di,问离祖先距离不超过x的子孙有多少个(子孙包括祖先)对1e9+7取模. 限制: 1 <= n <= 1e5; 0 <= x <= 1e9; 1 <= di <= 100 思路: 因为di <= 100,所以可以用快速幂来处理这道题, 大概过程为:先用dp算出前100的答案,剩下的用快速幂来处理.

Codeforces 56D Changing a String 编辑距离 dp

题目链接:点击打开链接 编辑距离,,== 一边dp一边记录前驱太累,,还是dp后找路径大法好 #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll int #define N 1010 char s[N], t[N]; int dp[N][N], n, m; // 0为插入 1为删除 2 3为替换 stru

Codeforces 455A Boredom 取数字的dp

题目链接:点击打开链接 给定一个n长的序列 删除x这个数就能获得x * x的个数 的分数,然后x+1和x-1这2个数会消失,即无法获得这2个数的分数 问最高得分. 先统计每个数出现的次数,然后dp一下,对于每个数只有取或不取2种状态. #include <algorithm> #include <cctype> #include <cassert> #include <cstdio> #include <cstring> #include <

[Codeforces 464D]World of Darkraft(期望DP)

[Codeforces 464D]World of Darkraft(期望DP) 题面 游戏中有k种装备,每种装备初始时都是等级1.zyd每打一只怪,就会随机爆出一件装备.掉落和更新装备方式如下: 假设这种装备当前等级为t,那么系统会在[1,t+1]中等概率随机出该装备的等级.爆出装备后,会装备上身上的和爆出的装备之间等级更高的那件,并卖掉等级更低的装备.其中等级为i的装备价格为i金币. 求打了n只怪后获得金币的期望值,精确到\(10^{-9}\). \(n,k\leq 10^5\) 分析 首先