F - 二分二分

Crawling in process... Crawling failed Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

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 ≤ MN × 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
题目大意:给你一个n*n矩阵,每一个位置都有一个值,这个值由该点的该点的行列标决定,问你第m小的元素是多少。
思路分析:比赛时都贴上二分标签了,最后还没敲出来,首次我们要二分答案,然后check,check函数里面按列
进行枚举,因为在j确定的情况下函数关于i递增,然后直接二分查找刚好小于等于mid的元素在每一列的位置,累加
return,判断返回的数与m的关系,继续二分
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int inf=1e9;
typedef long long ll;
const int C=;
ll f(ll x,ll y)
{
return (x*x+C*x+y*y-C*y+x*y);
}
ll n,m;
ll checknum(ll num)
{
ll cnt=;
for(int i=;i<=n;i++)//枚举列,因为在列数确定的情况下表达式关于i是递增的
{
ll sl=,sr=n;
int ant=;
while(sl<=sr)
{
ll smid=(sl+sr)>>;
if(f(smid,i)<=num)
{
ant=smid;
sl=smid+;
}
else sr=smid-;
}
cnt+=ant;
}
return cnt;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
ll l=-*n,r=n*n+*n+n*n+n*n;
ll ans;
while(l<=r)
{
ll mid=(l+r)>>;
//cout<<mid<<endl;
if(checknum(mid)>=m)
{
ans=mid;
//cout<<ans<<endl;
r=mid-;
}
else l=mid+;
// cout<<ans<<endl;
}
printf("%lld\n",ans);
}
}

poj3685 二分套二分的更多相关文章

  1. poj3579 二分套二分

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

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

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

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

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

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

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

  5. Matrix [POJ3685] [二分套二分]

    Description 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. Input 第一行输 ...

  6. 二分套二分 hrbeu.acm.1211Kth Largest

    Kth Largest TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Description There are two sequences A and ...

  7. 51nod 1105(第K大数 二分套二分)

    题目链接:http://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=620811 参考自:https://blog.csdn.net/f_ ...

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

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

  9. Gym 101064 D Black Hills golden jewels 【二分套二分/给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合】

    D. Black Hills golden jewels time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. c++实现dll注入其它进程

    DLL注入技术才具有强大的功能和使用性,同时简单易用,因为DLL中可以实现复杂的功能和很多的技术. 技术要点: 1.宿主进程调用LoadLibrary,就可以完成DLL的远程注入.可以通过Create ...

  2. stdarg.h详解

    读Linux内核中的vsprintf函数的时候遇到了C语言的可变参数调用,查了挺多资料还是这篇比较详细,而且自己验证了下,确实如此 (一)写一个简单的可变参数的C函数  下面我们来探讨如何写一个简单的 ...

  3. 编译的时候 c:\windows\assembly\ 卸载不掉

    easyhook 开始还可以调试,几次过后 其自己去找c:\windows\assembly\ 下的包,编译多少次都不行. c:\windows\assembly\  卸载不掉 cmd cd \win ...

  4. Git使用说明--常用命令

    These are common Git commands used in various situations: start a working area (see also: git help t ...

  5. iPhone开发:Objective C 代码规范-iOS总结版

    一,关于空行 A:.h中的空行 1,文件说明与头文件包涵(#import)之间空1行 2,头文件包涵(#import)之间,如果需要分类区别,各类别之间空1行 3,头文件包涵(#import)与@cl ...

  6. android使用apktool反编译出现Input file (d:\t) was not found or was not readable

    Input file (d:\t) was not found or was not readable 出现这个错误是因为apktool压缩包下载错误,我是下成首页的那个压缩包了 正确下载地址:htt ...

  7. 转:Red Hat JBoss团队发布WildFly 8,全面支持Java EE 7并包含全新的嵌入式Web服务器

    原文来自于:http://www.infoq.com/cn/news/2014/02/wildfly8-launch Red Hat的JBoss部门今天宣布WildFly 8正式发布.其前身是JBos ...

  8. delphi线程的创建、挂起、激活与终止(用绘图做实验,简单又好用)

    unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  9. ViewPager的使用方法和实现过程

    布局文件里添加viewPager布局 <android.support.v4.view.ViewPager android:id="@+id/search_viewpager" ...

  10. POJ-2386(深广搜基础)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25322   Accepted: 12759 D ...