loj516 「LibreOJ β Round #2」DP 一般看规律
传送门:https://loj.ac/problem/516
【题解】
那段代码求的是相同的数中间隔最小的值。
离散后用set维护每个值出现次数,每次操作相当于合并两个set,这步可以启发式合并。
加元素的时候直接找前驱和后继即可。
学了新姿势:set中insert有返回的,可以访问.first来调用新插入元素的iterator
# include <set>
# include <vector>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5 + , M = 3e5 + ;
const int mod = 1e9+, inf = ; int n, m, a[N];
struct quest {
int x, y;
}q[N];
vector<int> ps;
set<int> s[M];
set<int>::iterator it, it2;
int ans[M], Ans = inf, id[M]; inline int getid(int x) {
return lower_bound(ps.begin(), ps.end(), x) - ps.begin() + ;
} int main() {
cin >> n >> m;
for (int i=; i<=n; ++i) {
scanf("%d", a+i);
ps.push_back(a[i]);
}
for (int i=; i<=m; ++i) {
scanf("%d%d", &q[i].x, &q[i].y);
ps.push_back(q[i].x); ps.push_back(q[i].y);
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
for (int i=; i<=n; ++i) a[i] = getid(a[i]);
for (int i=; i<=m; ++i) q[i].x = getid(q[i].x), q[i].y = getid(q[i].y);
for (int i=; i<=n; ++i) s[a[i]].insert(i);
for (int i=; i<=ps.size(); ++i) {
ans[i] = inf; id[i] = i;
if(s[i].size() < ) continue;
for (it = s[i].begin(), it2 = ++s[i].begin(); it2 != s[i].end(); ++it, ++it2)
ans[i] = min(ans[i], *it2 - *it);
Ans = min(Ans, ans[i]);
}
for (int i=, X, Y, x, tans; i<=m; ++i) {
X = q[i].x, Y = q[i].y;
if(!s[id[X]].size()) {
printf("%d\n", Ans);
continue;
}
if(!s[id[Y]].size()) {
swap(id[X], id[Y]);
printf("%d\n", Ans);
continue;
}
tans = min(ans[id[X]], ans[id[Y]]);
if(s[id[X]].size() > s[id[Y]].size()) swap(id[X], id[Y]);
for (it = s[id[X]].begin(); it != s[id[X]].end(); ++it) {
x = *it;
it2 = s[id[Y]].insert(x).first;
++it2;
if(it2 != s[id[Y]].end()) tans = min(tans, *it2 - x);
--it2;
if(it2 != s[id[Y]].begin()) --it2, tans = min(tans, x - *it2);
}
s[id[X]].clear();
ans[id[Y]] = tans;
ans[id[X]] = inf;
Ans = min(Ans, tans);
printf("%d\n", Ans);
}
return ;
}
loj516 「LibreOJ β Round #2」DP 一般看规律的更多相关文章
- [LOJ#516]「LibreOJ β Round #2」DP 一般看规律
[LOJ#516]「LibreOJ β Round #2」DP 一般看规律 试题描述 给定一个长度为 \(n\) 的序列 \(a\),一共有 \(m\) 个操作. 每次操作的内容为:给定 \(x,y\ ...
- LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律
二次联通门 : LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 /* LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 set ...
- loj #547. 「LibreOJ β Round #7」匹配字符串
#547. 「LibreOJ β Round #7」匹配字符串 题目描述 对于一个 01 串(即由字符 0 和 1 组成的字符串)sss,我们称 sss 合法,当且仅当串 sss 的任意一个长度为 ...
- [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}的一个子集,使得这个子集中任意两 ...
随机推荐
- Httpd主配置文件httpd.conf 详解
Apache的主配置文件:/etc/httpd/conf/httpd.conf默认站点主目录:/var/www/html/Apache服务器的配置信息全部存储在主配置文件/etc/httpd/conf ...
- LINUX硬件查看命令
1.查看系统PCI设备 lspci lspci -v 显示更详细的PCI设备信息 2.查看CPU信息 more / proc /cpuinfo 3.查看系统内存信息 more /proc /mem ...
- BIO、NIO、AIO通信机制
一.BIO的理解 首先我们通过通信模型图来熟悉下BIO的服务端通信模型:采用BIO通信模型的服务端,通常由一个独立的Acceptor线程负责监听客户端的连接,它接收到客户端的连接请求之后为每个客户端创 ...
- CMD (sea.js)模块定义规范
转自http://www.cnblogs.com/hongchenok/p/3685677.html CMD 模块定义规范 在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(C ...
- WPF 分页控件的实现 -用户控件
效果图:
- 【Python】关于Python里面小数点精度控制的问题
基础 浮点数是用机器上浮点数的本机双精度(64 bit)表示的.提供大约17位的精度和范围从-308到308的指数.和C语言里面的double类型相同.Python不支持32bit的单精度浮点数.如果 ...
- Python 静态方法、类方法和属性方法
Python 静态方法.类方法和属性方法 静态方法(staticmethod) staticmethod不与类或者对象绑定,类和实例对象都可以调用,没有自动传值效果,Python内置函数staticm ...
- zlog使用手册
zlog使用手册 来源 http://hardysimpson.github.io/zlog/UsersGuide-CN.html Contents Chapter 1 zlog是什么? 1.1 ...
- [luogu1654]OSU!
update 9.20:本篇题解已经被\(yyb\)证明是出锅的 这道题目最后的式子看上去是很简单的,不到10行就码完了,但是求式子的过程并没有那么简单. 很容易想到一种枚举思路: 因为每一段连续的1 ...
- POJ2826:An Easy Problem?!——题解(配特殊情况图)
http://poj.org/problem?id=2826 题目大意:给两条线,让它接竖直下的雨,问其能装多少横截面积的雨. ———————————————————————————— 水题,看题目即 ...