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)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...
随机推荐
- JavaScript——this
一.JavaScript 函数与方法 <body> <script type="text/javascript"> function show(){ con ...
- python语法_字符类型
str(string): 字符串 str(被转换的数据) int(interger):整数 int(被转换的数据)
- JavaScript将字典序升序排列类似php中的ksort函数
/** * 将json数据进行排序 * @param {*jason} data */ export function JsonSort(jsonData) { try { let tempJsonO ...
- jQuery 实现点击页面其他地方隐藏菜单
点击页面其它地方隐藏id为messageList的div 代码: $('body').delegate("#message", 'click', function(e) { var ...
- 解决web资源跨域请求问题
参考地址: http://my.oschina.net/lichaoqiang/blog/317823 在浏览器请求中,出现跨域访问资源的问题,我们肯定会遇到.如果跨域请求被阻止,有可能导致css.j ...
- ADC裸机程序
硬件平台:JZ2440 实现功能:通过采集触摸屏ADC的电压值,推算触摸xy坐标 start.s init.c nand.c interrupt.c uart.c uart.h my_stdio.c ...
- Redis入门到高可用(十六)—— 持久化
一.持久化概念 二.持久化方式 三.redis持久化方式之——RDB 1.什么是RDB 在 Redis 运行时, RDB 程序将当前内存中的数据库快照保存到磁盘文件中, 在 Redis 重启动时, R ...
- nginx配置虚拟主机vhost的方法详解
Nginx vhost配置,可实现基于ip.端口号.servername的虚拟主机,同时可避免直接修改主配置文件.在nginx下配置虚拟主机vhost非常方便.这篇文章主要介绍了nginx配置虚拟主机 ...
- cocos2d-x 编译 安卓(android)apk文件
摘要: 一.下载Android环境 搭建Android环境需要用到Android SDK.NDK.Ant和JDK: 下载Android SDK 下载Android NDk 下载Android JD ...
- nginx----------nginx日志详细分解
1.客户端(用户)IP地址.如:上例中的 47.52.45.228 2.访问时间.如:上例中的 [03/Jan/2013:21:17:20 -0600] 3.请求方式(GET或者POST等).如:上例 ...