zoukankan      html  css  js  c++  java
  • Matrix (二分套二分

    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

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int t;
    long long n,k;
    long long make(long long i,long long j){
        return i*i+j*j+i*100000-j*100000+i*j;
    }
    bool erfen(long long m){
        long long 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(){
        cin>>t;
        for(int w=0;w<t;w++){
            cin>>n>>k;
            long long l=-100000*n;
            long long r=n*n+n*n+100000*n+n*n,ans;
            while(l<=r){//外层二分 
                long long m=l+r>>1;
                if(erfen(m)){ 
                    ans=m;
                    r=m-1;
                }else l=m+1;
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    C#3.0分部份方法
    欢迎提议
    控制台关闭的特殊处理
    好头晕
    c#如何计算当月季度时间段
    CMMI定义(转)
    SQL Server 聚集索引和非聚集索引的区别
    ObjectBuilder2.0 的学习
    WaitHandle、AutoResetEvent、ManualResetEvent整理
    使用XML RPC进行远程调用
  • 原文地址:https://www.cnblogs.com/yfr2zaz/p/10480303.html
Copyright © 2011-2022 走看看