bzoj 4443: [Scoi2015]小凸玩矩阵
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 149 Solved: 81
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 5 6 6
8 3 4 3
6 8 6 3
Sample Output
HINT
1<=K<=N<=M<=250,1<=矩阵元素<=10^9
题解:
N个数中的第K大,就是第N-K+1小,这点要是没看见就毁了。设这个数为x,二分这个数,判断是否合法。
判断合法的方法,N^2枚举矩阵中的每个数,如果这个a[i][j]数小于等于x,让i连一条到j,容量为1的边。因为要保证N个数的i,j各不相同,所以设行i为x集,列j为y集,所有边权均为1,做最大流,也就是二分图的最大匹配。如果跑出来的maxflow大于等于N-K+1,说明x满足条件。注意以上两个不等关系都是大于等于,因为要考虑这样一种情况,整个矩阵的数字都是1,第1小是1,第N小还是1。我被这个坑了好久。。。
/**************************************************************
Problem: 4443
User: __abcdef__
Language: C++
Result: Accepted
Time:216 ms
Memory:14640 kb
****************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef long long LL;
const int inf=1e9,maxn=;
int N,M,K,S,T,tot,MIN=inf,MAX;
int a[maxn][maxn],tmp[maxn][maxn];
struct MAT{
int x,y,v,f;
}mat[maxn*maxn];
inline int cmp(const MAT & e,const MAT & w){
return e.v<w.v;
} struct Edge{
int to,rest,next;
}e[maxn*maxn*];
int head[maxn*maxn],cnt=;
inline void Addedge(int x,int y,int r){
e[++cnt].to=y; e[cnt].rest=r; e[cnt].next=head[x]; head[x]=cnt;
e[++cnt].to=x; e[cnt].rest=; e[cnt].next=head[y]; head[y]=cnt;
} int dis[maxn*maxn];
inline bool BFS(){
memset(dis,,sizeof(dis));
static queue<int> Q;
while(!Q.empty()) Q.pop();
Q.push(S); dis[S]=;
while(!Q.empty()){
int x=Q.front(); Q.pop();
for(int i=head[x];i;i=e[i].next){
int y=e[i].to;
if(dis[y]==&&e[i].rest){
dis[y]=dis[x]+;
Q.push(y);
}
}
}
if(dis[T]!=) return true;
return false;
}
inline int DFS(int x,int flow){
if(x==T) return flow;
int now=,temp;
for(int i=head[x];i;i=e[i].next){
int y=e[i].to;
if(dis[y]==dis[x]+&&e[i].rest){
temp=DFS(y,min(flow-now,e[i].rest));
e[i].rest-=temp;
e[i^].rest+=temp;
now+=temp;
if(now==flow) return flow;
}
}
if(!now) dis[x]=;
return now;
} inline int dinic(){
int ans=;
while(BFS()==true) ans+=DFS(S,inf);
return ans;
} inline bool jud(int x){ for(int i=;i<=cnt+;i++) e[i].to=e[i].rest=e[i].next=;
memset(head,,sizeof(head)); cnt=;
S=; T=N+M+;
for(int i=;i<=N;i++) Addedge(S,i,);// S 连向 x集 权值为 1
for(int i=;i<=M;i++) Addedge(N+i,T,);//y集 连向 T 权值为 1
for(int i=;i<=N;i++){// x集 连向 y集
for(int j=;j<=M;j++){
if(tmp[i][j]<=x){
Addedge(i,N+j,);
}
}
}
int maxflow=dinic();
if(maxflow>=N-K+) return true;
else return false;
}
inline int find(int l,int r){
if(l+>=r){
if(jud(l)==true) return l;
else return r;
}
int mid=(l+r)>>;
if(jud(mid)==true) return find(l,mid);
else return find(mid+,r);
}
int main(){
scanf("%d%d%d",&N,&M,&K);
for(int i=;i<=N;i++){
for(int j=;j<=M;j++){
scanf("%d",&a[i][j]);
mat[++tot].x=i; mat[tot].y=j; mat[tot].v=a[i][j];
}
}
tot=;
sort(mat+,mat+N*M+,cmp);
for(int i=;i<=N*M;i++){
if(mat[i].v!=mat[i-].v) mat[i].f=++tot;
else mat[i].f=tot;
}
for(int i=;i<=N*M;i++){
tmp[mat[i].x][mat[i].y]=mat[i].f;
MIN=min(MIN,mat[i].f); MAX=max(MAX,mat[i].f);
}
int ggg=find(MIN,MAX);
for(int i=;i<=N;i++){
for(int j=;j<=M;j++){
if(tmp[i][j]==ggg){
printf("%d\n",a[i][j]);
return ;
}
}
}
return ;
}
bzoj 4443: [Scoi2015]小凸玩矩阵的更多相关文章
- BZOJ 4443: [Scoi2015]小凸玩矩阵 最大流
4443: [Scoi2015]小凸玩矩阵 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4443 Description 小凸和小方是好 ...
- bzoj 4443 [Scoi2015]小凸玩矩阵 网络流,二分
[Scoi2015]小凸玩矩阵 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1564 Solved: 734[Submit][Status][Di ...
- 【刷题】BZOJ 4443 [Scoi2015]小凸玩矩阵
Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个数字不能在同一行或同一列,现小凸想知道选出来的N个数中第K大的数字的最 ...
- BZOJ 4443 [Scoi2015]小凸玩矩阵(二分答案+二分图匹配)
[题目链接]http://www.lydsy.com/JudgeOnline/problem.php?id=4443 [题目大意] 从矩阵中选出N个数,其中任意两个数字不能在同一行或同一列 求选出来的 ...
- BZOJ 4443: [Scoi2015]小凸玩矩阵 二分图最大匹配+二分
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=4443 题解: 二分答案,判断最大匹配是否>=n-k+1: #include< ...
- 2018.06.30 BZOJ4443: [Scoi2015]小凸玩矩阵(二分加二分图匹配)
4443: [Scoi2015]小凸玩矩阵 Time Limit: 10 Sec Memory Limit: 128 MB Description 小凸和小方是好朋友,小方给小凸一个N*M(N< ...
- BZOJ_4443_[Scoi2015]小凸玩矩阵_二分+二分图匹配
BZOJ_4443_[Scoi2015]小凸玩矩阵_二分+二分图匹配 Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个 ...
- 【BZOJ4443】[Scoi2015]小凸玩矩阵 二分+二分图最大匹配
[BZOJ4443][Scoi2015]小凸玩矩阵 Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个数字不能在同一行或 ...
- 图论(网络流):[SCOI2015]小凸玩矩阵
Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个数字不能在同一行或同一列,现小凸想知道选出来的N个数中第K大的数字的最 ...
随机推荐
- Android 判断当前是否在跑monkey测试
/** * Returns true if Monkey is running. */ public static boolean isMonkeyRunning() { ...
- 《IT项目经理成长手记》
下载地址: https://download.csdn.net/download/h623691640/9109265 读后感: 曾担任过试用期3个月项目经理,遇到的问题确实如书中所说,几乎有一些是一 ...
- 【BZOJ4515】[Sdoi2016]游戏 树链剖分+线段树
[BZOJ4515][Sdoi2016]游戏 Description Alice 和 Bob 在玩一个游戏. 游戏在一棵有 n 个点的树上进行.最初,每个点上都只有一个数字,那个数字是 1234567 ...
- angular -- 无刷新做分页
无刷新做分页参考地址: http://www.jq22.com/demo/angular201707111100/ 示例代码: <!DOCTYPE html> <html lang= ...
- thinkphp --- 写入日志
在开发过程中,对于一些参数,不好直接输入或者打印调试,特别是在微信开发过程中,这个时候,通过日志来查看信息就显得格外重要. 下面是在TP3.2.3框架中,写入日志的方法: public functio ...
- 遍历DataSet
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- SQL Fundamentals:替代变量(&,&&)以及DEFINE,UNDEFINE,ACCEPT指令
替代变量 利用替代变量可以实现数据操作的交互性.替代变量的操作类似于键盘输入操作. 所谓的替代变量,指的就是在进行查询或更新操作时,某些数据是由用户所输入的,而这些数据前可以使用“&”标记. ...
- Fatal error in launcher: Unable to create process using '"c:\python37\python3.exe" "C:\Python37\Scripts\pip3.exe" install opencv-python'
pip3.exe install opencv-python 报错: Fatal error in launcher: Unable to create process using '"c: ...
- python网络编程知识体系
python的网络编程包括: 1.mvc-socket-线程-进程-并发-IO异步-消费者生产者 2.mysql-paramiko-审计堡垒机-redis-分布式监控 线程.进程 和 协程 原理剖析 ...
- 安装Ruby、Sass在WebStrom添加Watcher实现编辑scss文件时自动生成.map和压缩后的.css文件
前言 这段时间一直在看Bootstrap,V3官方直接提供了Less版本的源码,就先将Less学完了,很简单的语法,学习写Demo都是在Webstorm里写的,配置了Watcher自动编译(详见< ...