HDU 6076 Security Check DP递推优化
Security Check
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Picture from Wikimedia Commons
Two teams A and B are going to travel by plane. Each team has n players, ranked from 1 to n according to their average performance. No two players in the same team share the same rank. Team A is waiting in queue 1 while team B is waiting in queue 2. Nobody else is waiting for security check.
Little Q is the policeman who manages two queues. Every time he can check one person from one queue, or check one each person from both queues at the same time. He can't change the order of the queue, because that will make someone unhappy. Besides, if two players Ai and Bj are being checked at the same time, satisfying |Ai−Bj|≤k, they will make a lot of noise because their rank are almost the same. Little Q should never let that happen.
Please write a program to help Little Q find the best way costing the minimum time.
In each test case, there are 2 integers n,k(1≤n≤60000,1≤k≤10) in the first line, denoting the number of players in a team and the parameter k.
In the next line, there are n distinct integers A1,A2,...,An(1≤Ai≤n), denoting the queue 1 from front to rear.
Then in the next line, there are n distinct integers B1,B2,...,Bn(1≤Bi≤n), denoting the queue 2 from front to rear.
4 2
2 3 1 4
1 2 4 3
Time 1 : Check A_1.
Time 2 : Check A_2.
Time 3 : Check A_3.
Time 4 : Check A_4 and B_1.
Time 5 : Check B_2.
Time 6 : Check B_3.
Time 7 : Check B_4.
题解:
设定f[i][j] 表示 递推到 a[i], b[j] 时的最少时间
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N = 6e5 + , inf = 1e9; int n, a[N], b[N], k, fos[N], f[][N][];
vector<int > hav1[N],hav2[N]; int dfs(int i,int j) {
if(!i || !j) return j+i;
if(abs(a[i] - b[j]) <= k) {
int& ret = f[i>j?:][i][a[i]-b[j] +k];
if(ret) return ret;
return ret = min(dfs(i-,j),dfs(i,j-))+;
}
int ok;
if(i > j) ok = hav1[i-j][upper_bound(hav1[i-j].begin(),hav1[i-j].end(),i) - hav1[i-j].begin()-];
else ok = hav2[j-i][upper_bound(hav2[j-i].begin(),hav2[j-i].end(),i) - hav2[j-i].begin()-];
return ok?(dfs(ok,j - i + ok) + i - ok):max(i,j);
}
int main() {
int T;cin>>T;while(T--) {
scanf("%d%d",&n,&k);
memset(f,,sizeof(f));
memset(fos,,sizeof(fos));
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= n; ++i) scanf("%d",&b[i]),fos[b[i]] = i;
for(int i = ; i <= *n; ++i)
hav1[i].clear(),hav2[i].clear(),hav1[i].push_back(),hav2[i].push_back();
for(int i = ; i <= n; ++i) {
for(int j = a[i] - k; j <= a[i] + k; ++j) {
if(j < || j > n) continue;
if(i > fos[j]) hav1[i - fos[j]].push_back(i);
else hav2[fos[j] - i].push_back(i);
}
}
for(int i = ; i <= *n; ++i)
hav1[i].push_back(n+),hav2[i].push_back(n+);
for(int i = ; i <= *n; ++i) sort(hav1[i].begin(),hav1[i].end());
for(int i = ; i <= *n; ++i) sort(hav2[i].begin(),hav2[i].end());
printf("%d\n",dfs(n,n));
}
return ;
}
HDU 6076 Security Check DP递推优化的更多相关文章
- 2016多校第4场 HDU 6076 Security Check DP,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6076 题意:现要检查两条队伍,有两种方式,一种是从两条队伍中任选一条检查一个人,第二种是在每条队伍中同 ...
- HDU 6076 - Security Check | 2017 Multi-University Training Contest 4
/* HDU 6076 - Security Check [ DP,二分 ] | 2017 Multi-University Training Contest 4 题意: 给出两个检票序列 A[N], ...
- hdu 6076 Security Check
题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6076 2017 Multi-University Training Contest - Team 4 ...
- HDU Tickets(简单的dp递推)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu2089(数位DP 递推形式)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Cayley-Hamilton定理与矩阵快速幂优化、常系数线性递推优化
原文链接www.cnblogs.com/zhouzhendong/p/Cayley-Hamilton.html Cayley-Hamilton定理与矩阵快速幂优化.常系数线性递推优化 引入 在开始本文 ...
- HDU 3469 Catching the Thief (博弈 + DP递推)
Catching the Thief Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题解报告:hdu 2084 数塔(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...
- hdu 2604 Queuing(dp递推)
昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...
随机推荐
- 【POJ3498】March of the Penguins(最大流,裂点)
题意:在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃,但是它们的跳跃距离,有一个上限. ...
- 【CF707B】Bakery(想法题)
题意: 有N个城市,M条无向边,其中有K个城市是仓库 现在要在非仓库的城市中选择一家开面包店,使得其最少与一个仓库联通,且到所有仓库距离的最小值最小 (1 ≤ n, m ≤ 10^5, 0 ≤ k ≤ ...
- h5页面判断微信端用浏览器打开代码
<div class="weixin-tip"> <p> <img src="img/live_weixin.png" alt=& ...
- Win32 绘制RGB三原色图案
以前看到三原色的图案,一直很好奇是如何画出来.后来终于搞清楚了,其实很简单,实际上就是RGB三个分量的"位与"运算. 下面给出Win32绘制三原色图案的例子,特此记录在此: #in ...
- centos 7安装postgresql10.3
最新版本安装请移步:阿里云服务器 centos 7 安装postgresql 11 一.Postgresql简介 官方网站:https://www.postgresql.org/ 简介参考zhihu文 ...
- AC日记——总分 Score Inflation 洛谷 P2722
题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类& ...
- const T、const T*、T *const、const T&、const T*& 的区别
原文地址: http://blog.csdn.net/luoweifu/article/details/45600415 这里的T指的是一种数据类型,可以是int.long.doule等基本数据类型, ...
- Light oj 1085 - All Possible Increasing Subsequences (简单dp + 离散化 + BIT)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1085 题意: 问你有多少个上升子序列. 思路: dp[i]表示以第i个数结尾的 ...
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- ADO如何记录SQL日志
ADO如何记录SQL日志 procedure TfrmDM.ADOConnection1WillExecute(Connection: TADOConnection; var CommandText: ...