ural 2069 Hard Rock

題意:給出n條縱向的路,m條橫向的路,走過路徑的最小邊權值爲路徑權值,求最大路徑權值。

數據量很大,但是其實只有四種情況有可能走出最有解:第一條橫向和最後一條縱向,第一條縱向和最後一條橫向,最大橫向和第一條、最後一條縱向,最大縱向和第一條、最後一條橫向,找最大值就可以了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int h[100005], w[100005];
int main() {
    int n, m, i, j;
    while(~scanf("%d%d", &n, &m)) {
        int mh = 0;
        int mw = 0;
        for(i = 0; i < n; i++) {
            scanf("%d", &h[i]);
            if(h[mh] < h[i])
                mh = i;
        }
        for(j = 0; j < m; j++) {
            scanf("%d", &w[j]);
            if(w[mw] < w[j])
                mw = j;
        }
        int ans = max(min(h[0], w[m - 1]), min(w[0], h[n - 1]));
        ans = max(ans, min(w[0], min(w[m - 1], h[mh])));
        ans = max(ans, min(w[mw], min(h[0], h[n - 1])));
        printf("%d\n", ans);

    }
    return 0;
}


發佈了67 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章