zoukankan      html  css  js  c++  java
  • LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律

    二次联通门 : 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 = 0;
        for (; !isdigit (word); word = getchar ())
            if (word == '-')
                temp = 1;
        for (now = 0; isdigit (word); now = now * 10 + word - '0', 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 = 1; 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
    ", Answer);
        }
        
        return 0;
    }
  • 相关阅读:
    JAVA类与对象(一)----基础概念理解
    JAVA数组
    JAVA算术运算符、关系运算符和位运算符
    JAVA数据类型
    JAVA配置环境变量
    C#中怎么生成36位的GUID
    jqAjax
    二进制1的个数
    成绩转换
    对决
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/7308544.html
Copyright © 2011-2022 走看看