码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3685 Matrix(嵌套二分)

时间:2016-10-02 06:39:09      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

同行元素递减,同列元素递增,采用嵌套二分的方法

 

 

#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(嵌套二分)

标签:

原文地址:http://www.cnblogs.com/IMGavin/p/5926692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!