HDU 4362
方程很简单
p[i][j] = min{dp[i-1][k] + |pos[i-1][k] - pos[i][k]|} + v[i][j];
循环求值肯定TLE,主要是绝对值不方便。好吧,我真的BI了狗了,竟然想不到怎么样去绝对值。F,只要从左到右dp一次,再从右往左dp一次,不就好了吗?????F。当然了,必须是选按位置排序。开始用单调队列,后来发现根本没必要了。
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm> using namespace std;
struct Node{
int pos,val;
};
const int inf=1<<30;
Node map[55][1050];
bool cmp(Node a,Node b){
if(a.pos<b.pos) return true;
return false;
}
int que[1050];
int dp[55][1050]; int main(){
int T,n,m,x;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&m,&n,&x);
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j].pos);
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j].val);
}
sort(map[i]+1,map[i]+1+n,cmp);
}
for(int i=1;i<=n;i++){
dp[1][i]=abs(map[1][i].pos-x)+map[1][i].val;
}
int k,mm;
for(int i=2;i<=m;i++){
mm=inf;
k=1;
for(int j=1;j<=n;j++){
for(k;k<=n;k++){
if(map[i-1][k].pos>map[i][j].pos) break;
mm=min(mm,dp[i-1][k]-map[i-1][k].pos);
}
dp[i][j]=mm+map[i][j].pos+map[i][j].val;
}
k=n;
mm=inf;
for(int j=n;j>=1;j--){
for(k;k>=1;k--){
if(map[i-1][k].pos<map[i][j].pos) break;
mm=min(mm,dp[i-1][k]+map[i-1][k].pos);
}
dp[i][j]=min(dp[i][j],mm-map[i][j].pos+map[i][j].val);
}
}
int ans=inf;
for(int i=1;i<=n;i++)
ans=min(dp[m][i],ans);
printf("%d\n",ans);
}
return 0;
}
HDU 4362的更多相关文章
- HDU 4362 Dragon Ball 贪心DP
Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon ...
- HDU 4362 Dragon Ball 线段树
#include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...
- 清北学堂2018DP&图论精讲班 DP部分学习笔记
Day 1 上午 讲的挺基础的--不过还是有些地方不太明白 例1 给定一个数n,求将n划分成若干个正整数的方案数. 例2 数字三角形 例7 最长不下降子序列 以上太过于基础,不做深入讨论 例3 给定一 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- Android源码下载方法
1. 下载 repo 工具 mkdir ~/bin PATH=~/bin:$PATH curl https://storage.googleapis.com/git-repo-downloads/re ...
- 使用Dreamweaver在一张图片上添加多个热点链接
所有代码: <html> <head> <meta charset="utf-8"> <title>无标题文档</title& ...
- Windows开发小问题集
ON_BN_KILLFOCUS无效,因为需要BS_NOTIFY
- node(koa)微信公众号接口认证
使用微信测试号, 花生壳内网穿透 需要注意 token 两边都是自定义, 需要保持一致, const Koa = require('koa') const sha1 = require('sha1') ...
- CSS——宠物demo
注意:ul中自带padding值,需要清除. <!DOCTYPE html> <html lang="en"> <head> <meta ...
- SQL基本操作——DROP撤销索引、表以及数据库
DROP撤销索引.表以及数据库 --DROP INDEX 命令删除表格中的索引 DROP INDEX table_name.index_name --DROP TABLE 语句删除表(表的结构.属性以 ...
- python调用java API
JPype documentation JPype is an effort to allow python programs full access to java class libraries. ...
- Objective-C在ARC下结合GCD的单例模式和宏模版
单例模式在iOS开发过程中经常用到,苹果提供过objective c单例的比较官方的写法: static MyGizmoClass *sharedGizmoManager = nil; + (MyGi ...
- .net 程序集加载,版本不匹配的解决方法
经常有些时候,A.dll引用的是Microsoft.EntityFrameworkCore.dll version=1.0.0.0 publicKeyToken="adb9793829dda ...
- PAT_A1018#Public Bike Management
Source: PAT A1018 Public Bike Management (30 分) Description: There is a public bike service in Hangz ...