H - Hard Rock
Ilya is a frontman of the most famous rock band on Earth. Band decided to make the most awesome music video ever for their new single. In that music video Ilya will go through Manhattan standing on the top of a huge truck and playing amazing guitar solos. And during this show residents of the island will join in singing and shaking their heads. However, there is a problem. People on some streets hate rock.
Recall that Manhattan consists of n vertical and m horizontal streets which form the grid of ( n − 1)×( m − 1) squares. Band’s producer conducted a research and realized two things. First, band’s popularity is constant on each street. Second, a popularity can be denoted as an integer from 1 to 10 9. For example, if rockers go along the street with popularity equal to 10 9 then people will greet them with a hail of applause, fireworks, laser show and boxes with... let it be an orange juice. On the other hand, if rockers go along the street with popularity equal to 1 then people will throw rotten tomatoes and eggs to the musicians. And this will not help to make the most awesome music video!
So, a route goes from the upper left corner to the bottom right corner. Let us define the route coolness as the minimal popularity over all streets in which rockers passed non-zero distance. As you have probably guessed, the musicians want to find the route with the maximal coolness. If you help them then Ilya will even give you his autograph!
Input
In the first line there are integers n and m (2 ≤ n, m ≤ 10 5), separated by space. These are the numbers of vertical and horizontal streets, respectively.
In the following n lines there are popularity values (one value on each line) on vertical streets in the order from left to right.
In the following m lines there are popularity values (one value on each line) on horizontal streets in the order from top to bottom.
It is guaranteed that all popularity values are integers from 1 to 10 9.
Output
Output a single integer which is a maximal possible route coolness.
Example
input | output |
---|---|
2 3 4 8 2 7 3 | 4 |
4 3 12 4 12 3 21 5 16 | 12 |
Notes
Explanation of the first sample (the "coolest" route is highlighted):
https://www.cnblogs.com/StupidBoy/p/5241258.html
题意:一个方格图,共有n+m条线,一个人从左上角走到右下角,问使路径上最小值最大。
分析:
这个人要么先横着走,走到某个点,然后下到最底部,然后再横着走到终点
要么先竖着走,走到某个点,然后横着走到最右部,然后再竖着走到终点
以上直觉是对的,但是我证明了下
证明:如果最优路径不是这样的
假设最优路径是先竖着走(跟先横着走是对称情况,同理可以证明先横着走的)
那么它一定要在某个点横着走的,
也就是说,前两步我们可以跟答案一样,
考虑到最优路径跟我们不一样,
如果最优路径的最后一步是竖着走的,那么我们的路径走过的边最优路径都走了,答案不可能比其劣
如果最优路径最后一步是横着走的,那么我们可以先竖着走到最下部,然后横着走,答案也不可能比其劣
也就说我们这样走的方法就是最优答案
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <functional>
#include <cstdio>
#include <cmath>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
#define ll long long
#define F first
#define S second
#define p_b push_back
#define m_p make_pair
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int M = 1e7 + 7;
const int N = 1e6 + 7;
int v[N], h[N];
int main() {
int n, m;
scanf ("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf ("%d", &v[i]);
}
for (int i = 1; i <= m; ++i) {
scanf ("%d", &h[i]);
}
int ans = -INF, tmp;
ans = max(ans, min(v[1], h[m]));
ans = max(ans, min(h[1], v[n]));
tmp = -INF;
for (int i = 2; i < n; ++i) {
tmp = max(tmp, v[i]);
}
ans = max(ans, min(tmp, min(h[1], h[m])));
tmp = -INF;
for (int i = 2; i < m; ++i) {
tmp = max(tmp, h[i]);
}
ans = max(ans, min(tmp, min(v[1], v[n])));
printf ("%d\n", ans);
return 0;
}