POJ3685 Matrix(嵌套二分)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(嵌套二分)的主要内容,如果未能解决你的问题,请参考以下文章