[GDCPC2023] Path Planning mex(二分)

https://www.luogu.com.cn/problem/P9698?contestId=135929

二分区间mex,将0-x全部坐标存入,因为只能向下走,向右走,那么将下标x递增排序,y必然不可出现逆序对

#pragma GCC optimize(3,"Ofast","inline")
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll; 
#define int long long
#define ld long double
int n,m;
int cnt=0;
array<int,2> b[1000010],c[1000100];
bool cmp(array<int,2> z,array<int,2> y){
if(z[0]==y[0]) return z[1]<y[1];
	return z[0]<y[0];
}
bool check(int x){
int id=0;
	for(int i=0;i<=x;i++){
		c[i]=b[i];
	}
	sort(c,c+1+x,cmp);
	int ix=-1;
	for(int i=0;i<=x;i++){
		if(c[i][1]<ix) return 0;
		ix=max(ix,c[i][1]);
	}return 1;
}
void solve()
{ cin>>n>>m;

vector<vector<int>>a(n+10,vector<int>(m+10));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
	{cin>>a[i][j];
	b[a[i][j]]={i,j};
	}
	int l=0,r=n*m-1;
	while(l<r){
		int mid=(l+r+1)/2;
		if(check(mid)) l=mid;
		else r=mid-1;
	}
cout<<l+1<<endl;
}

signed main()
{
	ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
	int t; cin>>t;
	while(t--)
{
	solve();
}
	
	
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值