Description:
You are given n n n arrays a 1 , a 2 , . . . , a n a_{1}, a_{2}, ..., a_{n} a1,a2,...,an; each array consists of exactly m integers. We denote the y − t h y-th y−th element of the x − t h x-th x−th array as a x , y a_{x,y} ax,y.
You have to choose two arrays KaTeX parse error: Expected '}', got 'EOF' at end of input: a_{i and KaTeX parse error: Expected '}', got 'EOF' at end of input: a_{j ( 1 ≤ i , j ≤ n 1≤i,j≤n 1≤i,j≤n, it is possible that i = j i=j i=j). After that, you will obtain a a a new array b b b consisting of m m m integers, such that for every k ∈ [ 1 , m ] , b k = m a x ( a i , k , a j , k ) k∈[1,m] ,b_{k}=max(a_{i,k},a_{j,k}) k∈[1,m],bk=max(ai,k,aj,k).
Your goal is to choose i i i and j j j so that the value of m i n k = 1 m b k min_{k=1}^{m}b_{k} mink=1mbk is maximum possible.
Input
The first line contains two integers n n n and m m m ( 1 ≤ n ≤ 3 ⋅ 1 0 5 , 1 ≤ m ≤ 8 1≤n≤3⋅10^5, 1≤m≤8 1≤n≤3⋅105,1≤m≤8) — the number of arrays and the number of elements in each array, respectively.
Then n n n lines follow, the x − t h x-th x−th line contains the array a x a_{x} ax represented by m integers a x , 1 , a x , 2 , . . . , a x , m ( 0 ≤ a x , y ≤ 1 0 9 ) a_{x,1}, a_{x,2}, ..., a_{x,m} (0≤a_{x,y}≤10^9) ax,1,ax,2,...,ax,m(0≤ax,y≤109).
Output
Print two integers i i i and j j j ( 1 ≤ i , j ≤ n 1≤i,j≤n 1≤i,j≤n, it is possible that i = j i=j i=j) — the indices of the two arrays you have to choose so that the value of m i n k = 1 m b k min_{k=1}^{m}b_{k} mink=1mbk is maximum possible. If there are multiple answers, print any of them.
Example
input
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
1 5
题意:
给出
n
n
n 个长度为
m
m
m 的数组,在两个数组中选取位置一样中大的那个元素,然后求新组成的数组中的元素最小值,然后求全部最小值中的最小值,输出是由哪两个数组组合成的。
二分答案,二进制优化,具体解释看代码吧。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if (a > 9)
Out(a / 10);
putchar(a % 10 + '0');
}
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);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
if (a >= mod)
a = a % mod + mod;
ll ans = 1;
while (b)
{
if (b & 1)
{
ans = ans * a;
if (ans >= mod)
ans = ans % mod + mod;
}
a *= a;
if (a >= mod)
a = a % mod + mod;
b >>= 1;
}
return ans;
}
// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
return qpow(a, p - 2, p);
}
///扩展欧几里得
int exgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int g = exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return g;
}
///使用ecgcd求a的逆元x
int mod_reverse(int a, int p)
{
int d, x, y;
d = exgcd(a, p, x, y);
if (d == 1)
return (x % p + p) % p;
else
return -1;
}
///中国剩余定理模板0
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
int M = 1, y, x = 0;
for (int i = 0; i < n; ++i) //算出它们累乘的结果
M *= a[i];
for (int i = 0; i < n; ++i)
{
int w = M / a[i];
int tx = 0;
int t = exgcd(w, a[i], tx, y); //计算逆元
x = (x + w * (b[i] / t) * x) % M;
}
return (x + M) % M;
}
int n, m;
int val[(1 << 8) + 10], pos[(1 << 8) + 10];
int a[300010][10];
int l = inf, r = -1;
int ans1, ans2;
bool judge(int x)
{
rep(i, 1, n)
{
int t = 0;
rep(j, 0, m - 1)
{
if (a[i][j + 1] < x)
t |= (1 << j); //对t在二进制下的第j位赋值为1,t是第二个序列选择的情况,
//第一个序列选择的都是比x大的数据,这样保证x是新序列最小的
}
if (val[t] >= x)
{
ans1 = i;
ans2 = pos[t];
if (t == 0)
ans2 = i;
return true;
}
}
return false;
}
int main()
{
mem(val, 0);
mem(pos, 0);
val[0] = inf;
sd(n);
sd(m);
rep(i, 1, n)
{
rep(j, 1, m)
{
sd(a[i][j]);
l = min(l, a[i][j]); //全部的最小值
r = max(r, a[i][j]); //全部的最大值
}
}
rep(i, 1, n)
{
rep(j, 1, (1 << m) - 1)
{ //j就是在每一个序列里一个有2^m种选取情况,然后对于每种情况求最小值
int temp = inf;
rep(k, 0, m - 1)
{
if (j & (1 << k)) //j的第k位是不是1
temp = min(temp, a[i][k + 1]);
}
if (temp > val[j])
{
val[j] = temp;
pos[j] = i;
}
}
}
int mid, ans = -1, t = l;
while (l <= r)
{
mid = (l + r) >> 1;
if (judge(mid))
l = mid + 1, ans = mid;
else
r = mid - 1;
}
if (ans == -1) //处理边界
ans = t;
judge(ans);
pdd(ans1, ans2);
return 0;
}