codeforces 1288D. Minimax Problem (two points)

Link: https: //codeforces.com/contest/1288/problem/D

D. Minimax Problem

The meaning of problems: Given n arrays, length m, from the selection of two arrays array n, whichever is the maximum value of each one consisting of two arrays in a new array, a new array is referred to as a minimum the maximum value of c, c, all combinations

 

Ideas: 8 m ranges only, the scope of the array elements is 1E9, m ranges apparently very specific, it seems to be compressed by the state array in binary form, where the answer to a method bipartite title. Dichotomy in the range of minimum to maximum array elements, each Check (mid) again, when Check for each array, represented in binary form, a value less than mid array is represented by 0, 1 or greater with the mid represents, when all the arrays are carried out binary conversion, such as an array 1 2 3 4 5, mid = 3, then the state of the array can be compressed to 00,111. Then enumerated to binary, so that in fact it will only enumerate 2 m × 2 m times, if two binary phase enumeration or 11111111, then that set of solutions found, continue to return half, so the process time complexity only O (n-m + 2 × m × 2 m ), the range of only 8 m. Specific look at the code

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<queue> 
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn = 3e5+5;
10 int n,m;
11 int cnt[maxn];
12 int a[maxn][10];
13 int num[270];
14 intANS1, ANS2;
 15  BOOL Check ( int CUR) {
 16      Memset (NUM, 0 , the sizeof (NUM)); // NUM array initialization 
. 17      for ( int I = . 1 ; I <= n-; I ++ ) {
 18 is          int X = 0 ;
 . 19          for ( int J = m - . 1 ; J> = 0 ; J, ) {
 20 is              IF (A [I] [J]> = CUR) = X + ( . 1 << J); /// statistics meet arrays a [i] [j]> = cur of every 
21          }
 22         NUM [X] = I; // each array are converted to binary 
23 is      }
 24      for ( int I = 0 ; I <( . 1 << m); I ++ ) {
 25          for ( int J = 0 ; J <( . 1 m <<); J ++ ) {
 26 is              IF (NUM [I] =! 0 && NUM [J] =! 0 && (J | I) == ( . 1 << m) - . 1 ) { // enumeration binary form All number 
27                  ANS1 = NUM [I];
 28                  ANS2 = NUM [J];
 29                  return  to true;
30             }
31         }
32     }
33     return false;
34 }
35 int main(){
36     int r = -1,l = 1e9+10;
37     scanf("%d%d",&n,&m);
38     for(int i = 1;i<=n;i++){
39         for(int j = 0;j<m;j++){
40             scanf("%d",&a[i][j]);
41             r = max(r,a[i][j]);
42             l = min(l,a[i][j]);
43         }
44     }
45     int mid;
46     while(l<r){//二分答案 
47         mid = (l+r+1)/2;
48         if(check(mid)) l = mid ;
49         else r = mid - 1;
50     }
51     check(l);
52     printf("%d %d",ans1,ans2);
53     return 0;
54 }

Guess you like

Origin www.cnblogs.com/AaronChang/p/12210870.html