LuoguP5017 摆渡车 $dp$
题意
吐槽
听同学说今年\(pjT3\)很难,于是就去看了下。
一眼斜率优化...为什么\(n,m\)这么小啊...
感觉这题出的还是不错的。
Solution
首先我们先转化一波题意:给出数轴上\(n\)个点,让你选择若干个两两距离大于等于\(m\)的点,使得每个点到右边第一个你选的点的距离和最小。
大力\(dp\),发现可以斜率优化。
\(pj\)会考斜率优化?
冷静一波,显然每个点的决策肯定在\([i-m*2,i-m]\)之间...
所以好像直接\(O(max(t_i)*m)\)转移常数小并不会\(T\)?
至少我\(Luogu\)数据过了...
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define cross(i,u) for (register int i=first[u];i;i=last[i])
using namespace std;
typedef long long ll;
inline ll read(){
ll x=0;int ch=getchar(),f=1;
while (!isdigit(ch)&&(ch!='-')) ch=getchar();
if (ch=='-'){f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int N = 1010, maxn = 5000110;
int n,m,Max,a[N],sum[maxn],Sum[maxn],dp[maxn];
bool vis[maxn];
inline void solve(){
For(i,0,Max){
dp[i]=Sum[i]*i-sum[i];
For(j,max(0,i-2*m),i-m){
int cnt=Sum[i]-Sum[j];
dp[i]=min(dp[i],dp[j]+cnt*i-sum[i]+sum[j]);
}
}
int ans=1e9;
For(i,Max-m,Max) ans=min(ans,dp[i]);
printf("%d",ans);
}
int main(){
n=read(),m=read();
For(i,1,n) a[i]=read(),Max=max(Max,a[i]),Sum[a[i]]++,sum[a[i]]+=a[i];
Max+=m;
For(i,1,Max) sum[i]+=sum[i-1],Sum[i]+=Sum[i-1];
solve();
}
再冷静一波...好像有很多状态是没用的?
如果一个点前面的\(m\)个点中都没有给出的点那么\(dp_i=dp_{i-m}\)...
然后算下复杂度...好像是\(O(nm+max(t_i))\)
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define cross(i,u) for (register int i=first[u];i;i=last[i])
using namespace std;
typedef long long ll;
inline ll read(){
ll x=0;int ch=getchar(),f=1;
while (!isdigit(ch)&&(ch!='-')) ch=getchar();
if (ch=='-'){f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int N = 1010, maxn = 5000110;
int n,m,Max,a[N],sum[maxn],Sum[maxn],dp[maxn];
inline void solve(){
For(i,0,Max){
dp[i]=Sum[i]*i-sum[i];
if (i>m&&Sum[i]-Sum[i-m]==0){dp[i]=dp[i-m];continue;}
For(j,max(0,i-2*m),i-m){
int cnt=Sum[i]-Sum[j];
dp[i]=min(dp[i],dp[j]+cnt*i-sum[i]+sum[j]);
}
}
int ans=1e9;
For(i,Max-m,Max) ans=min(ans,dp[i]);
printf("%d",ans);
}
int main(){
n=read(),m=read();
For(i,1,n) a[i]=read(),Max=max(Max,a[i]),Sum[a[i]]++,sum[a[i]]+=a[i];
Max+=m;
For(i,1,Max) sum[i]+=sum[i-1],Sum[i]+=Sum[i-1];
solve();
}
口胡
不难发现最后有用的点数是\(nm\)...
我们把这\(nm\)个点抠出来,基排一下,然后直接斜率优化\(dp\)应该能做到优秀的\(O(nm)\)的复杂度。
给我把nm开到1000w,ti开到1e9
\(upd:\)我第二个复杂度好像算错了...应该是\(O(nm^2+max(t_i))\)
LuoguP5017 摆渡车 $dp$的更多相关文章
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- Light OJ 1031---Easy Game(区间DP)
题目链接 http://lightoj.com/volume_showproblem.php?problem=1031 Description You are playing a two player ...
- HDU 5807 Keep In Touch DP
Keep In Touch Problem Description There are n cities numbered with successive integers from 1 to n ...
- 17996 Daily Cool Run (dp)
时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 Description Daily Cool Run is a popular gam ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- BC.5200.Trees(dp)
Trees Accepts: 156 Submissions: 533 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/6 ...
- hdu 5115 区间dp ***
题意:有n只狼,每只狼有两种属性,一种攻击力一种附加值,我们没杀一只狼,那么我们受到的伤害值为这只狼的攻击值与它旁边的两只狼的附加值的和,求把所有狼都杀光受到的最小的伤害值. 枚举中间k作为最后杀死的 ...
- HDU 4258 Covered Walkway 斜率优化DP
Covered Walkway Problem Description Your university wants to build a new walkway, and they want at ...
随机推荐
- Css3帧动画深入探寻,讲点项目中实际会碰到的问题
先加个副标题XD --如何解决background-size为100%下处理@keyframes 正是在项目中遇到副标题,才引起我更深入的探寻 先略带一下基本的css3动画 css3的动画实现是通过属 ...
- [转]坐在马桶上看算法:只有五行的Floyd最短路算法
此算法由Robert W. Floyd(罗伯特·弗洛伊德)于1962年发表在“Communications of the ACM”上.同年Stephen Warshall(史蒂芬·沃舍尔)也独立发表了 ...
- win10系统下我的电脑右键没有属性
1.右键点击系统左下角的开始,菜单中点击运行 2.在输入框中输入regeidt,点击确定打开系统的注册表编辑器 3.然后依次打开HKEY_CURRENT_USER\Software\Microsoft ...
- Python使用OpenCV实现简单的人脸检测
文章目录: OpenCV安装 安装numpy 安装opencv OpenCV使用 OpenCV测试 效果图: 注意: 图片人脸检测 程序要求: 技术实现思路 注意 本文使用的环境是:Windows+P ...
- MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移
系统信息: mysql主库 mysql从库 VIP 192.168.1.150 mysql 主主同步都设置 auto-increment-offset,auto-increment-increment ...
- marshmallow: 简化Python对象系列化
转载:http://www.thinksaas.cn/topics/0/594/594368.html marshmallow -一个轻量级的库用于将复杂对象转成简单的Python数据类型.或从简单的 ...
- linux bash shell之declare
一. #Set the right GC options based on the what we are runningdeclare -a server_cmds=("master&qu ...
- (二)HtmlUnit 使用
第一节: htmlunit 模拟浏览器请求 第二节: htmlunit 获取指定元素 第三节: htmlunit 使用代理 IP 第四节: htmlunit 取消 css,javascript 支持 ...
- Oracle学习笔记:wm_concat函数合并字段
在Oracle中使用wm_concat(column)可以实现字段的分组合并,逗号分隔. 例如,现有表temp_cwh_test: -- 创建临时表 create table temp_cwh_tes ...
- PHP5.6中php-fpm的配置、启动、关闭和重启
转:http://blog.csdn.net/field_yang/article/details/52401994 该文主要讲述:如何配置PHP-fpm.常见报错解决方法和php-fpm的启动.关闭 ...