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

POJ 3685 二分

时间:2017-07-24 14:47:16      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:als   bool   nbsp   miss   row   span   mon   memory   hose   

Matrix
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 7034   Accepted: 2071

Description

Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i2 + 100000 × i + j2 - 100000 × j + i × j, you are to find the M-th smallest element in the matrix.

Input

The first line of input is the number of test case.
For each test case there is only one line contains two integers, N(1 ≤ N ≤ 50,000) and M(1 ≤ M ≤ N × N). There is a blank line before each test case.

Output

For each test case output the answer on a single line.

Sample Input

12

1 1

2 1

2 2

2 3

2 4

3 1

3 2

3 8

3 9

5 1

5 25

5 10

Sample Output

3
-99993
3
12
100007
-199987
-99993
100019
200013
-399969
400031
-99939

Source

题意:
有n*n的矩阵,A(ij)的值是 i2 + 100000 × i + j2 - 100000 × j + i × j,求矩阵中第k小的数。
输入 t
输入t行n k
输出结果
代码:
//和上一题差不多,当j固定时表达式随i的增大而增大,先二分第k小数的值s,然后枚举j,
//找小于等于s的i的个数有几个,用这个值与k比较来二分。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int t;
ll n,k;
ll make(ll i,ll j){
    return i*i+j*j+i*100000-j*100000+i*j;
}
bool solve(ll m){
    ll cnt=0;
    for(int j=1;j<=n;j++){
        int l=1,r=n,ans=0;
        while(l<=r){
            int i=(l+r)>>1;
            if(make(i,j)<=m){
                ans=i;
                l=i+1;
            }else r=i-1;
        }
        cnt+=ans;
    }
    return cnt>=k;
}
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&n,&k);
        ll l=-100000*n;
        ll r=n*n+n*n+100000*n+n*n,ans;
        while(l<=r){
            ll m=(l+r)>>1;
            if(solve(m)){
                ans=m;
                r=m-1;
            }else l=m+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

POJ 3685 二分

标签:als   bool   nbsp   miss   row   span   mon   memory   hose   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/7228335.html

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