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}的一个子集,使得这个子集中任意两 ...
随机推荐
- LintCode-70.二叉树的层次遍历 II
二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 按照 ...
- <Android>tab选项卡
1.继承TabActivity实现 a) 在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件 b) Activity要继承TabActivity c ...
- 微信抢红包软件-android
微信红包不错的分析: 附带源码 并包含了源码 参考: Android中微信抢红包助手的实现 (1) https://www.jianshu.com/p/19ddd41aa349 (2) http:// ...
- 第25天:js-封装函数-淘宝鼠标展示
封装函数: 1.函数形参相当于变量,不能加引号. 2.实参要和形参一一对应. 案例:鼠标移到小图上,背景展示相应放大的图片.代码如下: <!DOCTYPE html> <html l ...
- 文件上传C:\fakepath\解决方案
1.设置IE:工具 -> Internet选项 -> 安全 -> 自定义级别 -> 找到“其他”中的“将本地文件上载至服务器时包含本地目录路径”,选中“启用”即可 2.利用js ...
- CodeForces 632E Thief in a Shop
题意:给你n种物品,每种无限个,问恰好取k个物品能组成哪些重量.n<=1000,k<=1000,每种物品的重量<=1000. 我们搞出选取一种物品时的生成函数,那么只要对这个生成函数 ...
- hdu 1068 Girls and Boys (二分匹配)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- C#基础-连接Access与SQL Server
1.连接Access数据库 string strConnection = "Provider=Microsoft.Ace.OleDb.12.0; Data Source=" + S ...
- [Leetcode] search in rotated sorted array ii 搜索旋转有序数组
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- POJ. 1005 I Think I Need a Houseboat(水 )
POJ. 1005 I Think I Need a Houseboat(水 ) 代码总览 #include <cstdio> #include <cstring> #incl ...