zoukankan      html  css  js  c++  java
  • HDU3530【STL/单调队列/RMQ】

    题目链接【http://acm.hdu.edu.cn/showproblem.php?pid=3530】

    题意:输入n,m,k;n代表n个点,在这n(n<100000)个点中找到最长的连续子序列,使得这段子序列中最大值与最小值的差在在区间[m,k]内;

    解法一:multiset(STL)

      首先是复杂度,set和multiset的时间复杂度是n*log(n),十万个点可以接受。set里面可以自动排序,因为有重复的值,所以要用到multiset;

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 100050;
    int n, m, k;
    int a[maxn];
    int main ()
    {
        while(~scanf("%d%d%d", &n, &m, &k))
        {
            multiset<int>st;//写到main()函数里面,结束后会被析构掉
            int pos = 1, ans = 0;
            for(int i = 1; i <= n; i++)
            {
                scanf("%d", &a[i]);
                st.insert(a[i]);
                while(*st.rbegin() - *st.begin() > k) st.erase(a[pos++]);
                if(*st.rbegin() - *st.begin() >= m) ans = max(ans, i - pos + 1);
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    想的太多,做的太少。
  • 相关阅读:
    Single Number II
    Pascal's Triangle
    Remove Duplicates from Sorted Array
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Unique Paths
    Sort Colors
    Swap Nodes in Pairs
    Merge Two Sorted Lists
    Climbing Stairs
  • 原文地址:https://www.cnblogs.com/pealicx/p/6406502.html
Copyright © 2011-2022 走看看