codeforces ~ 1004 C Sonya and Robots (dp)
1 second
256 megabytes
standard input
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.
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.
Print one number — the number of possible pairs that Sonya can give to robots so that they will not meet.
5
1 5 4 1 3
9
7
1 2 1 1 1 3 2
7
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+;
int a[maxn];
int vis[maxn];
int dp[maxn];
int main(){
int n;
while(cin>>n){
memset(vis,,sizeof(vis));
memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<=n;i++){
cin>>a[i];
if(!vis[a[i]]){
vis[a[i]]=;
dp[i]=dp[i-]+;
}else{
dp[i]=dp[i-];
}
}
long long ans=;
memset(vis,,sizeof(vis));
for(int i=n;i>=;i--){
if(!vis[a[i]]){
vis[a[i]]=;
ans+=dp[i-];
}
}
printf("%I64d\n",ans); }
}
codeforces ~ 1004 C Sonya and Robots (dp)的更多相关文章
- Sonya and Robots CodeForces - 1004C (思维题)
Sonya and Robots time limit per test 1 second memory limit per test 256 megabytes input: standard in ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- Sonya and Robots(CodeForces 1004C)
Since Sonya is interested in robotics too, she decided to construct robots that will read and recogn ...
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)
题目链接: C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 ...
- Codeforces Round #495 (Div. 2) C. Sonya and Robots
http://codeforces.com/contest/1004/problem/C 题意: 在一行上有n个数字,现在在最左边和最右边各放置一个机器人,左右机器人各有一个数字p和q.现在这两个机器 ...
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
随机推荐
- gzip,bzip2,xz压缩工具
gzip,bzip2,xz压缩工具====================== gzip压缩工具 示例:[root@aminglinux yasuo]# ls1.txt 2.txt 3.txt[roo ...
- docker简介以及优缺点
1.docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制, ...
- 7-3 python操作excel
1.写excel 写入特定单元格数据 # .导入xlwt模块 # .新建一个excel # .添加一个sheet页 # .往指定的单元格中写入数据 # .保存excel import xlwt boo ...
- HyperLedger Fabric 1.4 区块链技术定义(2.1)
区块链技术指使用点对点传输.共识机制.加密算法等技术,保证分布式数据库区块写入链中数据的一致性,达到去中心化和不可篡改的目的. 区块链就是一种特殊的分布式数据库,使用现有的各种成熟的技术, ...
- POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 153 ...
- 硬件中断--DEBUG系列
问题描述: 在线调试时,全速运行,程序进入硬件中断,查看堆栈窗口,发现是从A函数进去的.但是A函数应该没有问题的: 再次重复,发现是从B函数进去的,但是B函数之前运行起来也没有问题的,而且没有传入参数 ...
- Apache 多端口配置方法
首先修改httpd.conf配置文件. 添加8080端口 Listen 8080 打开虚拟配置文件 # Virtual hosts Include conf/extra/httpd-vhosts.co ...
- Python3中文教程
搜索 此文档来源自网络 安装 PYTHON❝ Tempora mutantur nos et mutamur in illis. (时光流转,吾等亦随之而变.) ❞ — 古罗马谚语 深入欢迎来到 Py ...
- 常见算法用Pascal实现
基本算法 这些都是非常基本的的算法,希望所有学习的人都能理解! 1.数论算法 求两数的最大公约数 function gcd(a,b:integer):integ ...
- 【Adaptive Boosting】林轩田机器学习技法
首先用一个形象的例子来说明AdaBoost的过程: 1. 每次产生一个弱的分类器,把本轮错的样本增加权重丢入下一轮 2. 下一轮对上一轮分错的样本再加重学习,获得另一个弱分类器 经过T轮之后,学得了T ...