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. 使用axios请求发送数据

    之前一直没有用成功,今天看了一些博客,学会了使用axios插件 1.首先就是下载依赖啦 2.main.js import axios from 'axios'Vue.prototype.$axios ...

  2. Oracle笔记4-pl/sql-分支/循环/游标/异常/存储/调用/触发器

    一.pl/sql(Procedure Language/SQL)编程语言 1.概念 PL/SQL是Oracle数据库对SQL语句的扩展.在普通SQL语句的使用上增加了编程语言的特点,所以PL/SQL把 ...

  3. dedecms无法下载远程jpeg图片 织梦不能提取文章内容中的jpeg图片生成缩略图

    文件:/dede/inc/inc_archives_functions.php 代码: preg_match_all("/(src)=[\"|'| ]{0,}([^>]*\. ...

  4. Debug view 是个好工具

    有时候不用 VS 调试, 在 代码里面加入 Debug.Writeline(" Debug information!!");  这个时候打开 debug view 就可以检测出输出 ...

  5. sharepoint国内网站一览表(转发)

    中国石油化工集团公司http://www.sinopecgroup.com/Pages/index.aspx () 中国南方航空http://group.csair.com/_layouts/grou ...

  6. Help for enable SSL 3.0 and disable TLS 1.0..

    https://support.mozilla.org/en-US/questions/967266 i cant find tab Encryption for enable SSL 3.0 and ...

  7. Mercurial (hg)

    附上两个站点: http://z42.readthedocs.org/zh/latest/devtools/hg.html http://bucunzai.net/hginit/ Mercurial( ...

  8. BZOJ 4502: 串 AC自动机

    4502: 串 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 195  Solved: 95[Submit][Status][Discuss] Des ...

  9. 如何获得C4C里某个code字段对应的描述信息

    通过我这篇文章介绍的方法使用C4C OData服务去取服务订单数据(Sales Order): 如何用代码的方式取出SAP C4C销售订单创建后所有业务伙伴的数据 https://www.jiansh ...

  10. 初识QT中的qDebug()

    首先在头文件中包含 #include<QDebug> 当开发者需要为一个装置.文件.字符串或者控制台,写出调试和跟踪信息时,该类被使用. 在需要使用的地方插入: qDebug(][]); ...