831A. Office Keys
time limit per test 2 seconds
memory limit per test 256 megabytes
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn’t be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1≤n≤1000, n≤k≤2000, 1≤p≤10^9) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1,a2,…,an (1≤ai≤10^9) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1,b2,…,bk (1≤bj≤10^9) — positions of the keys. The positions are given in arbitrary order.
Note that there can’t be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input1
2 4 50
20 100
60 10 40 80
Output1
50
Input2
1 2 10
11
15 7
Output2
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
题解
DP
题目大意:n个人,k把钥匙,办公室在位置p
读入n个人的位置和k把钥匙的位置
要求每个人拿一把钥匙去办公室,
问最少什么时候所有人都能到办公室。
f[i][j]表示前i个人,在前j把钥匙中都拿到了钥匙,并且到终点的最优最长需求时间。
动态转移方程见代码
注意之前要先排序
1 |
|
洛谷3040 [USACO12JAN]贝尔分享Bale Share
题目描述
FJ有N (1 <= N <= 20)包干草,干草i的重量是 S_i (1 <= S_i <= 100),他想尽可能平均地将干草分给3个农场。
他希望分配后的干草重量最大值尽可能地小,比如, B_1,B_2和 B_3是分配后的三个值,假设B_1 >= B_2 >= B_3,则他希望B_1的值尽可能地小。
例如:8包干草的重量分别是:2 4 5 8 9 14 15 20,一种满足要求的分配方案是
农场 1: 2 9 15 B_1 = 26
农场 2: 4 8 14 B_2 = 26
农场 3: 5 20 B_3 = 25
请帮助FJ计算B_1的值。
输入输出格式
输入格式:
Line 1: The number of bales, N.
Lines 2..1+N: Line i+1 contains S_i, the size of the ith bale.
输出格式:
Line 1: Please output the value of B_1 in a fair division of the hay bales.
输入输出样例
输入样例#1:
8
14
2
5
15
8
9
20
4
输出样例#1:
26
题解
f[i][j][k] 表示 使用前i种草料,是否可以使b1为j,b2为k(b3可以通过sum-b1-b2算出来)
1 |
|