POJ1821 Fence (monotone queue)

/*
    Design condition F [i] [j] denotes the front i j ex-Tu, and then transferred
    Found part can maintain a monotonic decrease complexity by queue
*/
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e5+7;
const int inf=0x3f3f3f3f;
int n,k;
struct node{
    int l,p,s;
}s[N];
int q[N];
int f[110][16100];
bool cmp(node a,node b){
    return a.s<b.s;
}
int main () {
    cin>>n>>k;
    int i;
    for(i=1;i<=k;i++){
      cin>>s[i].l>>s[i].p>>s[i].s;
    }
    sort(s+1,s+1+k,cmp);
    int j;
    memset(f,0,sizeof f);
    int hh=0;
    int tt=-1;
    for(i=1;i<=k;i++){
        hh=0;
        tt=0;
        Q [ 0 ] = 0 ; // head of the queue 0, the current state of the only legal 
        for (J = . 1 ; J <= n-; J ++ ) {
            F [I] [J] = max (F [I- . 1 ] [J], F [I] [J- . 1 ]); // where the brush is not carpenter 
            IF (J> = S [I] .s S + [I] .L) // coating is less than but can not break, because the front of the worker may also be applied to the back of the board 
                Continue ;
             the while (HH <&& TT = Q [HH] + S [I] .L <J) // when it exceeds the range necessary to remove the coating 
                HH ++ ;
             iF (J <S [I] .s) { // will be possible in a queue 
                int tmp = F [I- . 1 ] [J] -s [I ] .p * J;
                 the while (HH <&& TT = F [I- . 1 ] [Q [TT]] - S [I] .p * Q [TT] < tmp)
                    tt--;
                q[++tt]=j;
                continue;
            }
            f[i][j]=max(f[i][j],f[i-1][q[hh]]+s[i].p*(j-q[hh]));
        }
    }
    cout<<f[k][n]<<endl;
}
View Code

Look at the code explanation

Guess you like

Origin www.cnblogs.com/ctyakwf/p/12454694.html