首页 > 代码库 > POJ3685 Matrix(嵌套二分)
POJ3685 Matrix(嵌套二分)
同行元素递减,同列元素递增,采用嵌套二分的方法
#include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<map>#include<queue>#include<vector>#include<cmath>#include<utility>using namespace std;typedef __int64 LL;LL n, m;LL F(LL l, LL r){ return l * l + 100000 * l + r * r - 100000 * r + l *r;}LL solve(LL x){ LL sum = 0; for(int i = 1; i <= n; i++){ int l = 1, r = n + 1; while(l < r){ LL mid = (l + r)>>1; if(F(mid, i) > x){ r = mid; }else{ l = mid + 1; } } sum += l - 1; } return sum;}int main(){ int t; cin>>t; while(t--){ cin>>n>>m; LL l = -100000*n, r = n*n+100000*n+n*n+n*n; while(l < r){ LL mid = (l + r) >> 1; if(solve(mid) < m){ l = mid + 1; }else{ r = mid; } } cout<<l<<‘\n‘; } return 0;}
POJ3685 Matrix(嵌套二分)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。