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

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

Source

 
题意:给你一个N*N的矩阵,第i行,j列的值为 i2 + 100000 × i + j2 - 100000 × j + i × j,求这些矩阵中的第m小的值。
 
分析:暴力的话超时就不讲了,换到二分:首先分析 i2 + 100000 × i + j2 - 100000 × j + i × j 这个式子,可以发现当j固定时,整个式子的值是递增的,因此可以得出一个结论:在矩阵的同一列中,从上往下数值是递增的,因此可以利用二分枚举每一列的值。
   二分的核心思想:求数组中的第m小的数y,转换成求数组中<x的数量>=m的最小x,则y=x-1(待会证明), 所以该题二分套二分的核心思想就是:首先第第一层外层二分法枚举x,使得数组中<x的数量>=m(即合法),从而无限逼近直到得出最小的x,然后第二层内层层再用二分法求出在数组中<x的数量的个数,这个地方又是一个典型的二分,将求出在数组中<x的个数转换为求出值>=x的最小的下标p,那么<x的最大的下标q就是该下标减1即q=p-1了,然后统计个数就好。
    但是这两层二分都各有一个需要注意的问题:
    1.第一层二分中为什么y==x-1?因为假设y!=x-1,那么存在一个小于x的数x-1,使得
<x-1的数量>=m与x是最小值矛盾,固得证。其实这也是整数性质的体现,因为可以这样看,求一个序列中的第m小的数值,则比该值+1的数值w(二分枚举出的,不管数组中存不存在),在数组中<w的数值是有m个的,那么w-1便是该第m小的数值。
    2.非常容易错!好好体会,见代码
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
long long mid,l,r,n,m;
long long v(long long i,long long j)
{
return i*i+*i+j*j-*j+i*j;
}
long long ok(long long x)
{
long long sum=;
for(long long j=;j<=n;j++)
{
long long l=,r=n+; /*2.这个地方r不能初始化为n,因为假设
n个数全是<x的话,按理讲应该sum+=n的,但是最后r-1之后sum+=(n-1)了,
这是因为v(n,j)>x和v(n,j)=x的效果是不同的,假设v(n,j)>x的话,r仍然为n+1,sum+//=n,而当v(n,j)=x话,r更新为n,sum+=n-1;*/
while(r-l>)
{
long long mid=(l+r)>>;
if(v(mid,j)>=x)
r=mid;
else
l=mid;
}
sum+=(r-);
}
return sum;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%lld %lld",&n,&m);
r=1e12;l=-1e12;
while(r-l>)
{
mid=(l+r)>>;
if(ok(mid)>=m)
r=mid;
else
l=mid;
}
printf("%lld\n",r-);
}
return ;
}

  

poj 3685 Matrix 二分套二分 经典题型的更多相关文章

  1. poj 3579 Median 二分套二分 或 二分加尺取

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5118   Accepted: 1641 Descriptio ...

  2. poj3579 二分套二分

    和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...

  3. POJ-3579 Median---二分第k大(二分套二分)

    题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...

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

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

  5. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

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

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

  7. poj 3685 Matrix 【二分】

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

  8. POJ 3685 二分套二分

    Matrix Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that e ...

  9. POJ 3233 Matrix Power Series 二分+矩阵乘法

    链接:http://poj.org/problem?id=3233 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思 ...

随机推荐

  1. Java 虚拟机的运行模式

    这几天在读周志明老师的<深入理解JVM虚拟机> 讲到了 java的运行模式, 有mixed 模式 interpret模式还有compile模式.效果如下面所示 java -version ...

  2. 【转贴】Windows virtio 驱动

    Windows virtio 驱动 https://blog.51cto.com/dangzhiqiang/1833615 去年去中建总部的时候用过. 发现很多搞openstack的人都不清楚这一块的 ...

  3. 【Python】【基础知识】【内置函数】【dir的使用方法】

    原英文帮助文档: dir([object]) Without arguments, return the list of names in the current local scope. With ...

  4. CSP前模板复习

    Tarjan 求强连通分量 展开查看 #include #include #include using namespace std; const int N = 1e4 + 1e3; int n, m ...

  5. WPF中Brush类型

    画刷Brush使用 画刷类 SolidColorBrush LinearGradientBrush RadialGradientBrush ImageBrush VisualBrush Drawing ...

  6. Git 一般性操作

    git全局设定 git config --global user.name “码云账号” git config --global user.email “码云注册邮箱” git 定位文件夹cd进入到需 ...

  7. java web实现同一账号在不同浏览器不能同时登录

    网上看了很多方法,个人也看了,自己也总结了几个比较常用的: 前提都是用session监听器,对session的创建与销毁进行监听 一.在用户登录时保存该用户的状态有这几种保存方式: 1.保存到内存中( ...

  8. 线程的函数中调用MFC对话框类的变量

    线程的函数中调用MFC对话框类的变量多线程传输文件的对话框 现在想要在对话框上添加一个进度条 为进度条映射变量m_progress这就需要在传输一段文件后就更新m_progress的值使进度条前进 也 ...

  9. oracle重置dba用户密码

    1.进入sqlplus里面: [oracle@master ~]$ sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on Tue ...

  10. centos 7 搭建 LNMP ( Linux+Nginx+MySQL+PHP )

    操作系统 | CentOS Linux release 7.6.1810 (Core) [root@localhost ~# cat /etc/redhat-release CentOS Linux ...