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 +  × i + j2 -  × 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( ≤ N ≤ ,) and M( ≤ 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


Sample Output

-

-
- - -

Source

 

 题目大意:题目意思很简单。这个题目乍一看,先打n为比较小例如8的表,会觉得很有规律,大小规律是从右上往左下依次增大,但是这个规律到n为5*10^4就不一定保持了。

           解题思路:有一个规律是看得见的,j不变i增大函数值也在增大。根据这个可以对这n列二分得到<x的值,同样所求的x也是可以二分慢慢靠近最后的结果,我处理得到最后的结果是个数为m+1的最小值,所以最后mid-1即为答案。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define inf 1<<30
#define ll long long
#define N 50006
ll n,m;
ll cal(ll i,ll j){
return i*i+*i+j*j-*j+i*j;
}
bool solve(ll mid){
ll cnt=;
for(ll j=;j<=n;j++){
ll low=;
ll high=n+;
while(low<high){
ll tmp=(low+high)>>;//另外的写法
ll ans=cal(tmp,j);
if(ans>=mid){
high=tmp;
}
else{
low=tmp+;
}
}
cnt+=low-;//另外的写法
}
if(cnt>=m) return true;
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){ scanf("%I64d%I64d",&n,&m);
ll low=-1e12;
ll high=1e12; while(low<high){
ll mid=(low+high)>>;//另外的写法
if(solve(mid)){
high=mid;
}
else{
low=mid+;
}
}
printf("%I64d\n",low-);//另外的写法 }
return ;
}

另外的写法,试比较

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define inf 1<<30
#define ll long long
#define N 50006
ll n,m;
ll cal(ll i,ll j){
return i*i+*i+j*j-*j+i*j;
}
bool solve(ll mid){
ll cnt=;
for(ll j=;j<=n;j++){
ll low=;
ll high=n+;
ll tmp=(low+high)>>;
while(low<high){
ll ans=cal(tmp,j);
if(ans>=mid){
high=tmp;
}
else{
low=tmp+;
}
tmp=(low+high)>>;
}
cnt+=tmp-;
}
if(cnt>=m) return true;
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){ scanf("%I64d%I64d",&n,&m);
ll low=-1e12;
ll high=1e12;
ll mid=(low+high)>>;
while(low<high){
if(solve(mid)){
high=mid;
}
else{
low=mid+;
}
mid=(low+high)>>;
}
printf("%I64d\n",mid-); }
return ;
}

poj 3685 Matrix(二分搜索之查找第k大的值)的更多相关文章

  1. poj 3579 Median (二分搜索之查找第k大的值)

    Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...

  2. 查找第K大的值

    这种题一般是给定N个数,然后N个数之间通过某种计算得到了新的数列,求这新的数列的第K大的值 POJ3579 题意: 用$N$个数的序列$x[i]$,生成一个新序列$b$. 新的序列定义为:对于任意的$ ...

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

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

  4. POJ_3685_Matrix_(二分,查找第k大的值)

    描述 http://poj.org/problem?id=3685 一个n*n的矩阵,(i,j)的值为i*i+100000*i+j*j-100000*j+i*j,求第m小的值. Matrix Time ...

  5. poj 2579 中位数问题 查找第K大的值

    题意:对列数X计算∣Xi – Xj∣组成新数列的中位数. 思路:双重二分搜索 对x排序 如果某数大于 mid+xi 说明在mid后面,这些数的个数小于 n/2 的话说明这个中位数 mid 太大 反之太 ...

  6. POJ_3579_Median_(二分,查找第k大的值)

    描述 http://poj.org/problem?id=3579 给你一串数,共C(n,2)个差值(绝对值),求差值从大到小排序的中值,偶数向下取. Median Time Limit: 1000M ...

  7. 基于快速排序思想partition查找第K大的数或者第K小的数。

    快速排序 下面是之前实现过的快速排序的代码. function quickSort(a,left,right){ if(left==right)return; let key=partition(a, ...

  8. 如何用快排思想在O(n)内查找第K大元素--极客时间王争《数据结构和算法之美》

    前言 半年前在极客时间订阅了王争的<数据结构和算法之美>,现在决定认真去看看.看到如何用快排思想在O(n)内查找第K大元素这一章节时发现王争对归并和快排的理解非常透彻,讲得也非常好,所以想 ...

  9. 利用划分树求解整数区间内第K大的值

    如何快速求出(在log2n的时间复杂度内)整数区间[x,y]中第k大的值(x<=k<=y)? 其实我刚开始想的是用快排来查找,但是其实这样是不行的,因为会破坏原序列,就算另外一个数组来存储 ...

随机推荐

  1. hdu 5402 Travelling Salesman Problem(大模拟)

    Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...

  2. MySQL精粹

    关于Mysql整理的需要记忆和熟练掌握的内容 1.查询数据表的信息(比如有多少行数据): show table status like 'tab_User' -- 数据表中的数量   2. 使用 ex ...

  3. tomcat 7配置数据库连接池,使用SQL Server2005实现

    昨 天看了一些网上的tomcat数据库连接池配置的东西,但是一直没配好,主要原因是网上的文章几乎没有针对tomcat 7进行配置的,而且针对SQL SERVER的也不多,今天上午看了官方的文档,花了一 ...

  4. Gunplot 命令大全

    在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. plot命令 gnuplot> plot sin(x) with line linetype 3 linew ...

  5. Unity 对象池的使用

    在游戏开发过程中,我们经常会遇到游戏发布后,测试时玩着玩着明显的感觉到有卡顿现象.出现这种现象的有两个原因:一是游戏优化的不够好或者游戏逻辑本身设计的就有问题,二是手机硬件不行.好吧,对于作为程序员的 ...

  6. jQuery.extend 和 jQuery.fn.extend

    1.jQuery.extend 我们先把jQuery看成了一个类,这样好理解一些.jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑 ...

  7. python-文件操作(1)

    本文内容涉及python打开/创建文件对象,文件的读写.文件指针位置的移动.获取命令行参数. 1. open() open函数以指定模式返回一个file对象,如: file_object = open ...

  8. 除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。

    在 SELECT 后加 TOP 100 PERCENT .

  9. HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2)

      public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int  ...

  10. poj3614 贪心

    Sunscreen Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6410   Accepted: 2239 Descrip ...