[LOJ#516]「LibreOJ β Round #2」DP 一般看规律

试题描述

给定一个长度为 \(n\) 的序列 \(a\),一共有 \(m\) 个操作。

每次操作的内容为:给定 \(x,y\),序列中所有 \(x\) 会变成 \(y\)。

同时我们有一份代码:

int ans = 2147483647;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (a[i] == a[j])
ans = std::min(ans, j - i);
}
}
std::cout << ans << std::endl;

请在每次修改后输出代码运行的结果。

输入

第一行两个数,表示 \(n,m\)。

第二行 \(n\) 个数,表示 \(a_1,a_2,\cdots, a_n\)。

然后 \(m\) 行每行两个数 \(x\) 和 \(y\),表示序列中所有 \(x\) 会变成 \(y\)。

输出

对于每次修改,输出答案。

输入示例

5 10
2 7 6 3 8
6 1
7 1
1 3
5 6
1 7
9 5
1 10
7 6
7 5
3 9

输出示例

2147483647
1
1
1
1
1
1
1
1
1

数据规模及约定

\(1 \leq n, m \leq 100000\)

每个出现的数字绝对值均在int范围内。

题解

启发式合并,维护每种数字在序列中的位置,用个 set 偷一发懒。【还有 unordered_map】

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <unordered_map>
#include <set>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 100010 unordered_map <int, int> id;
set <int> pos[maxn*3];
int n, q, cnt, real[maxn*3]; struct Que {
int a, b;
Que() {}
Que(int _, int __): a(_), b(__) {}
} qs[maxn]; int main() {
n = read(); q = read();
for(int i = 1; i <= n; i++) {
int x = read();
if(!id.count(x)) id[x] = ++cnt, real[cnt] = cnt;
pos[id[x]].insert(i);
}
for(int i = 1; i <= q; i++) {
int a = read(), b = read();
qs[i] = Que(a, b);
if(!id.count(a)) id[a] = ++cnt, real[cnt] = cnt;
if(!id.count(b)) id[b] = ++cnt, real[cnt] = cnt;
} int ans = 2147483647;
for(int i = 1; i <= q; i++) {
if(qs[i].a == qs[i].b){ printf("%d\n", ans); continue; }
int a = id[qs[i].a], b = id[qs[i].b];
if(pos[real[a]].size() > pos[real[b]].size()) swap(real[a], real[b]);
a = real[a]; b = real[b];
for(auto A: pos[a]) {
set <int> :: iterator it = pos[b].lower_bound(A);
if(it != pos[b].end()) ans = min(ans, *it - A);
if(it != pos[b].begin()) ans = min(ans, A - *(--it));
pos[b].insert(A);
}
pos[a].clear();
printf("%d\n", ans);
} return 0;
}

注:代码是 C++11 的,偷懒用 unordered_map 和 auto。

总有那么一段时间天天犯 SB。。。

[LOJ#516]「LibreOJ β Round #2」DP 一般看规律的更多相关文章

  1. LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律

    二次联通门 : LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 /* LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 set ...

  2. loj516 「LibreOJ β Round #2」DP 一般看规律

    传送门:https://loj.ac/problem/516 [题解] 那段代码求的是相同的数中间隔最小的值. 离散后用set维护每个值出现次数,每次操作相当于合并两个set,这步可以启发式合并. 加 ...

  3. [LOJ#531]「LibreOJ β Round #5」游戏

    [LOJ#531]「LibreOJ β Round #5」游戏 试题描述 LCR 三分钟就解决了问题,她自信地输入了结果-- > -- 正在检查程序 -- > -- 检查通过,正在评估智商 ...

  4. [LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机)

    [LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机) 试题描述 IOI 的比赛开始了.Jsp 和 Rlc 坐在一个角落,这时他们听到了一个异样的声音 …… 接着他们发现自己收 ...

  5. [LOJ#530]「LibreOJ β Round #5」最小倍数

    [LOJ#530]「LibreOJ β Round #5」最小倍数 试题描述 第二天,LCR 终于启动了备份存储器,准备上传数据时,却没有找到熟悉的文件资源,取而代之的是而屏幕上显示的一段话: 您的文 ...

  6. [LOJ#515]「LibreOJ β Round #2」贪心只能过样例

    [LOJ#515]「LibreOJ β Round #2」贪心只能过样例 试题描述 一共有 \(n\) 个数,第 \(i\) 个数 \(x_i\) 可以取 \([a_i , b_i]\) 中任意值. ...

  7. [LOJ#525]「LibreOJ β Round #4」多项式

    [LOJ#525]「LibreOJ β Round #4」多项式 试题描述 给定一个正整数 k,你需要寻找一个系数均为 0 到 k−1 之间的非零多项式 f(x),满足对于任意整数 x 均有 f(x) ...

  8. [LOJ#526]「LibreOJ β Round #4」子集

    [LOJ#526]「LibreOJ β Round #4」子集 试题描述 qmqmqm有一个长为 n 的数列 a1,a2,……,an,你需要选择集合{1,2,……,n}的一个子集,使得这个子集中任意两 ...

  9. loj #547. 「LibreOJ β Round #7」匹配字符串

    #547. 「LibreOJ β Round #7」匹配字符串   题目描述 对于一个 01 串(即由字符 0 和 1 组成的字符串)sss,我们称 sss 合法,当且仅当串 sss 的任意一个长度为 ...

随机推荐

  1. Firefox火狐广告过滤插件Adblock Plus过滤规则包[中文维护小组]

    如果你经常使用Firefox火狐浏览器那么一定知道Adblock Plus这款广告过滤插件,功能非常强大,但是Adblock Plus广告过滤插件自带的过滤规则并不多,而且也不太适合我们中国的网站,在 ...

  2. [dp]uestc oj E - 菲波拉契数制

    E - 菲波拉契数制 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. javascript报错:ReferenceError: $ is not defined解决办法

    原因很简单,要么是未导入jquery包,要么是导入的顺序不对. 例如,我在制作Chrome扩展程序时,其中的一块代码如下: 然后运行时报上述错误. 解决方法:我们不难发现script位置有问题,因为$ ...

  4. 在.net平台上运行伪JAVA

    由于在一个项目局方要求使用JAVA平台, 而当前又都是.net平台的应用. 重新用JAVA开发工作量太大. 时间也来不及. 想到在.net中有url rewrite功能, 何不先"骗&quo ...

  5. 2018.4.1 Ubuntu16.04 下配置Tomcat服务器以及设置dingshi启动

    Tomcat自启动的设置技巧 以root用户登录系统: cd /etc/rc.d/init.d/ vi tomcat #!/bin/sh # # tomcat: Start/Stop/Restart ...

  6. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fundService': Injection of resource dependencies failed;

    在进行SSM的Controller的编写, 从浏览器访问后端Controller的时候遇到了这个问题. 这个问题的描述: 创建Bean的对象失败 错误代码如下: @Service("fund ...

  7. idea中pom.xml没有工作 IDEA中maven项目pom.xml依赖不生效解决

    问题: 今天在web项目中需要引入poi相关jar包.查看之下才发现pom.xml中的依赖虽然已经下载到了本地仓库 repository,但是却没有加入到项目路径的 Extenal Libraries ...

  8. ajax $.post 一直报 Forbidden (CSRF token missing or incorrect.)

    由于后台整合类视图代码,所以修改了写法,完了之后用下面的写法写的post请求都报 403 error $.post( "{% url 'test_record:select_node_pag ...

  9. Centos7离线部署kubernetes 1.13集群记录

    一.说明 本篇主要参考kubernetes中文社区的一篇部署文章(CentOS 使用二进制部署 Kubernetes 1.13集群),并做了更详细的记录以备用. 二.部署环境 1.kubernetes ...

  10. CentOS7的systemctl使用

    CentOS 7开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替. 1.原来的 service 命令与 sys ...