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)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...
随机推荐
- 修复ubuntu引导
1. 插入ubuntu光盘启动临时ubuntu 2. ctrl alt t 进入命令行 3. 过程如下
- openlayers研究
openlayers研究 待定........ 在 url encode 中 + 和 %20 都表示空格, encode(+)会变成空格 如果要传 + 这个字符需要转义 1. + URL 中+号表示 ...
- PAT1018 Public Bike Management【dfs】【最短路】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805489282433024 题意: 给定一个图,一个目的地和每个节 ...
- Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- Lint found fatal errors while assembling a release target
1.Android 打包错误信息 Generate signed Bundle or APK 打包时,报了一个错,错误信息如下: Error:Execution failed for task ´: ...
- docker基本知识
- VoiceXML简介
简单来说,VoiceXML就是语音网络世界的HTML,一种用于语音应用的开放标准的标记语言.VoiceXML的问世使得为HTML发展起来的web体系也能够轻松地创建和使用语音应用. 发展历史: 199 ...
- 基于usb4java的usb通讯
下载java API及lib库地址:http://usb4java.org/index.html 1.导入所需要的库: 2.添加配置文件:文件名:javax.usb.properties:内容:jav ...
- Windows 下运行Makefile文件
下载并安装Microsoft Visual Studio2017 配置环境变量: 计算机右击-属性-高级系统设置-环境变量-选择Path编辑-添加nmake的路径: D:\Microsoft Visu ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...