acwing298 围栏
有 NN 块木板从左到右排成一行,有 MM 个工匠对这些木板进行粉刷,每块木板至多被粉刷一次。
第 ii 个木匠要么不粉刷,要么粉刷包含木板 Si 的,长度不超过 Li 的连续的一段木板,每粉刷一块可以得到 Pi 的报酬。
如何安排能使工匠们获得的总报酬最多
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N=16e3+3,M=102; int n,m,f[N][N]; int hh,tt,q[N]; struct T{ int c,L,p; bool operator<(T &u)const{ return p<u.p; } }a[N]; int func(int i,int k){ return f[i-1][k]-k*a[i].c; } void solve(){ int i,j; for(i=1;i<=n;i++){ hh=tt=0; for(j=0;j<=m;j++){ f[i][j]=max(f[i-1][j],f[i][j-1]); if(j>=a[i].p){ while(hh<=tt&&q[hh]<j-a[i].L) hh++; if(hh<=tt) f[i][j]=max(f[i][j],func(i,q[hh])+a[i].c*j); } else{ while(hh<=tt&&func(i,j)>func(i,q[tt])) tt--; q[++tt]=j; } } } cout<<f[n][m]<<endl; } int main(){ int i,j; cin>>m>>n; for(i=1;i<=n;i++) cin>>a[i].L>>a[i].c>>a[i].p; sort(a+1,a+1+n); solve(); }
原文地址:http://www.cnblogs.com/towboa/p/16852000.html
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。