码迷,mamicode.com
首页 > 其他好文 > 详细

[POI 2014] Little Bird

时间:2018-11-05 22:25:30      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:get   复杂   时间   规划   lse   template   c++   min   .com   

[题目链接]

         https://www.lydsy.com/JudgeOnline/problem.php?id=3831

[算法]

        单调队列优化动态规划

        时间复杂度 : O(N)

[代码]

          

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1000010
const int inf = 2e9;

int n;
int a[MAXN] , f[MAXN];

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == -) f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 0;
    x *= f;
}
inline int solve(int k)
{
        deque< int > q;
        for (int i = 1; i <= n; i++) f[i] = inf;
        q.clear();
        for (int i = 1; i <= n; i++)
        {
                while (!q.empty() && i - q.front() > k) q.pop_front();
                if (q.empty()) f[i] = 0;
                else f[i] = f[q.front()] + (a[q.front()] <= a[i]);
                while (!q.empty() && ((f[i] < f[q.back()]) || ((f[i] == f[q.back()]) && a[i] >= a[q.back()]))) q.pop_back();
                q.push_back(i);
        }        
        return f[n];
}

int main()
{
        
        read(n);
        for (int i = 1; i <= n; i++) read(a[i]);
        int T;
        read(T);
        while (T--)
        {
                int x;
                read(x);
                printf("%d\n" , solve(x));
        }
        
        return 0;
    
}

 

[POI 2014] Little Bird

标签:get   复杂   时间   规划   lse   template   c++   min   .com   

原文地址:https://www.cnblogs.com/evenbao/p/9911902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!