HDU 4362 Dragon Ball(维护最小值DP优化)
来源:互联网 发布:出售数据资源 编辑:程序博客网 时间:2024/05/29 17:08
题意: 在连续的 n 秒中,每秒会出现 m 个龙珠,出现之后会立即消失,知道了第一秒所在的位置,每从一个位置移动到另一个位置的时候,消耗的价值为 abs(i-j), 知道了次出现的龙珠的价值,问 n 秒之后得到的最大价值是多少。
思路:这道题朴素的做法时间复杂度为O(n*n*m)勉强可以水过去,正解应该是用单调队列的思路维护最小值优化。
由状态转移方程dp[i][j] = min{ dp[i-1][k] + abs(pos[i-1][k]-pos[i][j]) } + cost[i][j]
可以推出dp[i][j]+pos[i][j]+cost[i][j] = min(dp[i-1][k]+pos[i-1][k]) (当pos[i-1][k]>pos[i][j]))
所以可以按位置排序后维护dp[i-1][k]+pos[i-1][k])的最小值。
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long #define pii (pair<int, int>)//#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const LL INF = 1000000000000000;int n, m;LL x;struct Period {LL pos, e, dp;bool operator < (const Period& A) const {return pos < A.pos;}} node[55][1005];int main() {//freopen("input.txt", "r", stdin);int T; cin >> T;while(T--) {scanf("%d%d%I64d", &m, &n, &x);for(int i = 1; i <= m; i++)for(int j = 1; j <= n; j++) scanf("%I64d", &node[i][j].pos);for(int i = 1; i <= m; i++)for(int j = 1; j <= n; j++) scanf("%I64d", &node[i][j].e);for(int i = 1; i <= n; i++) node[1][i].dp = abs(node[1][i].pos-x)+node[1][i].e;sort(node[1]+1, node[1]+1+n);for(int i = 2; i <= m; i++) {sort(node[i]+1, node[i]+1+n);int cur = 1;LL t = INF;for(int j = 1; j <= n; j++) {while(cur <= n && node[i-1][cur].pos <= node[i][j].pos) {t = min(t, node[i-1][cur].dp-node[i-1][cur].pos);cur++;}node[i][j].dp = t+node[i][j].e+node[i][j].pos;}cur = n;t = INF;for(int j = n; j >= 1; j--) {while(cur>=1 && node[i-1][cur].pos >= node[i][j].pos) {t = min(t, node[i-1][cur].dp+node[i-1][cur].pos);cur--;}node[i][j].dp = min(node[i][j].dp, t+node[i][j].e-node[i][j].pos);}} LL ans = INF;for(int i = 1; i <= n; i++) ans = min(ans, node[m][i].dp);cout << ans << endl;}return 0;}
0 0
- HDU 4362 Dragon Ball(维护最小值DP优化)
- hdu 4362 Dragon Ball(dp)
- hdu 4362 Dragon Ball 单调队列优化 dp
- hdu 4362 Dragon Ball(优先队列+dp)
- HDU 4362 - Dragon Ball
- hdu 4362 Dragon Ball
- hdu4362 Dragon Ball(dp+单调队列优化)
- hdu 4362 Dragon Ball 很裸的DP
- HDU 3872 Dragon Ball(DP+线段树+单调栈)
- hdu4326 Dragon Ball 单调队列优化Dp
- HDU_3872 Dragon Ball dp
- 【DP】 hdu4362 Dragon Ball
- HDU 4362 Dragon Ball 线段树
- HDU 3872 Dragon Ball
- hdu 3872 Dragon Ball
- HDU-4363-Dragon Ball
- hdu_4362 Dragon Ball (DP+单调栈)
- MUTC7 C - Dragon Ball 单调队列dp
- 移动“芯”软:IoT与数据中心或成蓝海
- ConcurrentHashMap的使用 水太深了
- 警示新能源车自燃 工信部排查安全隐患
- ASP.Net 文件存取拒绝
- PyQt5初级教程--PyQt5中的布局管理[5/13]
- HDU 4362 Dragon Ball(维护最小值DP优化)
- 如何用FtpWebRequest 传送巨大文件
- iOS9公测版下载安装详细图文教程
- Android持久化js的localstorage
- 【LeetCode-面试算法经典-Java实现】【103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)】
- 【LeetCode-面试算法经典-Java实现】【102-Binary Tree Level Order Traversal(二叉树层序遍历)】
- C语言的条件运算符
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- 对配置性编程的一点看法