D. Minimax Problem(二分+二进制)

D. Minimax Problem
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given nn arrays a1a1, a2a2, ..., anan; each array consists of exactly mm integers. We denote the yy-th element of the xx-th array as ax,yax,y.

You have to choose two arrays aiai and ajaj (1i,jn1≤i,j≤n, it is possible that i=ji=j). After that, you will obtain a new array bb consisting of mmintegers, such that for every k[1,m]k∈[1,m] bk=max(ai,k,aj,k)bk=max(ai,k,aj,k).

Your goal is to choose ii and jj so that the value of mink=1mbkmink=1mbk is maximum possible.

Input

The first line contains two integers nn and mm (1n31051≤n≤3⋅105, 1m81≤m≤8) — the number of arrays and the number of elements in each array, respectively.

Then nn lines follow, the xx-th line contains the array axax represented by mm integers ax,1ax,1, ax,2ax,2, ..., ax,max,m (0ax,y1090≤ax,y≤109).

Output

Print two integers ii and jj (1i,jn1≤i,j≤n, it is possible that i=ji=j) — the indices of the two arrays you have to choose so that the value of mink=1mbkmink=1mbk is maximum possible. If there are multiple answers, print any of them.

Example
input
Copy
6 5
5 0 3 1 2
1 8 9 1 3
1 2 3 4 5
9 1 0 3 7
2 3 0 6 3
6 4 1 7 0
output
Copy
1 5

 

 

#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<string>
#define met(a,x) memset(a,x,sizeof(a));
#define rep(i,a,b) for(ll i = a;i <= b;i++)
#define bep(i,a,b) for(ll i = a;i >= b;i--)
#define lowbit(x) (x&(-x))
// #define mid ((l + r) >> 1)
// #define len (r - l + 1)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define pb push_back
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
typedef unsigned long long ull;
typedef pair<int, int>Pi;
typedef pair<ll, pair<ll, ll> > Pii;
const ll inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const ll maxn = 1000100;
const ll mod = 1000000007;
int a[maxn][10];
int n, m,xx,yy;
int ok(int x) {
    int vis[300] = { 0 };
    rep(i,1,n){
        int now = 0;
        rep(j,1,m){
            if(a[i][j] >= x)now = now*2 + 1;
            else now = now*2;
        }
        vis[now] = i;
    }
    rep(i, 0, 255) {
        rep(j, 0, 255) {
            if (vis[i] && vis[j] && ((i|j) == ((1 << m) - 1))) {
                xx = vis[i];
                yy = vis[j];
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    scanf("%d%d",&n,&m);
    rep(i, 1, n) {
        rep(j, 1, m) {
            scanf("%d",&a[i][j]);
        }
    }
    int l = 0, r = 1e9;
    while (l <= r) {
        int mid = (l + r) / 2;
        if (ok(mid)) l = mid + 1;
        else r = mid - 1;
    }
    cout << xx << ' ' << yy << endl;
    return 0;
}
View Code

 

posted @ 2020-02-01 22:09  cherish__lin  阅读(175)  评论(0编辑  收藏  举报