Educational Codeforces Round 80 D. Minimax Problem

http://codeforces.com/contest/1288/problem/D

 

题意:

给出一张n*m的表a[n][m],m<=8,从中任选两行i,j

令b[k]=max(a[i][k],a[j]k])

 

 

最大化 min(b[k])

 

 

二分答案x

若a[i][j]>=x,令c[i][j]=1,否则c[i][j]=0

若最终答案>=x,则存在i,j,满足c中第i行和第j行 按位或 的结果都是1

枚举行肯定超时

m<=8,每一行的01状态不超过256种,所以可以枚举行的01状态

可以用bitset做

 

#include<cmath>
#include<cstdio>
#include<bitset>
#include<cstring>
#include<iostream>

using namespace std;

#define N 300001

int n,m,a[N][8];
bitset<8>b[N];

int vis[256]; 
int use[256][256],siz[256];

int all;

int turn[N];

bool check(int x)
{
     memset(vis,0,sizeof(vis));
     for(int i=1;i<=n;++i) 
     {
         b[i].reset();
        for(int j=0;j<m;++j)
            if(a[i][j]>=x) b[i].set(j);
         vis[b[i].to_ulong()]++; 
     }
     for(int i=0;i<=all;++i)
         if(vis[i])
             for(int j=1;j<=siz[i];++j)
                 if(vis[use[i][j]]) return true;
     return false;
}

void solve(int x)
{
     memset(vis,0,sizeof(vis));
     for(int i=1;i<=n;++i) 
     {
         b[i].reset();
        for(int j=0;j<m;++j)
            if(a[i][j]>=x) b[i].set(j);
         vis[b[i].to_ulong()]++; 
         turn[i]=b[i].to_ulong();
     }
     int p=-1,q;
     for(int i=0;i<=all && p==-1;++i)
         if(vis[i])
             for(int j=1;j<=siz[i];++j)
                 if(vis[use[i][j]])
                 {
                     p=i;
                     q=use[i][j];
                     break;
                }
     int pp,qq;            
     for(int i=1;i<=n;++i)
     {
         if(turn[i]==p) pp=i;
         if(turn[i]==q) qq=i;
     }
     printf("%d %d",pp,qq);
} 

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
        for(int j=0;j<m;++j)
            scanf("%d",&a[i][j]);
    int l=0,r=1e9,mid,mx;
    all=pow(2,m)-1;
    for(int i=0;i<=all;++i)
         for(int j=i;j<=all;++j)
             if((i|j)==all) use[i][++siz[i]]=j;
    while(l<=r)
    {
        mid=l+r>>1;
        if(check(mid)) mx=mid,l=mid+1;
        else r=mid-1;
    }
    solve(mx);
    return 0;    
}

 

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 mm integers, 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
posted @ 2020-01-20 20:34  TRTTG  阅读(155)  评论(0编辑  收藏  举报