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