[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. Round #322 (Div. 2) 581D Three Logos (模拟)

    先枚举两个矩形,每个矩形横着放或竖着放,把一边拼起来, 如果不是拼起来有缺口就尝试用第三个矩形去补. 如果没有缺口就横着竖着枚举一下第三个矩形和合并的矩形x或y拼接. #include<bits ...

  2. UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)

    大体思路是从终点反向做一次BFS得到一个层次图,然后从起点开始依次向更小的层跑,跑的时候选则字典序最小的,由于可能有多个满足条件的点,所以要把这层满足条件的点保存起来,在跑下一层.跑完一层就会得到这层 ...

  3. [神经网络]一步一步使用Mobile-Net完成视觉识别(五)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第五篇,讲解如何调用测试训练结果. 上一篇中我们输出了训练的模型,这一篇中我们通过调用训练好的模型来完 ...

  4. [Java] 新手快速就业需要掌握的知识点

    目的:主要是分享下日常工作中使用到的技术点,根据二八定律快速掌握使用知识点,先就业再沉淀去积累经验.(个人建议仅供参考) 背景:目前一般来说,都是前后端分离.你只需要提供接口给前端,他来处理就可以了, ...

  5. VA助手添加扩展文件后缀名

    Allow C/C++ files with a non-standard extension Follow these steps to make Visual Assist consider as ...

  6. Ajax的原理及Django上传组件

    title: Ajax的原理及Django上传组件 tags: Django --- Ajax的原理及Django上传组件 Ajax的原理 ajax 是异步JavaScript和xml ajax就是向 ...

  7. CPP-基础:c++读取ini文件

    配置文件格式是[JP]K=2EC156673E 2F4240 5595F6 char str[50];GetPrivateProfileString("JP", "K&q ...

  8. NFS缓存IO机制

    NFS的缓存IO机制<一> async 参数模式下分析 NFS 默认的mount参数为async,async 参数表示内核不会透传程序的IO请求给sever,对于写IO会延迟执行,积累一定 ...

  9. Codeforces Round 513 (Div.1+Div.2)

    比赛传送门 10月4号的比赛,因为各种原因(主要是懒),今天才写总结-- Div1+Div2,只做出两个题+迟到\(20min\),日常掉\(rating\)-- \(\rm{A.Phone\;Num ...

  10. GCD和NSThread延时执行对比

    1.NSThread: [self performSelector:@selector(performSome:) withObject:self afterDelay:.f]; [[self cla ...