数据结构优化dp:

本文探讨了如何使用数据结构优化动态规划问题,特别是在处理区间最值和严格递增子序列问题时。通过案例分析,展示了线段树在覆盖任务分配和区间最值查询中的应用,以及树状数组在维护前缀和、解决严格递增子序列计数问题上的效率。文章强调了在外层循环固定时,内层循环中利用数据结构进行状态转移的重要性,以降低时间复杂度。
摘要由CSDN通过智能技术生成

写在前面:
一般的,只要决策的候选集合只扩大,不缩小,我们就可以仅用一个变量维护最值,不断与新加入候选集合的元素比较,即可直接得到最优决策,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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值