用数组模拟队列速度会更快一点。

一、Fence POJ - 1821:
A team of k (1 <= K <= 100) workers should paint a fence which contains N (1 <= N <= 16 000) planks numbered from 1 to N from left to right. Each worker i (1 <= i <= K) should sit in front of the plank Si and he may paint only a compact interval (this means that the planks from the interval should be consecutive). This interval should contain the Si plank. Also a worker should not paint more than Li planks and for each painted plank he should receive Pi $ (1 <= Pi <= 10 000). A plank should be painted by no more than one worker. All the numbers Si should be distinct.

Being the team’s leader you want to determine for each worker the interval that he should paint, knowing that the total income should be maximal. The total income represents the sum of the workers personal income.

Write a program that determines the total maximal income obtained by the K workers.
Input
The input contains:
Input

N K
L1 P1 S1
L2 P2 S2

LK PK SK

Semnification

N -the number of the planks; K ? the number of the workers
Li -the maximal number of planks that can be painted by worker i
Pi -the sum received by worker i for a painted plank
Si -the plank in front of which sits the worker i
Output
The output contains a single integer, the total maximal income.
Sample Input
8 4
3 2 2
3 2 3
3 3 5
1 1 7
Sample Output
17
Hint
Explanation of the sample:

the worker 1 paints the interval [1, 2];

the worker 2 paints the interval [3, 4];

the worker 3 paints the interval [5, 7];

the worker 4 does not paint any plank

题意:有N块木板从左往右排成一行,有M个工匠对这些木板分别进行粉刷,每块木板至多被粉刷一次。第 i 个工匠要么不粉刷,要么粉刷包含木板 si 的,长度不超过 li 的连续的一段木板。
每粉刷一块可以得到 pi 的报酬,求如何安排能使工匠门得到的总报酬最多。

设F(i,j)表示前 i 个工匠,粉刷前 j 块木板(可以有空着的),工匠们能获得的最大的报酬。
状态转移方程显然,在单调队列优化dp的模型中,val(i,j) 仅与 j 有关。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
#include<deque>
#define ll long long
#define llu unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=100100;
int n,m;
int f[110][16100];
struct node
{
    int si,li,pi;
}a[110];

int cal(int i,int k)
{
    return f[i-1][k]-a[i].pi*k;
}
bool cmp(const node &a,const node &b)
{
    return a.si<b.si;
}

int main(void)
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=m;i++)
            scanf("%d%d%d",&a[i].li,&a[i].pi,&a[i].si);

        memset(f,0,sizeof(f));
        sort(a+1,a+m+1,cmp);

        deque<int>q;
        for(int i=1;i<=m;i++)
        {
            q.clear();
            for(int k=max(0,a[i].si-a[i].li);k<=a[i].si-1;k++)
            {
                while(q.size()&&cal(i,q.back())<=cal(i,k)) q.pop_back();
                q.push_back(k);

            }

            for(int j=1;j<=n;j++)
            {
                f[i][j]=max(f[i][j],f[i-1][j]);
                f[i][j]=max(f[i][j],f[i][j-1]);

                if(j>=a[i].si)
                {
                    while(q.size()&&q.front()<j-a[i].li) q.pop_front();
                    if(q.size()) f[i][j]=max(f[i][j],cal(i,q.front())+a[i].pi*j);
                }
            }


        }
        printf("%d\n",f[m][n]);
    }
    return 0;
}

二、Cut the Sequence POJ - 3017:
Given an integer sequence { an } of length N, you are to cut the sequence into several parts every one of which is a consecutive subsequence of the original sequence. Every part must satisfy that the sum of the integers in the part is not greater than a given integer M. You are to find a cutting that minimizes the sum of the maximum integer of each part.

Input
The first line of input contains two integer N (0 < N ≤ 100 000), M. The following line contains N integers describes the integer sequence. Every integer in the sequence is between 0 and 1 000 000 inclusively.

Output
Output one integer which is the minimum sum of the maximum integer of each part. If no such cuttings exist, output −1.

Sample Input
8 17
2 2 2 8 1 8 2 1
Sample Output
12
Hint
Use 64-bit integer type to hold M.

mdzz,用二叉堆调了一晚上(一定是我太弱了),结果一发暴力过了。。。。
暴力出奇迹。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
#include<deque>
#include<queue>
#define ll long long
#define llu unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=100100;
int n;
ll dp[maxn],a[maxn],sum,m;
int c[maxn];

int main(void)
{
    while(scanf("%d%lld",&n,&m)!=EOF)
    {

        int pos=0;
        bool flag=false;
        sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            if(a[i]>m) flag=true;
            sum+=a[i];
            while(sum>m) sum-=a[++pos];
            c[i]=pos;
        }
        if(flag)
        {
            printf("-1\n");
            continue;
        }

        deque<int>q;
        memset(dp,0x3f,sizeof(dp));
        dp[0]=0;

        for(int i=1;i<=n;i++)
        {
            //cout<<c[i]<<endl;

            while(q.size()&&q.front()<=c[i])
                q.pop_front();

            while(q.size()&&a[i]>=a[q.back()])
                q.pop_back();

            q.push_back(i);

            dp[i]=min(dp[i],dp[c[i]]+a[q.front()]);

            for(int j=0;j<q.size()-1;j++)
                dp[i]=min(dp[i],dp[q[j]]+a[q[j+1]]);

        }

        printf("%lld\n",dp[n]);
    }
    return 0;
}

三、单调队列优化多重背包。
ACWing
时间复杂度O(N * M)。
有 N 种物品和一个容量是 V 的背包。

第 i 种物品最多有 si 件,每件体积是 vi,价值是 wi。

求解将哪些物品装入背包,可使物品体积总和不超过背包容量,且价值总和最大。
输出最大价值。

输入格式
第一行两个整数,N,V (0<N≤1000, 0<V≤20000),用空格隔开,分别表示物品种数和背包容积。

接下来有 N 行,每行三个整数 vi,wi,si,用空格隔开,分别表示第 i 种物品的体积、价值和数量。

输出格式
输出一个整数,表示最大价值。

数据范围
0<N≤1000
0<V≤20000
0<vi,wi,si≤20000
提示
本题考查多重背包的单调队列优化方法。

输入样例
4 5
1 2 3
2 4 1
3 4 3
4 5 2
输出样例:
10

用STL deque T了
用数组模拟过了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
#include<deque>
#include<queue>
#define ll long long
#define llu unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=20100;
int f[maxn],v[1100],w[1100],s[1100];
int q[maxn];

int cal(int i,int u,int k)
{
    return f[u+k*v[i]]-k*w[i];
}

int main(void)
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(f,0x80,sizeof(f));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&v[i],&w[i],&s[i]);
        }


        f[0]=0;
        for(int i=1;i<=n;i++)
        {
            for(int u=0;u<v[i];u++)
            {
                int l=1,r=0;
                int maxp=(m-u)/v[i];
                for(int k=maxp-1;k>=max(maxp-s[i],0);k--)
                {
                    while(l<=r&&cal(i,u,q[r])<=cal(i,u,k)) r--;
                    q[++r]=k;
                }

                for(int p=maxp;p>=0;p--)
                {
                    while(l<=r&&q[l]>p-1) l++;
                    if(l<=r)
                        f[u+p*v[i]]=max(f[u+p*v[i]],cal(i,u,q[l])+p*w[i]);
                    if(p-s[i]-1>=0)
                    {
                        while(l<=r&&cal(i,u,q[r])<=cal(i,u,p-s[i]-1)) r--;
                        q[++r]=p-s[i]-1;
                    }

                }
            }
        }
        int ans=0;
        for(int i=1;i<=m;i++)
            ans=max(ans,f[i]);
        printf("%d\n",ans);
    }
    return 0;
}