写在前面:
一般的,只要决策的候选集合只扩大,不缩小,我们就可以仅用一个变量维护最值,不断与新加入候选集合的元素比较,即可直接得到最优决策,O(1)的进行转移。
在更加复杂的情况下,我们就要用更加高级的数据结构,而不仅仅是一个变量维护dp的决策候选集合,以便快速执行插入元素,删除元素,查询最值等操作,把朴素的在取值范围中枚举的时间优化为维护数据结构的时间。

一、Cleaning Shifts POJ - 2376:
Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.
Input

  • Line 1: Two space-separated integers: N and T

  • Lines 2…N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.
    Output

  • Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.
    Sample Input
    3 10
    1 7
    3 6
    6 10
    Sample Output
    2
    Hint
    This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

INPUT DETAILS:

There are 3 cows and 10 shifts. Cow #1 can work shifts 1…7, cow #2 can work shifts 3…6, and cow #3 can work shifts 6…10.

OUTPUT DETAILS:

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

题意:有一条很长的白色丝带,被划分成一个个长度为1的网格,其中第L到第R个网格不慎被染上了墨水,现在有N张贴纸,第 i 张贴纸可以覆盖 ai 到 bi 个格子,售价为 ci ,求用若干条贴纸覆盖纸带上被染了墨水的格子的最少花费。

题意可能略有出入,但是确实是这么一类题。

设 F(x)为覆盖[L,x]的最小花费,很容易列出状态转移方程。
F(bi)=min{ F(x)} + ci,其中 ai-1<= x <bi

这是一个带有修改的区间最值,可以用线段树维护。
本题中网格位置都很小,可以直接在[L-1,R] 上建立线段树,当坐标较大时,也可以先离散化再建立线段树。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define ll long long
#define llu unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int maxn=1001000;
ll dp[maxn];
struct nn
{
    ll l,r,c;
}a[maxn];
struct node
{
    int l,r;
    ll minn;
}t[maxn<<2];

void pushup(int cnt)
{
    t[cnt].minn=min(t[cnt<<1].minn,t[cnt<<1|1].minn);
}

void build(int l,int r,int cnt)
{
    t[cnt].l=l,t[cnt].r=r;
    if(l==r)
    {
        if(l==1) t[cnt].minn=0;
        else t[cnt].minn=lnf;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,cnt<<1);
    build(mid+1,r,cnt<<1|1);
    pushup(cnt);
}

void change(int pos,ll val,int cnt)
{
    if(t[cnt].l==t[cnt].r)
    {
        t[cnt].minn=val;
        return ;
    }
    if(pos<=t[cnt<<1].r) change(pos,val,cnt<<1);
    else change(pos,val,cnt<<1|1);
    pushup(cnt);
}

ll ask(int l,int r,int cnt)
{
    if(l<=t[cnt].l&&t[cnt].r<=r)
    {
        return t[cnt].minn;
    }

    ll ans=lnf;
    if(l<=t[cnt<<1].r) ans=min(ans,ask(l,r,cnt<<1));
    if(r>=t[cnt<<1|1].l) ans=min(ans,ask(l,r,cnt<<1|1));
    return ans;
}

bool cmp(const nn &a,const nn &b)
{
    return a.r<b.r;
}

int main(void)
{
    int n,t;
    while(scanf("%d%d",&n,&t)!=EOF)
    {
        memset(dp,0x3f,sizeof(dp));
        build(1,t+1,1);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld%lld",&a[i].l,&a[i].r);
            a[i].c=1;
        }

        sort(a+1,a+n+1,cmp);

        for(int i=1;i<=n;i++)
        {
            ll ans=ask(max(1ll,a[i].l),a[i].r,1);
            if(ans==lnf) continue;
            dp[a[i].r+1]=min(dp[a[i].r+1],ans+a[i].c);
            change(a[i].r+1,dp[a[i].r+1],1);

        }
        if(dp[t+1]==lnf) printf("-1\n");
        else printf("%lld\n",dp[t+1]);
    }
    return 0;
}

二、The Battle of Chibi HDU - 5542:
Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao’s army. But all generals and soldiers of Cao Cao were loyal, it’s impossible to convince any of them to betray Cao Cao.

So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.

Yu Zhou discussed with Gai Huang and worked out N information to be leaked, in happening order. Each of the information was estimated to has ai value in Cao Cao’s opinion.

Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact M information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N information and just select M of them. Find out how many ways Gai Huang could do this.
Input
The first line of the input gives the number of test cases, T(1≤100). T test cases follow.

Each test case begins with two numbers N(1≤N≤1000) and M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then N numbers in a line, the ith number ai(1≤ai≤1e9) indicates the value in Cao Cao’s opinion of the ith information in happening order.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the ways Gai Huang can select the information.

The result is too large, and you need to output the result mod by 1000000007(109+7).
Sample Input
2
3 2
1 2 3
3 2
3 2 1
Sample Output
Case #1: 3
Case #2: 0

Hint
In the first cases, Gai Huang need to leak 2 information out of 3. He could leak any 2 information as all the information value are in increasing order.
In the second cases, Gai Huang has no choice as selecting any 2 information is not in increasing order.

题意:问有多少个长度为M的严格递增子序列。

    设 F(i,j) 为前 j 个数构成的以 Aj 结尾的数列中,长度为 i 的严格递增子序列有多少个。
    很容易想到状态转移方程:
     F(i,j) = sum of F(i - 1,k),其中 k < j,且Ak < Aj

    很显然如果暴力枚举 k 的话会T掉,在 最外层循环 i 固定时,内层循环 j 和 k进行时,发现 每当 j + 1 时,k的取值范围仅仅多了,k = j 这一决策,由于我们 j 是从前往后循环,所以我们只需要查询 j 前面 比 Aj 小的数的和即可,维护前缀和,不就是树状数组吗,
于是复杂度就降到了 N * M * logN。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#define ll long long
#define llu unsigned ll
using namespace std;


char buffer[100001],*S,*T;
inline char Get_Char()
{
    if (S==T)
    {
        T=(S=buffer)+fread(buffer,1,100001,stdin);
        if (S==T) return EOF;
    }
    return *S++;
}
inline int read()
{
    char c;int re=0;
    for(c=Get_Char();c<'0'||c>'9';c=Get_Char());
    while(c>='0'&&c<='9') re=(re<<1)+(re<<3)+(c-'0'),c=Get_Char();
    return re;
}



const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=1100;
int dp[maxn][maxn];
int b[maxn],a[maxn];
int sum[maxn];


void add(int x,int val)
{
    for(;x<maxn;x+=(x&-x))
        sum[x]+=val,sum[x]%=mod;
}

int ask(int x)
{
    int ans=0;
    for(;x;x-=(x&-x))
        ans+=sum[x],ans%=mod;
    return ans;
}

int main(void)
{
    int t;
    t=read();
    int pp=0;
    while(t--)
    {
        int n,m;
        n=read(),m=read();

        b[1]=-inf;
        for(int i=1;i<=n;i++)
        {
            a[i]=read();
            b[i+1]=a[i];
        }
        sort(b+1,b+n+2);
        int cnt=unique(b+1,b+n+2)-(b+1);

        for(int i=1;i<=n;i++)
            a[i]=lower_bound(b+1,b+cnt+1,a[i])-b;
        dp[0][0]=1;

        for(int i=1;i<=m;i++)
        {
            memset(sum,0,sizeof(sum));
            add(1,dp[i-1][0]);
            for(int j=1;j<=n;j++)
            {
                dp[i][j]=0;
                dp[i][j]+=ask(a[j]-1);
                add(a[j],dp[i-1][j]);
                dp[i][j]%=mod;
            }
        }

        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=dp[m][i],ans%=mod;
        printf("Case #%d: %lld\n",++pp,ans);
    }
    return 0;
}

写在后面:
无论dp决策的限制条件是多是少,我们都要尽量对其进行分离。多维dp状态在执行内层循环时,可以把外层循环的变量看成常量。状态转移取最优决策时,简单的限制条件用循环顺序处理,复杂的限制条件用数据结构维护。