Codeforces Round #495 (Div. 2) C. Sonya and Robots
http://codeforces.com/contest/1004/problem/C
题意:
在一行上有n个数字,现在在最左边和最右边各放置一个机器人,左右机器人各有一个数字p和q。现在这两个机器人从起点开始分别向右和向左移动,当它们移动到和他们自己数字相同的格子时就会停止,否则就会一直移动下去知道出界。现在要计算出有多少组不重复的(p,q)可以使得这两个机器人不相遇。
思路:
只要去考虑每个数第一次出现的位置和最后出现的位置即可。这题我用了前缀和的思想,首先从左到右扫描一遍,算出1~i这个区间内有多少个不重复的数。然后从右到左再扫描一遍,算出i~n这个区间内有多少个不重复的数。
最后再扫描一遍即可,对于第i个数(这个数是第一次出现),那么在它右边有多少不重复的数都是可以与之配对的。最后相加即可。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e5+; int a[maxn];
int n, first[maxn], last[maxn], fnum[maxn], lnum[maxn]; int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&n);
memset(first,-,sizeof(first));
memset(last,-,sizeof(last));
fnum[] = lnum[n+] = ;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
fnum[i] = fnum[i-];
if(first[a[i]]==-)
{
first[a[i]] = ;
fnum[i]++;
}
}
for(int i=n;i>=;i--)
{
lnum[i] = lnum[i+];
if(last[a[i]]==-)
{
last[a[i]] = ;
lnum[i]++;
}
}
long long ans = ;
for(int i=;i<=n;i++)
{
if(fnum[i]-fnum[i-]==)
{
ans += lnum[i+];
}
}
printf("%lld\n",ans);
return ;
}
Codeforces Round #495 (Div. 2) C. Sonya and Robots的更多相关文章
- Codeforces Round #495 (Div. 2) D. Sonya and Matrix
http://codeforces.com/contest/1004/problem/D 题意: 在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离. ...
- Codeforces Round #495 (Div. 2) Sonya and Matrix
正常没有正方形的限制下,值为i的点个数4i 那么从0开始遍历,第一个不为4i的值就是min(x, y) 由于对称性我们姑且令x为这个值 我们先列举n*m=t的各种情况 对于一对n, m.我们已经知道n ...
- Codeforces Round #495 (Div. 2) B
题目链接:http://codeforces.com/contest/1004/problem/B B. Sonya and Exhibition time limit per test 1 seco ...
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries 水题
C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries —— 二进制压缩
题目链接:http://codeforces.com/contest/714/problem/C C. Sonya and Queries time limit per test 1 second m ...
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]
C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)
https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...
随机推荐
- date格式互转
+"%Y/%m/%d-%H:%M:%S" date -d "2017/11/21 17:02:09" +%s
- maven学习之pom.xml或settings.xml对nexus的配置(转)
(1)在POM中配置Nexus仓库 <project> ... <repositories> <repository ...
- cmd运行java程序---路径容易出错的问题
初学者在首次使用cmd运行java程序时面临着很多的问题,重要的基本为“设置环境变量过程”与运行过程中的“路径出错问题”.由于环境变量设置的网络分享更多,且为大众情况,因此比较容易解决! 由于本人 ...
- ubuntu系统上如何添加新的根证书
如果自己部署了一个CA系统,或者使用openssl生成了一个自签名的证书,如何让ubuntu系统信任这些证书呢 添加证书: 首先,复制pem格式的根证书,重命名为 .crt格式 然后,执行下边的命令 ...
- python 发送无附件邮件
import smtplibimport tracebackfrom email.mime.text import MIMETextfrom config.config import * ...
- 【数据可视化-Echarts】Echart基础
林峰老师Echart基础课程笔记
- winfrom弹出窗口用timer控件控制倒计时20秒后关闭
功能描述: 因为在程序退出时需要确认是否是误操作,所以加了密码输入的子窗体,子窗体在20秒内会自动关闭 代码如下: private int count; private void Form2_Load ...
- jupyter notebook安装/代码补全/支持golang 踩坑记
安装(不要用root) 安装anaconda3,然后ln -s bin目录下的jupyter命令到/usr/bin目录下 生成密码备用 敲ipython进入交互终端 In [1]: from note ...
- Python3学习之路~5.13 re模块 正则表达式
re模块用于对python的正则表达式的操作. 常用正则表达式符号 字符数字: . 匹配除换行符以外的任意字符,即[^\n] \s 匹配任意空白符(如\t.\n.\r ) \S 匹配任意非空白符 \w ...
- Java-idea-mybatis plugin插件使用
方案一.免费插件[推荐] Free Mybatis plugin 方案二.破解插件 安装路径 File→Setting→plugin→Install plugin 搜索需要插件即可 搜索Mybati ...