【Codeforces797F】Mice and Holes
来源:互联网 发布:安卓放弃java 编辑:程序博客网 时间:2024/05/20 18:02
题意:
列出dp方程:
发现这个方程在每一层中都是单调的,可以类似斜率优化维护。
#include <bits/stdc++.h>#define gc getchar()#define ll long long#define N 5009#define inf 0x3f3f3f3f3f3f3f3fllusing namespace std;int n,m,pos[N],S,q[N];ll dp[N][N],sum[N];struct node{ int pos,num; bool operator <(const node &rhs) const { return pos<rhs.pos; }}a[N];int read(){ int x=1; char ch; while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1; int s=ch-'0'; while (ch=gc,ch>='0'&&ch<='9') s=s*10ll+ch-'0'; return s*x;}int main(){ n=read(),m=read(); for (int i=1;i<=n;i++) pos[i]=read(); sort(pos+1,pos+n+1); for (int i=1;i<=m;i++) a[i].pos=read(),a[i].num=read(),S+=a[i].num; sort(a+1,a+m+1); if (S<n) { puts("-1"); return 0; } for (int i=1;i<=n;i++) dp[0][i]=inf; for (int i=1;i<=m;i++) { for (int j=1;j<=n;j++) sum[j]=sum[j-1]+abs(pos[j]-a[i].pos); int head=1,tail=0; for (int j=0;j<=n;j++) { while (head<=tail&&q[head]<j-a[i].num) head++; while (head<=tail&&dp[i-1][j]-sum[j]<=dp[i-1][q[tail]]-sum[q[tail]]) tail--;//单调队列,数值逐渐增大 q[++tail]=j; dp[i][j]=dp[i-1][q[head]]-sum[q[head]]+sum[j]; } } printf("%lld\n",dp[m][n]); return 0;}
阅读全文
0 0
- 【Codeforces797F】Mice and Holes
- CF 797F Mice and Holes(单调队列优化dp)
- CF797F:Mice and Holes(dp + 单调队列优化)
- Codeforces 797F Mice And Holes DP(单调队列优化)
- 1056. Mice and Rice
- PAT_1056: Mice and Rice
- 1056. Mice and Rice
- 1056.Mice and Rice
- 1056 Mice and Rice
- 1016 Mice and Rice
- 1056. Mice and Rice
- PAT_1056. Mice and Rice
- UVA 356 Square Pegs And Round Holes
- imfill matlab fillimage regions and holes
- [lwn]File holes, races, and mmap()
- UVa 356 - Square Pegs And Round Holes
- UVA, 356 Square Pegs And Round Holes
- PAT1056.Mice and Rice (25)
- 没事刷个脸儿!iPhone8放弃指纹识别,改用3D刷脸
- BZOJ3534: [Sdoi2014]重建(变元矩阵树)
- Java重载与重写
- iOS 11为苹果地图带来了哪些改进?
- iOS11 beta iCloud Activation Lock曝漏洞
- 【Codeforces797F】Mice and Holes
- iOS 11 测试版泄密,iPhone 8 新功能再次确认
- 未选择framework
- mysql学习之写几个最简单的存储过程
- 一篇小学生作文《我的爸爸在华为》,看哭无数人!
- iOS异常捕获
- 这数字太惊人!iPhone十年来光维修费就花了这个数......
- iOS 自动化测试的那些干货
- 移动端网络常见问题及优化对策