[LOJ#516]「LibreOJ β Round #2」DP 一般看规律
[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 一般看规律的更多相关文章
- LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律
二次联通门 : LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 /* LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 set ...
- loj516 「LibreOJ β Round #2」DP 一般看规律
传送门:https://loj.ac/problem/516 [题解] 那段代码求的是相同的数中间隔最小的值. 离散后用set维护每个值出现次数,每次操作相当于合并两个set,这步可以启发式合并. 加 ...
- [LOJ#531]「LibreOJ β Round #5」游戏
[LOJ#531]「LibreOJ β Round #5」游戏 试题描述 LCR 三分钟就解决了问题,她自信地输入了结果-- > -- 正在检查程序 -- > -- 检查通过,正在评估智商 ...
- [LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机)
[LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机) 试题描述 IOI 的比赛开始了.Jsp 和 Rlc 坐在一个角落,这时他们听到了一个异样的声音 …… 接着他们发现自己收 ...
- [LOJ#530]「LibreOJ β Round #5」最小倍数
[LOJ#530]「LibreOJ β Round #5」最小倍数 试题描述 第二天,LCR 终于启动了备份存储器,准备上传数据时,却没有找到熟悉的文件资源,取而代之的是而屏幕上显示的一段话: 您的文 ...
- [LOJ#515]「LibreOJ β Round #2」贪心只能过样例
[LOJ#515]「LibreOJ β Round #2」贪心只能过样例 试题描述 一共有 \(n\) 个数,第 \(i\) 个数 \(x_i\) 可以取 \([a_i , b_i]\) 中任意值. ...
- [LOJ#525]「LibreOJ β Round #4」多项式
[LOJ#525]「LibreOJ β Round #4」多项式 试题描述 给定一个正整数 k,你需要寻找一个系数均为 0 到 k−1 之间的非零多项式 f(x),满足对于任意整数 x 均有 f(x) ...
- [LOJ#526]「LibreOJ β Round #4」子集
[LOJ#526]「LibreOJ β Round #4」子集 试题描述 qmqmqm有一个长为 n 的数列 a1,a2,……,an,你需要选择集合{1,2,……,n}的一个子集,使得这个子集中任意两 ...
- loj #547. 「LibreOJ β Round #7」匹配字符串
#547. 「LibreOJ β Round #7」匹配字符串 题目描述 对于一个 01 串(即由字符 0 和 1 组成的字符串)sss,我们称 sss 合法,当且仅当串 sss 的任意一个长度为 ...
随机推荐
- 通过Java代码实现图片的放大和缩小
本文介绍的例子在Android安卓手机上测试通过. 先看看效果吧.可以看到这个开发好的安卓应用有三个按钮:Zoom In缩小图片,Zoom Out放大图片和Save保存. 初始页面: 可以在左边边框自 ...
- 关于火狐浏览器在ubuntu和安卓手机上的同步
最近在ubuntu使用火狐浏览器,感觉还不错.我想着,如果在我的安卓手机上装一个火狐浏览器,我就可以在手机上查看电脑上所收藏的网站了.然后我就去安卓应用市场下载了最新版的火狐浏览器.令人奇怪的是,我在 ...
- UI的组织形式
UI的组织形式是树状结构: 根据层次的不同分为叶子节点和干节点. 叶子节点负责简单的信息展示. 复杂的主干复杂叶子节点的组织和整体展示. http://www.cnblogs.com/feng9exe ...
- JS的闭包、高阶函数、柯里化
本文原链接:https://cloud.tencent.com/developer/article/1326958 https://cloud.tencent.com/developer/articl ...
- leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树
不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...
- 厌食?暴食?试试这个 VR 新疗法
今日导读 “我知道我要吃饭,但我真的什么都吃不下.” “我脑子里想的只有吃东西,吃吃吃!” ....... 作为一个正常人,我们完全无法想象患厌食症或贪食症人群所受的痛苦.长期的厌食,会使一个人瘦的只 ...
- JavaScript -- 条件语句和循环语句
if语句 在我们开发程序的时候,经常会遇到选择题,例如,年龄大于18,你就可以抽烟喝酒烫头,年龄小于18,你就只能吃饭喝水.在我们的代码中,我们可以用if语句来实现这种判断 语法一: if( cond ...
- Python中文编码问题(字符串前面加'u')
中文编码问题是用中文的程序员经常头大的问题,在python下也是如此,那么应该怎么理解和解决python的编码问题呢? 我们要知道python内部使用的是unicode编码,而外部却要面对千奇百怪的各 ...
- iOS 解决ipv6问题
解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...
- 洛谷 P2568 GCD
https://www.luogu.org/problemnew/show/P2568#sub 最喜欢题面简洁的题目了. 本题为求两个数的gcd是素数,那么我们将x和y拆一下, 假设p为$gcd(x, ...