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的更多相关文章

  1. Codeforces Round #495 (Div. 2) D. Sonya and Matrix

    http://codeforces.com/contest/1004/problem/D 题意: 在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离. ...

  2. Codeforces Round #495 (Div. 2) Sonya and Matrix

    正常没有正方形的限制下,值为i的点个数4i 那么从0开始遍历,第一个不为4i的值就是min(x, y) 由于对称性我们姑且令x为这个值 我们先列举n*m=t的各种情况 对于一对n, m.我们已经知道n ...

  3. Codeforces Round #495 (Div. 2) B

    题目链接:http://codeforces.com/contest/1004/problem/B B. Sonya and Exhibition time limit per test 1 seco ...

  4. 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 ...

  5. Codeforces Round #371 (Div. 2) C. Sonya and Queries 水题

    C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)

    https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...

随机推荐

  1. vue3版本到vue2版本的桥接工具

    vue2的命令可以正常使用.

  2. Java基础&面向对象(二)

    (七)函数 1.数的定义:具有特定功能的一段小程序,也称为方法: 2.函数的特点: 3.函数的应用:结果.是否需要未知内容参与运算: 4.函数的重载:在同一个类中,允许存在一个以上的同名函数,只要它们 ...

  3. react子传父

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. vue脚手架---vue-cli

    开年第一篇 今天先讲一讲 vue-cli的安装 npm install vue-cli 可能需要很多的时间视网络环境而定, 如果长时间等待 也可以试试使用淘宝的镜像(cnpm)安装( npm inst ...

  5. windows环境下MySQL mysql-5.7.17-winx64 (社区服务版,community server)安装教程

    根据网上查询的资料显示,MySQ在版本5.7开始根目录下没有dada目录,需要额外的“初始化数据库”的操作自动生成data目录. 1.    下载地址: https://cdn.mysql.com// ...

  6. java动态加载配置文件(申明:来源于网络)

    java动态加载配置文件 地址:http://blog.csdn.net/longvs/article/details/9361449

  7. mysql in 排序 也可以按in里面的顺序来排序

    SQL: select * from table where id IN (3,9,6);这样的情况取出来后,其实,id还是按3,6,9,排序的,但如果我们真要按IN里面的顺序排序怎么办?SQL能不能 ...

  8. LeetCode 521 Longest Uncommon Subsequence I 解题报告

    题目要求 Given a group of two strings, you need to find the longest uncommon subsequence of this group o ...

  9. pip升级包错误问题解决

    命令框内输入 sudo pip install six --upgrade --ignore-installed six --红色字体表示想要忽略的包名称--

  10. 学习gstreamer

    1. 对gst 的框架认识. 第一篇文章有结构图说明,清楚易懂:第二篇文章介绍了gst的简单使用 http://www.cnblogs.com/jingzhishen/p/3709639.html h ...