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. poj 2481 Cows(树状数组)题解

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  2. spring boot 使用@ConfigurationProperties

    有时候有这样子的情景,我们想把配置文件的信息,读取并自动封装成实体类,这样子,我们在代码里面使用就轻松方便多了,这时候,我们就可以使用@ConfigurationProperties,它可以把同类的配 ...

  3. 【第三十二章】 elk(3)- broker架构 + 引入logback

    实际中最好用的日志框架是logback,我们现在会直接使用logback通过tcp协议向logstash-shipper输入日志数据.在上一节的基础上修改!!! 一.代码 1.pom.xml 1 &l ...

  4. BZOJ5281: [Usaco2018 Open]Talent Show 01分数规划+01背包

    Description FarmerJohn要带着他的N头奶牛,方便起见编号为1…N,到农业展览会上去,参加每年的达牛秀!他的第i头奶牛重 量为wi,才艺水平为ti,两者都是整数.在到达时,Farme ...

  5. 机器学习-数据可视化神器matplotlib学习之路(五)

    这次准备做一下pandas在画图中的应用,要做数据分析的话这个更为实用,本次要用到的数据是pthon机器学习库sklearn中一组叫iris花的数据,里面组要有4个特征,分别是萼片长度.萼片宽度.花瓣 ...

  6. Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  7. poj 1144 Network 无向图求割点

    Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. Th ...

  8. pyqt 实现的俄罗斯方块

    from PyQt5.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication from PyQt5.QtCore impo ...

  9. .net Parallel并行使用

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

  10. 使用Maven + Jetty时,如何不锁定js css 静态资源

    Jetty会使用内存映射文件来缓存静态文件,包括js,css文件. 在Windows下,使用内存映射文件会导致文件被锁定,所以当Jetty启动的时候无法在编辑器对js或者css文件进行编辑. 解决办法 ...