關於動態規劃的單調隊列優化

POJ2373:Dividing the Path
描述
Farmer John's cows have discovered that the clover growing along the ridge of the hill in his field is particularly good. To keep the clover watered, Farmer John is installing water sprinklers along the ridge of the hill.


To make installation easier, each sprinkler head must be installed along the ridge of the hill (which we can think of as a one-dimensional number line of length L (1 <= L <= 1,000,000); L is even).


Each sprinkler waters the ground along the ridge for some distance in both directions. Each spray radius is an integer in the range A..B (1 <= A <= B <= 1000). Farmer John needs to water the entire ridge in a manner that covers each location on the ridge by exactly one sprinkler head. Furthermore, FJ will not water past the end of the ridge in either direction.


Each of Farmer John's N (1 <= N <= 1000) cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval (S,E). Each of the cow's preferred ranges must be watered by a single sprinkler, which might or might not spray beyond the given range.


Find the minimum number of sprinklers required to water the entire ridge without overlap.
輸入
* Line 1: Two space-separated integers: N and L


* Line 2: Two space-separated integers: A and B


* Lines 3..N+2: Each line contains two integers, S and E (0 <= S < E <= L) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge and so are in the range 0..L.
輸出
* Line 1: The minimum number of sprinklers required. If it is not possible to design a sprinkler head configuration for Farmer John, output -1.
樣例輸入
2 8
1 2
6 7
3 6
樣例輸出

3

這個題目程設課上田永鴻老師講了單調隊列的做法,雖然以前自己也一直都是用單調隊列優化動態規劃的,而且用的非常之多,但是今天突然奇想

覺得用單調隊列優化其實用普通的優先隊列也可以優化(當然了 普通的優先隊列優化 我寫出來複雜度要比單調隊列的複雜度多一個logN)


因爲每個對於進隊列一次出隊列一次,我們只要維護一段區間內的最優值即可,當然隨着i往後移動,可以轉移的區間也往後移動,我們不馬上

刪除不在區間內的值,而是等取出來時發現不符合區間的要求再將它刪除即可,直到出現的最小值是在這個區間內。

這樣的複雜度就是NlogN。這樣做看似和線段樹差不多。 但是這題的想法和之前做的STL專項練習中的題目G是一樣的。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN = 1000000+10;
int n,l,A,B,s,e;
struct Node
{
       int Pos, Cnt;
       bool operator<(const Node & a)const{
            return Cnt>a.Cnt;
       }
}F[MAXN];
int cow[MAXN];
priority_queue<Node> q;
int main()
{
    cin>>n>>l>>A>>B;A*=2;B*=2;
    for (int i = 1; i <= n; ++i){
        scanf("%d%d",&s,&e);
        cow[s+1]++; cow[e]--;
    }
    int cowCnt = 0; q.push(F[0]);
    for (int i = 2; i <= l;i+=2)
    {
        cowCnt +=cow[i]+cow[i-1];
        if (cowCnt==0 && i>=A){
           while (!q.empty()){
                 if (i-q.top().Pos>B) { q.pop(); continue;}
                 F[i].Pos = i; F[i].Cnt = q.top().Cnt+1;
                 break;
           }
        }
        if (i>=A && F[i-A+1].Cnt != 0) q.push(F[i-A+1]);
        if (i+1>=A && F[i-A+2].Cnt != 0) q.push(F[i-A+2]);
    }
    if (F[l].Cnt ==0) cout<<-1<<endl;else
    cout<< F[l].Cnt << endl;
    return 0;
}


發佈了59 篇原創文章 · 獲贊 12 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章