HDU 6076 题解
来源:互联网 发布:图片 js 点击放大 wap 编辑:程序博客网 时间:2024/05/17 06:00
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6076
Problem Description
In airport of Bytetown, there are two long queues waiting for security check. Checking a person needs one minute, and two queues can be checked at the same time.
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
Please write a program to help Little Q find the best way costing the minimum time.
Input
The first line of the input contains an integer
In each test case, there are 2 integers
In the next line, there are n distinct integers
Then in the next line, there are n distinct integers
Output
For each test case, print a single line containing an integer, denoting the minimum time to check all people.
Sample Input
14 22 3 1 41 2 4 3
Sample Output
7HintTime 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.
代码:
/*HDU 6076 - Security Check [ DP,二分 ] | 2017 Multi-University Training Contest 4题意: 给出两个检票序列 A[N], B[N] 规定 abs(A[i]-B[j]) <= k 的i,j不能同时检票 求最少的检票时间 限制 N<= 6e4, k <= 10分析: f(i,j) 为检票至i,j的时间 则 f(i,j) = f(i-1,j-1) + 1 , abs(A[i]-B[j]) > k = min(f(i-1,j), f(i,j-1)) + 1 , abs(A[i]-B[j]) <= k 对于第二项,由于k小,可DP 对于第一项,可以二分最大的 t 使得 f(i,j) = f(i-t,j-t) + t 成立 那么 f(i-t,j-t) 就是第二项的了*/ #include <bits/stdc++.h>using namespace std;const int N = 60005;vector<int> G[N<<1];int dp[N][25];int a[N], b[N], pos[N];int t, n, k;int f(int n, int m){ if (n == 0 || m == 0) return n+m; if (abs(a[n]-b[m]) > k) { int t = lower_bound(G[m-n+N].begin(), G[m-n+N].end(), n) - G[m-n+N].begin(); if (t == 0) return max(n, m); t = G[m-n+N][t-1]; return f(t, m-n+t) + n-t; } else { int t = b[m]-a[n]+k; if (dp[m][t] == -1) dp[m][t] = min(f(n-1, m), f(n, m-1)) + 1; return dp[m][t]; }}int main(){ scanf("%d", &t); while (t--) { for (int i = 0; i < N<<1; i++) G[i].clear(); memset(dp, -1, sizeof(dp)); scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); pos[a[i]] = i; } for (int i = 1; i <= n; i++) scanf("%d", &b[i]); for (int i = 1; i <= n; i++) for (int j = max(1, b[i]-k); j <= min(n, b[i]+k); j++) { G[i-pos[j]+N].push_back(pos[j]);// 记录当a、b保持当前相对位置(i-pos[j]+N)时,a的哪些位置是不能同时检查的 } for (int i = 0; i < N<<1; i++) sort(G[i].begin(), G[i].end()); int ans = f(n, n); printf("%d\n", ans); }}
转载文章:HDU 6076 - Security Check | 2017 Multi-University Training Contest 4
- HDU 6076 题解
- hdu 4602题解
- hdu 1789题解
- HDU 4665 题解
- HDU 4710题解
- HDU 4715题解
- hdu 1087题解
- hdu 2844题解
- hdu 2845 题解
- hdu 4007题解
- hdu 4004题解
- hdu 1879题解报告
- HDU 2180 时钟 题解
- HDU 1425 sort 题解
- HDU 1015 Safecracker 题解
- HDU Computer Transformation1041 题解
- HDU 题解 1024
- hdu 4451Dressing题解
- Centos 6.5搭建Hadoop-2.7.3分布式集群
- zookeeper伪分布安装
- ConcurrentHashMap详解
- 异平台异版本oracle数据导入导出
- navicat 关于orcale新建表空间,用户和权限分配
- HDU 6076 题解
- POJ2229 Sumsets
- Oracle db_link数据同步(数据量小)
- css3学习1
- 加载外部数据
- LitePal基本用法
- 如何查看ubuntu的内核版本和发行版本号?
- swift练习-String、Array、Dictionary、Set
- JS创建对象的模式及其优缺点详解