Educational Codeforces Round 80 (Rated for Div. 2) D. Minimax Problem

D. Minimax Problem
考虑二分答案,对于二分出的某个值x, 先将原数组处理成二进制的形式,大于x的位为1, 这样二进制数上限为2^8-1=255,然后暴力check。

#include
#define ll long long
#define PB push_back
#define endl '\n'
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ull unsigned long long
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define lowbit(x) (x & (-x))
#define rep(i, a, b) for(int i = a ; i <= b ; ++ i)
#define per(i, a, b) for(int i = b ; i >= a ; -- i)
#define clr(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn =3e5 + 10;
const ll mod = 1e9 + 7;
int n, m, u, v;
int a[maxn][10], vis[300];
bool check(int x){
     
    clr(vis, 0);
    rep(i, 1, n){
     
        int t = 0;
        rep(j, 1, m){
     
            if(a[i][j] >= x) t |= 1 << (j - 1);
        }
        vis[t] = i;
    }
    rep(i, 1, 256){
     
        rep(j, 1, 256){
     
            if(vis[i] && vis[j] && (i | j) == (1 << m) - 1){
     
                u = vis[i]; v = vis[j]; return true;
            }
        }
    }
    return false;
}
int main()
{
     
    cin >> 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) >> 1;
        if(check(mid)) l = mid + 1;
        else r = mid - 1;
    }
    cout << u << ' ' << v;
    return 0;
}

你可能感兴趣的:(比赛题解)