H - Perfect Ban (模擬)

題目鏈接:https://vjudge.net/problem/Gym-101341H

Constantine and Mike are playing the board game «Wrath of Elves». There are n races and m classes of characters in this game. Each character is described by his race and class. For each race and each class there is exactly one character of this race and this class. The power of the character of the i-th race and the j-th class equals to aij, and both players know it perfectly.

Now Constantine will choose a character for himself. Before that Mike can ban one race and one class so that Constantine would not be able to choose characters of this race or of this class. Of course, Mike does his best to leave Constantine the weakest possible character, while Constantine, on the contrary, chooses the strongest character. Which race and class Mike should ban?

Input

The first line contains two integers n and m (2 ≤ n, m ≤ 1000) separated by a space — the number of races and classes in the game «Wrath of Elves», correspondingly.

The next n lines contain m integers each, separated by a space. The j-th number in the i-th of these lines is aij (1 ≤ aij ≤ 109).

Output

In the only line output two integers separated by a space — the number of race and the number of class Mike should ban. Races and classes are numbered from one. If there are several possible answers, output any of them.

Examples

Input

2 2
1 2
3 4

Output

2 2

Input

3 4
1 3 5 7
9 11 2 4
6 8 10 12

Output

3 2

題目大意:隨意刪除一行一列,使矩陣剩下元素中的最大值是所有刪除情況的最小情況。(哇 感覺自己的語言非常到位)

思路概括:根據貪心的思想,我們所刪除的行和列應該是儘可能的包含可能多的大數,我們首先確定矩陣最大元素所在的行和列,那這個最大值我們是一定要刪除的,所以下一個要尋找的應該是分別除去最大值所在行 列的次大值(如果只單純的除去最大值找次大值的話,會忽略一種情況,就是我們的次大值和最大值在同一行同一列的情況,這樣的話,我們必然是要刪除最大值的行或者列的,這個次大值就沒有了意義,偷偷告訴大家,這種情況就是這個題的樣例8哦,我可是wa了好多發呢,qwq),我們找到次大值之後, 我們肯定是要刪除的是(最大值的行,次大值的列) 或者 (次大值的行,最大值的列) 這其中的一種情況的, 那我們怎麼判斷刪除哪種呢, 我們就可以非常直接的暴力跑這兩種情況中矩陣剩下的元素哪種情況的小,這樣就結束啦!!(撒花 撒花) 嘻嘻~

附上我千錘百煉的ac代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;

ll a[1050][1050];
ll n, m;

int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    ll minx = -INF;
    ll x = 0, y = 0;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            cin >> a[i][j];
            if(a[i][j] > minx)
            {
                minx = a[i][j];
                x = i;
                y = j;
            }
        }
    }//找出最大值的行和列



    ll maxx1 = -INF, maxx2 = -INF;
    ll xx = 0, yy = 0;
    ll xx2 = 0, yy2 = 0;
    for(int i=1; i<=n; i++)
    {
        if(i == x) continue;
        for(int j=1; j<=m; j++)
        {
            if(a[i][j] > maxx1)
            {
                maxx1 = a[i][j];
                xx = i;
                yy = j;
            }
        }
    }//不算最大值的行找出的次大值
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(j == y) continue;
            if(a[i][j] > maxx2)
            {
                maxx2 = a[i][j];
                xx2 = i;
                yy2 = j;
            }
        }
    }//不算最大值的列找出的最大值

    if(maxx1 > maxx2) xx = xx2, yy = yy2;//判斷不要行還是不要列



    ll minx1 = -INF, minx2 = -INF;
    for(int i=1; i<=n; i++)
    {
        if(i == x) continue;
        for(int j=1; j<=m; j++)
        {
            if(j == yy) continue;
            if(a[i][j] > minx1)
            {
                minx1 = a[i][j];
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(i == xx) continue;
        for(int j=1; j<=m; j++)
        {
            if(j == y) continue;
            if(a[i][j] > minx2)
            {
                minx2 = a[i][j];
            }
        }
    }//兩種情況分別找出的次次大值
    if(minx1 > minx2)  cout << xx << ' ' << y << endl;
    else cout << x <<' ' << yy << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章