[DP优化] POJ 1160 Post Office
来源:互联网 发布:java字符数组赋值 编辑:程序博客网 时间:2024/06/06 05:27
另解:四边形不等式优化
考虑这个最优解关于段数是凸的,那么我们不限制段数,而是给每段一个额外的权值,随着这个权值单调变化,最优解的段数也会单调变化,二分出最优解是
这个的本质是二分出那个凸函数在
然后这个dp是满足决策单调性的 复杂度
PS.之前一直有个坑是当三点共线也就是连续两段斜率一样时,二分不到中间那个点,这个其实需要一点小细节上的处理,感谢阿爷的帮助
#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;typedef long long ll;const int N=305;int n,m;ll f[N],X; int pre[N];int w[N][N],a[N];inline int ck(){ int p=n,ret=0; while (p) p=pre[p],ret++; return ret;}struct abcd{ int x,l,r; abcd(int x=0,int l=0,int r=0):x(x),l(l),r(r) { } bool operator < (const abcd &B) const{ return l<B.l; }}sta[N]; int pnt;inline ll calc(int x,int y){ if (x>y) return 1LL<<60; return f[x]+w[x+1][y]+X;}inline int Solve(ll X){ pnt=0; ::X=X; sta[++pnt]=abcd(0,1,n); for (int i=1;i<=n;i++){ int t=upper_bound(sta+1,sta+pnt+1,abcd(0,i,0))-sta-1; f[i]=calc(sta[t].x,i); pre[i]=sta[t].x; if (i==n) continue; while (calc(sta[pnt].x,sta[pnt].l)>=calc(i,sta[pnt].l)) pnt--; int L=sta[pnt].l,R=sta[pnt].r+1,MID; while (L+1<R){ MID=(L+R)>>1; if (calc(sta[pnt].x,MID)<calc(i,MID)) L=MID; else R=MID; } sta[pnt].r=R-1; if (R<=n) sta[++pnt]=abcd(i,R,n); } return ck();}int main(){ freopen("t.in","r",stdin); freopen("t.out","w",stdout); scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%d",a+i); for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) w[i][j]=w[i][j-1]+a[j]-a[(i+j)>>1]; ll L=0,R=1e9,MID; ll ans; while (L+1<R){ int t=Solve(MID=(L+R)/2); if (t<m){ R=MID; }else{ L=MID; ans=f[n]-MID*m; } } printf("%lld\n",ans); return 0;}
阅读全文
0 0
- POJ-1160 Post office 四边形优化DP
- [DP优化] POJ 1160 Post Office
- POJ 1160 Post Office 四边形不等式优化DP
- poj 1160 Post Office(四边形不等式优化dp)
- POJ 1160 Post Office(四边形不等式优化DP)
- [DP 四边形不等式优化] POJ 1160 Post Office
- poj 1160 Post Office (四边形不等式优化DP)
- dp四边形优化 poj 1160 Post Office题解
- poj-1160 post office (dp)
- Post Office - POJ 1160 dp
- 【DP】 POJ 1160 Post Office
- POJ 1160 Post Office DP
- POJ 1160 Post Office (DP)
- poj 1160 post office------DP
- poj 1160 Post Office(DP简单题)
- POJ 1160 Post Office (经典dp)
- POJ 1160 Post Office(DP)
- Poj-1160 Post Office(经典dp
- 使用Pull解析器读取XML文件和生成XML文件
- nova 冷迁移配置nova用户互信
- TCP、UDP、IP 协议分析
- 读写SharedPreferences中的数据
- 【Linux】中TCP的握手与挥手
- [DP优化] POJ 1160 Post Office
- Android之SQlite存储
- Android内容提供者(Content provider)
- TST运行报错解决
- java udp简单例子
- java tcp简单例子
- Linux_博客-端口分类调研
- defy手机 小技能 问题
- spring mvc maven