Memory Limit: 65536K
Total Submissions: 4637   Accepted: 1180

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 思路:
1 可以看出当j确定的时候i是单调递增的,那么就可以二分得到某个值当j确定时有多少i的值大于它,设为big
2 二分答案当big+ind>n*n(也即全部个数)时,这个值就太小了,增加下界,反之减少上界即可
错误原因 1:全部个数忘了n*n,打成n了 2:上下界错误,看成了1e4 3:读取爆longlong
#include <cstdio>
using namespace std; long long ind,n;
long long equ(long long i,long long j){
return i*i+j*j+i*j+(i-j)*100000;
}
long long judge(long long mid){
long long big=0;
for(int j=1;j<=n;j++){
int l=0;
int r=n+1;
long long cp;
while(r-l>1){
int m=(r+l)>>1;
cp=equ(m,j);
if(cp==mid){
l=m;
break;
}
else if(cp<mid){
l=m;
}
else{
r=m;
}
}
// printf("binary %I64d col %d %I64d\n",mid,j,l);
big+=n-l;
}
return big;
}
void printe(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
printf("%6I64d ",equ(i,j));
}
puts("");
}
}
int main(){
int T;
// freopen("C:\\Users\\Administrator\\Desktop\\input.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%I64d%I64d",&n,&ind);
long long l=-(3*n*n+n*300000),r=-l;
//printe();
while(r-l>1){
long long mid=l+r>>1;
long long big=judge(mid);
if(big>n*n-ind){
l=mid;
}
else {
r=mid;
}
}
printf("%I64d\n",r);
}
return 0;
}

  

POJ 3685 Matrix 二分 函数单调性 难度:2的更多相关文章

  1. POJ 3685 Matrix (二分套二分)

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8674   Accepted: 2634 Descriptio ...

  2. poj 3685 Matrix 二分套二分 经典题型

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 5724   Accepted: 1606 Descriptio ...

  3. poj 3685 Matrix 【二分】

    <题目链接> 题目大意: 给你一个n*n的矩阵,这个矩阵中的每个点的数值由   i2 + 100000 × i + j2 - 100000 × j + i × j  这个公式计算得到,N( ...

  4. poj 3685 Matrix(二分搜索之查找第k大的值)

    Description Given a N × N matrix A, whose element × i + j2 - × j + i × j, you are to find the M-th s ...

  5. POJ - 3685 Matrix

    二分kth,答案满足的条件为:m ≤ 小于等于x的值数cntx.x和cntx单调不减,随着x增大,条件成立可表示为:0001111. 本地打一个小型的表可以发现列编号j固定时候,目标函数f(i,j)似 ...

  6. POJ 3579 3685(二分-查找第k大的值)

    POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...

  7. POJ3685 Matrix —— 二分

    题目链接:http://poj.org/problem?id=3685 Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissio ...

  8. poj 2318 叉积+二分

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13262   Accepted: 6412 Description ...

  9. POJ poj 2155 Matrix

    题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...

随机推荐

  1. [noip模拟题]合理种植

    [问题描述] 大COS在氯铯石料场干了半年,受尽了劳苦,终于决定辞职.他来到表弟小cos的寒树中学,找到方克顺校长,希望寻个活干. 于是他如愿以偿接到了一个任务…… 美丽寒树中学种有许多寒树.方克顺希 ...

  2. Spring Boot条件注解

    一.为什么SpringBoot产生于Spring4? Spring4中增加了@Condition annotation, 使用该Annotation之后,在做依赖注入的时候,会检测是否满足某个条件来决 ...

  3. ubuntu 把软件源修改为国内源和更新

    1. 备份原始文件 sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup 2. 修改文件并添加国内源 vi /etc/apt/sourc ...

  4. Could not find a package configuration file provided by "ecl_build",.................couldn't find required component 'ecl_build'

    sudo apt-get install ros-kinetic-ecl-build

  5. .net Parallel并行使用

    因项目响应过慢,代码优化空间不大,在暂时无法调整系统架构的情况下,只有使用.NET中的TPL解决一些模块耗时过多的问题.但在使用过程中也碰到了一些问题,现在把它写下来,用于备忘. 1. Paralle ...

  6. c语言的左移和右移

    转自:https://www.cnblogs.com/myblesh/articles/2431806.html 先说左移,左移就是把一个数的所有位都向左移动若干位,在C中用<<运算符.例 ...

  7. RetinaNet论文理解

    引言 介绍 目前精度高的检测器都是基于two-stage,proposal-driven机制,第一阶段生成稀疏的候选对象位置集,第二阶段使用CNN进一步将每个候选位置分为前景或者背景以及确定其类别: ...

  8. Codeforces C - String Reconstruction

    C - String Reconstruction 方法一:把确定的点的父亲节点设为下一个点,这样访问过的点的根节点都是没访问过的点. 代码: #include<bits/stdc++.h> ...

  9. 第一次学习 CG( c for graphic) 遇到的一大推坑

    1.CG开发环境的配置: 具体的工具包下载及整体的配置过程可以参考:https://blog.csdn.net/seamanj/article/details/8300936. 上面网址的内容是对VS ...

  10. 基于netty框架的socket长连接负载均衡解决方案

    socket通讯的单机瓶颈 物联网的项目socket使用方式有两种: 短连接的socket请求 维持socket长连接的请求 对于socket短链接来说就好比是http请求,请求服务器,服务器返回数据 ...