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 ...
随机推荐
- Python 中关于文件操作的注意事项
文件操作 #打开文件 f = open('要打开的文件路径',mode = 'r/w/a', encoding = '文件原来写入时的编码') #操作 data = f.read() #读取 f.wr ...
- G - Dreamoon and NightMarket Gym - 101234G 优先队列+思路
题目:题目链接 题意:给出n种食物,食物有自己的价格并且可以自由搭配,每天吃之前没吃过的花费最少的搭配,问第k天的花费. 思路:第k小我们考虑用优先队列处理,虽然n比较大,但由于1 ≤ K ≤ min ...
- urllib使用一
urllib.urlopen()方法: 参数: 1.url(要访问的网页链接http:或者是本地文件file:) 2.data(如果有,就会由GET方法变为POST方法,提交的数据格式必须是appli ...
- Hive 压缩技术Data Compression
Mapreducwe 执行流程 :input > map > shuffle > reduce > output 压缩执行时间,map 之后,压缩,数据存储在本地磁盘,减少磁盘 ...
- python读取文件
请参考:http://www.cnblogs.com/sysuoyj/archive/2012/03/14/2395789.html
- mongo创建数据库和用户
1.linux安装mongo conf文件配置: 配置文件: dbpath=/home/data/mongodb/mongodb logpath=/home/data/logs/mongodb.log ...
- 使用Windows SFC和DISM工具来解决服务器OS问题
TechTarget中国原创] 随着使用时间的越来越多,Windows服务器安装的系统文件可能会被损坏或损毁.管理员一般可以通过系统自带的System File Checker (SFC) 或者更健壮 ...
- 【Kernel Logistic Regression】林轩田机器学习技术
最近求职真慌,一方面要看机器学习,一方面还刷代码.还是静下心继续看看课程,因为觉得实在讲的太好了.能求啥样搬砖工作就随缘吧. 这节课的核心就在如何把kernel trick到logistic regr ...
- 【Kernal Support Vector Machine】林轩田机器学习技术
考虑dual SVM 问题:如果对原输入变量做了non-linear transform,那么在二次规划计算Q矩阵的时候,就面临着:先做转换,再做内积:如果转换后的项数很多(如100次多项式转换),那 ...
- appium+python的APP自动化(2)
上节说到安卓上的测试环境都安装好了,这个时候要安装python了 1python的安装 https://www.python.org/15官网下载python2.7(3.0以上也行,个人爱好),安装也 ...