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

/*
LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 set启发式合并 题目中给定的代码意思求相同的数中间隔最小的值。 那么维护n个set就好
合并时把小的向大的上暴力合并 用了map所以不用离散化
*/
#include <iostream>
#include <cstdio>
#include <set>
#include <map> #define INF 2147483647 void read (int &now)
{
register char word = getchar ();
int temp = ;
for (; !isdigit (word); word = getchar ())
if (word == '-')
temp = ;
for (now = ; isdigit (word); now = now * + word - '', word = getchar ());
if (temp)
now = -now;
} std :: map <int, std :: set <int> > Need;
int N, M;
int Answer = INF; inline int min (int a, int b)
{
return a < b ? a : b;
} inline void Insert (int pos, int x)
{
std :: set <int> :: iterator now = Need[pos].lower_bound (x);
if (now != Need[pos].end ())
Answer = min (Answer, *now - x);
if (now != Need[pos].begin ())
Answer = min (Answer, x - *-- now);
Need[pos].insert (x);
} int main (int argc, char *argv[])
{
read (N), read (M);
int x, y;
for (int i = ; i <= N; i ++)
{
read (x);
Insert (x, i);
} for (; M; -- M)
{
read (x), read (y); if (x != y)
{
if (Need[x].size () > Need[y].size ())
std :: swap (Need[x], Need[y]);
for (std :: set <int> :: iterator i = Need[x].begin (); i != Need[x].end (); ++ i)
Insert (y, *i);
Need[x].clear ();
}
printf ("%d\n", Answer);
} return ;
}

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

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

    [LOJ#516]「LibreOJ β Round #2」DP 一般看规律 试题描述 给定一个长度为 \(n\) 的序列 \(a\),一共有 \(m\) 个操作. 每次操作的内容为:给定 \(x,y\ ...

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

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

  3. 「LibreOJ#516」DP 一般看规律

    首先对于序列上一点,它对答案的贡献只有与它的前驱和后驱(前提颜色相同)构成的点对, 于是想到用set维护每个颜色,修改操作就是将2个set暴力合并(小的向大的合并),每次插入时更新答案即可 颜色数要离 ...

  4. LibreOJ #507. 「LibreOJ NOI Round #1」接竹竿

    二次联通门 : LibreOJ #507. 「LibreOJ NOI Round #1」接竹竿 /* LibreOJ #507. 「LibreOJ NOI Round #1」接竹竿 dp 记录一下前驱 ...

  5. LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力

    二次联通门 : LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 /* LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 叫做计算几 ...

  6. LibreOJ #528. 「LibreOJ β Round #4」求和

    二次联通门 : LibreOJ #528. 「LibreOJ β Round #4」求和 /* LibreOJ #528. 「LibreOJ β Round #4」求和 题目要求的是有多少对数满足他们 ...

  7. LibreOJ #527. 「LibreOJ β Round #4」框架

    二次联通门 : LibreOJ #527. 「LibreOJ β Round #4」框架 /* LibreOJ #527. 「LibreOJ β Round #4」框架 %% xxy dalao 对于 ...

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

    二次联通门 : LibreOJ #526. 「LibreOJ β Round #4」子集 /* LibreOJ #526. 「LibreOJ β Round #4」子集 考虑一下,若两个数奇偶性相同 ...

  9. LibreOJ #525. 「LibreOJ β Round #4」多项式

    二次联通门 : LibreOJ #525. 「LibreOJ β Round #4」多项式 官方题解 : /* LibreOJ #525. 「LibreOJ β Round #4」多项式 由于会有多种 ...

随机推荐

  1. ALV报表——表头实现

    ABAP实现ALV表头的DEMO: 运行效果: 代码: *********************************************************************** ...

  2. Z算法板子

    给定一个串$s$, $Z$算法可以$O(n)$时间求出一个$z$数组 $z_i$表示$s[i...n]$与$s$的前缀匹配的最长长度, 下标从$0$开始 void init(char *s, int ...

  3. The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]

    也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...

  4. DO、VO、DTO 区别

    DTO:数据传输对象,主要用于外部接口参数传递封装,接口与接口进行传递使用. VO:视图对象,主要用于给前端返回页面参数使用. DO:数据对象,主要用于数据库层传递. DTO转DO:接口接收参数将参数 ...

  5. Pytorch:module 'torch' has no attribute 'bool'

    Pytorch:module 'torch' has no attribute 'bool' 这个应该是有些版本的Pytorch会遇到这个问题,我用0.4.0版本测试发现torch.bool是有的,但 ...

  6. 如何使用async和await这对组合设计统一的取Access Token的函数

    最近我在使用SAP云平台的机器学习API做和SAP系统的集成,因为SAP Cloud Platform Leonardo上的机器学期API,每次消费时需要传一个Access Token,故在每次实际调 ...

  7. 探究Java如何实现原子操作(atomic operation)

    1. 让我们首先了解下java 中 Volatile 关键字 Volatile可实现java内存模型当中的可见性, java内存模型的可见性: 可见性,是指线程之间的可见性,一个线程修改的状态对另一个 ...

  8. Python 渗透测试编程技术方法与实践 ------全书整理

    1.整个渗透测试的工作阶段 ( 1 )前期与客户的交流阶段.( 2 )情报的收集阶段.( 3 )威胁建模阶段.( 4 )漏洞分析阶段.( 5 )漏洞利用阶段.( 6 )后渗透攻击阶段.( 7 )报告阶 ...

  9. Linux命令——getent

    简介 getent命令帮助用户administrative databases中查找相关信息.administrative databases包括: passwd – can be used to c ...

  10. Pyspark:AssertionError: dataType should be DataType

    1. schema参数,AssertionError: dataType should be DataType # AssertionError: dataType should be DataTyp ...