Matrix

Time Limit: 6000MS Memory Limit: 65536K

Total Submissions: 7879 Accepted: 2374

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. 给你一个n * n的矩阵,矩阵一点的值是i^2 + 100000 × i + j^2 - 100000 × j + i × j,问在整个矩阵中第m大的值是多少。
  2. 刚开始分解组合这个表达式弄了半天发现没啥用,后来才发现这个表达式就是用来观察单调性的,当j不变的时候i是单调递增的,然后这样就可以按照有序性来进行二分了。
  3. 先枚举一个值(O(logn)),然后遍历每一列(O(n)),在每一列中二分查找比枚举的那个值小的有多少个(O(log(n))),这样总的时间复杂度就是O(n(logn)^2);

#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long ll; ll va(ll r, ll c){
ll sum = r*r + 100000*r + c*c - 100000*c + r*c;
return sum;
} bool checke_col(ll ans,ll n,ll m) {
ll num = 0;
for(int j=1;j<=n;j++) {
ll l = 0, r = n+1;
while(r - l > 1) {
ll mid = (l + r) >> 1;
if(va(mid,j) < ans)
l = mid;
else
r = mid;
}
num += l;
}
return num < m;
} int main() {
int t;
scanf("%d",&t);
while(t--) {
ll n,m;
scanf("%lld%lld",&n,&m);
ll l = -100000*n, r = n*n + 100000*n + n*n + n*n;
while(r - l > 1){
ll mid = (l + r) / 2;
if(checke_col(mid,n,m)) l = mid;
else r = mid;
}
printf("%lld\n",l);
}
return 0;
}

POJ:3685-Matrix的更多相关文章

  1. poj:4091:The Closest M Points

    poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...

  2. 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 ...

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

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

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

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

  5. poj 3685 Matrix 【二分】

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

  6. POJ 3685 Matrix 二分 函数单调性 难度:2

      Memory Limit: 65536K Total Submissions: 4637   Accepted: 1180 Description Given a N × N matrix A, ...

  7. POJ - 3685 Matrix

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

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

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

  9. POJ:3020-Antenna Placement(二分图的最小路径覆盖)

    原题传送:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Descri ...

  10. POJ:2695-The Pilots Brothers' refrigerator

    题目链接:http://poj.org/problem?id=2965 The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limi ...

随机推荐

  1. 移动webApp - 1像素实现(点5像素的秘密)

    在移动web项目中,经常会实现以下1像素的边框 移动web设计中,在retina显示屏下网页会由1px会被渲染为2px,那么视觉稿中1px的线条还原成网页需要css定义为0.5px 但是正当我们去用0 ...

  2. CSS3的Animation

    1.animation-name :动画名    2.animation-duration:时间    3.animation-delay:延时    4.animation-iteration-co ...

  3. 跨平台图表控件TeeChart使用教程:导入XML数据

    TeeChart的最新版中包含了一个自动加载XML数据的新组件.这个组件的名字叫做TTeeXMLSource,用户可以在TeeXML.pas unit中找到这个组件. 加载XML图表所需的最小属性是& ...

  4. [一个小问题]Mainfest配置文件的version问题小结

    问题起源自己安装好产品的新build,然后用自己本地编译出来的其中一个DLL去替换到产品中,本来以为可以正常执行,但是却发现这个新DLL无法被Load,无法被使用. 从Event Viewer里找到了 ...

  5. centos7.4 系统安装指导

    centos7 系统安装指导 安装前规划 下载安装文件 安装过程设置 安装后系统基本设置 安装前规划 CentOS 7.x系列只有64位系统,没有32位. 生产服务器建议安装CentOS-7-x86_ ...

  6. Java线程堆栈分析

    不知觉间工作已有一年了,闲下来的时候总会思考下,作为一名Java程序员,不能一直停留在开发业务使用框架上面.老话说得好,机会是留给有准备的人的,因此,开始计划看一些Java底层一点的东西,尝试开始在学 ...

  7. MySQL入门很简单: 2 MySQL数据类型

    2. MySQL数据类型 2.1 整数类型 后面的是默认显示宽度: tinyint(4) smallint(6) mediumint(9) int(11) bigint(20) 2.2 浮点型和定点数 ...

  8. naive bayes classifier in data mining

    https://www-users.cs.umn.edu/~kumar001/dmbook/slides/chap4_naive_bayes.pdf  -- textbook https://www. ...

  9. IOS 4个容易混淆的属性(textAligment contentVerticalAlignment contentHorizontalAlignment contentMode)

    四个容易混淆的属性:1. textAligment : 文字的水平方向的对齐方式1> 取值NSTextAlignmentLeft      = 0,    // 左对齐NSTextAlignme ...

  10. 2018.8.4session的removeAttribute()和invalidate()的区别

    session的removeAttribute()和invalidate()的区别 session.invalidate()是销毁跟用户关联session,例如有的用户强制关闭浏览器,而跟踪用户的信息 ...