codeforces 1288D. Minimax Problem(二分)
链接:https://codeforces.com/contest/1288/problem/D
D. Minimax Problem
题意:给定n个数组,长度为m,从n中数组挑选两个数组,两个数组中的每一位取两者的最大值组成一个新的数组,新数组中的最小值记为c,所有组合中c的最大值
思路:题目中m的范围只有8,数组中元素的范围是1e9,显然m的范围非常特殊,似乎可以把数组用二进制的形式进行状态压缩,这里采用二分答案的方法。二分范围是数组元素最小值到最大值,每次check(mid)一遍,check的时候对于每个数组,用二进制的形式表示出来,数组中小于mid的值用0表示,大于等于mid的用1表示,此时所有的数组都进行了二进制转化,比如一个数组1 2 3 4 5,mid = 3,然后数组就可以状态压缩为0 0 1 1 1 。然后去枚举这些二进制,这样其实最多只会枚举2m × 2m 次,如果枚举的两组二进制相或为11111111,那么说明找到了一组解,返回继续二分,如此过程时间复杂度只有O(n×m + 2m×2m),m的范围只有8。具体看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 3e5+;
int n,m;
int cnt[maxn];
int a[maxn][];
int num[];
int ans1,ans2;
bool check(int cur){
memset(num,,sizeof(num));//初始化num数组
for(int i = ;i<=n;i++){
int x = ;
for(int j = m - ;j>=;j--){
if(a[i][j]>=cur) x+=(<<j);///统计满足a[i][j]>=cur的数组的每一位
}
num[x] = i;//每个数组都转化为二进制
}
for(int i = ;i<(<<m);i++){
for(int j = ;j<(<<m);j++){
if(num[i]!= && num[j]!= && (j|i) == (<<m)-){//枚举二进制形式的所有数
ans1 = num[i];
ans2 = num[j];
return true;
}
}
}
return false;
}
int main(){
int r = -,l = 1e9+;
scanf("%d%d",&n,&m);
for(int i = ;i<=n;i++){
for(int j = ;j<m;j++){
scanf("%d",&a[i][j]);
r = max(r,a[i][j]);
l = min(l,a[i][j]);
}
}
int mid;
while(l<r){//二分答案
mid = (l+r+)/;
if(check(mid)) l = mid ;
else r = mid - ;
}
check(l);
printf("%d %d",ans1,ans2);
return ;
}
codeforces 1288D. Minimax Problem(二分)的更多相关文章
- Codeforces 1288D - Minimax Problem
题目大意: 给定n个序列,每个序列元素个数严格相等于m 你需要找到两个序列a[i]和a[j],使其每个对应位置的元素取大后得到b序列 b[k]=max(a[i][k],a[j][k]) 且让b序列中 ...
- D. Minimax Problem(二分+二进制)
D. Minimax Problem time limit per test 5 seconds memory limit per test 512 megabytes input standard ...
- 【codeforces】Educational Codeforces Round 80 D. Minimax Problem——二分+二进制处理
题目链接 题目大意 有n个维度为m的向量,取其中两个进行合并,合并时每个维度取两者之间的较大者,得到的新的向量中,维度值最小者最大为多少 分析 首先最需要注意的是m的取值,m最大只有8 那么我们可以二 ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- Codeforces 1114E - Arithmetic Progression - [二分+随机数]
题目链接:http://codeforces.com/problemset/problem/1114/E 题意: 交互题,有一个 $n$ 个整数的打乱顺序后的等差数列 $a[1 \sim n]$,保证 ...
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
- Codeforces 807C - Success Rate(二分枚举)
题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再 ...
- CodeForces - 348A Mafia (巧妙二分)
传送门: http://codeforces.com/problemset/problem/348/A A. Mafia time limit per test 2 seconds memory li ...
- Codeforces 702C Cellular Network(二分)
题目链接:http://codeforces.com/problemset/problem/702/C 题意: 在数轴上有N个城市和M个信号塔,给你这N个城市以及M个塔在数轴上的位置,求M个塔可以覆盖 ...
随机推荐
- codechef Scoring Pairs
难度 \(medium-hard\) 题意 官方中文题意 做法 很显然是可以通过计算常数个\(sum(A,B)=\sum\limits_{i=0}^A \sum\limits_{j=0}^B scor ...
- SQL Server 2012 下载和安装详细教程
https://blog.csdn.net/qq_37591637/article/details/93102794 选择图片中的三个,然后点击下载 ,文件内存很大 下载以后,如图所示,双击.exe程 ...
- .net mvc接收参数为null的解决方案
1.通过对象接收请求数据时的null 必须为对象的属性设置get与set private System.String _EMail = System.String.Empty; public Syst ...
- LeetCode 852. 山脉数组的峰顶索引 (二分)
题目链接:https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/ 我们把符合下列属性的数组 A 称作山脉: A.length ...
- centos8 samba
安装dnf install -y samba samba-client开机启动systemctl enable smb立即启动systemctl start smb防火墙放行firewall-cmd ...
- 在 myeclipse 引入项目
1.进入 myeclipse 在界面空白处,右键,出现如下图所示 2.选择 Import...,弹出如下图所示界面 3.双击上图红框内的内容,弹出如下图所示界面,然后点击按钮“Browse...”,选 ...
- ZedGraph怎样在双击图形后添加箭头标记
场景 在ZedGraph的曲线图上,双击图时会在图形上生成箭头符号标记. 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的 ...
- rabbitmq系列问题解决:406, "PRECONDITION_FAILED - inequivalent arg 'durable'
1. 安装rabbitmq,查看官网文档: https://www.rabbitmq.com/#getstarted 由于我是先安装了rabbitmq后自己随手创建了queue,后面又按照官方给的&q ...
- loj6277 数列分块入门题1
裸题分块. #include <bits/stdc++.h> using namespace std; ],b[],n,m,t1,t2,t3,t4,sq; int main(){ ios: ...
- Selenium3+python自动化012-测试用例模块化
测试用例模块化特点:为po模型做准备. 1.提取公共方法. 2.提取数据. 3.提取逻辑. # @Author:lsj # @version V1.0 # -*- coding:UTF-8 -*- i ...