poj3685(嵌套二分)
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 4658 | Accepted: 1189 |
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
#include <iostream> using namespace std;
long long N, M;
const long long INF = 1LL << 50;
long long mtr ( long long i, long long j )
{
return i * i + 100000 * i + j * j - 100000 * j + i * j;
} bool b_s ( long long X )
{
long long res = 0; for ( int i = 1; i <= N; i++ )
{
int cnt = N ;
int l = 1, r = N; while ( l <= r )
{
int mid = ( r + l ) >> 1; if ( mtr ( mid, i ) >= X )
{
r = mid - 1;
cnt = mid - 1;
}
else
{
l = mid + 1;
}
} res += cnt ;
}
return res >= M;
}
int main()
{
int n;
cin >> n ;
while ( n-- )
{
cin >> N >> M;
long long l = -INF, r = INF;
long long ans=-1; while ( l <= r )
{
long long mid = ( r + l )>>1;
if ( b_s ( mid ) )
{
r = mid - 1;
ans = mid - 1;
}
else
{
l = mid + 1;
}
} cout <<ans << endl;
} return 0;
}
poj3685(嵌套二分)的更多相关文章
- POJ3685 Matrix(嵌套二分)
同行元素递减,同列元素递增,采用嵌套二分的方法 #include<cstdio> #include<iostream> #include<cstdlib> #inc ...
- POJ3685 Matrix —— 二分
题目链接:http://poj.org/problem?id=3685 Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissio ...
- CCPC-Wannafly 秦皇岛 day5训练总结
训练总结 今天开局先上去看到了J,粗略(>_<)的看了下题意,然后就觉得应该是个dp 就开始写,思路是个三维dp,开的10000*10*10 觉得就不会爆,时间上也没问题,后面陆续wa了 ...
- WHY吃糖果 QDUOJ 二分嵌套
WHY吃糖果 QDUOJ 二分嵌套 原题链接 解题思路参考链接 题意 给出一个\(n*n\)的矩阵,每个格子的权值为\(i*i+j*j+i*j+100000*(i-j)\),求该矩阵中第m小的权值为多 ...
- poj3685 二分套二分
F - 二分二分 Crawling in process... Crawling failed Time Limit:6000MS Memory Limit:65536KB 64bit ...
- Matrix [POJ3685] [二分套二分]
Description 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. Input 第一行输 ...
- UVALive 6656 Watching the Kangaroo --二分
题意:给你一些区间,再查询一些点,问这些点与所有区间形成的最小距离的最大值.最小距离定义为:如果点在区间内,那么最小距离为0,否则为min(pos-L[i],R[i]-pos). 解法:当然要排个序, ...
- matlab实现的嵌套乘法、高精度、二分法
嵌套乘法的计算: \[ P(x) = 1 - x + x^2 - x^3 + ...+ x ^ {98} - x^{99} \] function y = nest( d, c, x, b ) if ...
- 从fastjson多层泛型嵌套解析,看jdk泛型推断
给你一组json数据结构,你把它解析出来到项目中,你会怎么做? // data1 sample { "code" : "1", "msg" ...
随机推荐
- Hadoop MapReduce编程 API入门系列之MapReduce多种输出格式分析(十九)
不多说,直接上代码. 假如这里有一份邮箱数据文件,我们期望统计邮箱出现次数并按照邮箱的类别,将这些邮箱分别输出到不同文件路径下. 代码版本1 package zhouls.bigdata.myMapR ...
- Solr快速入门(一)
概述 本文档介绍了如何获取和运行Solr,将各种数据源收集到多个集合中,以及了解Solr管理和搜索界面. 首先解压缩Solr版本并将工作目录更改为安装Solr的子目录.请注意,基本目录名称可能随Sol ...
- Choerodon 的微服务之路(二):Choerodon 的微服务网关
链接地址:https://my.oschina.net/choerodon/blog/2254030
- markdown 计算器
计算器 分四种运算(加减乘除).括号.去除最后括号.验证等式是否计算完成 bracket = re.compile(r'\([^()]+\)') # 找括号 multiplys = re.compil ...
- 找不到dll原因
1.没有生成成功: A.代码有错误 B.引用dll有问题(dll生成失败|dll版本不对) C.源文件损坏 2.dll生成路径不对 3.对dll的引用丢失
- 非阻塞方式connect编程
参考博客: ①setsockopt()函数使用详解:http://blog.csdn.net/tody_guo/article/details/5972588 ②setsockopt :SO_LING ...
- [转]C++内存管理
[导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...
- ML二:NNSearch数据结构--二叉树
wiki百科:http://zh.wikipedia.org/wiki/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AD%A6%E4%B9%A0 opencv学习笔记--二杈决策树: ...
- spring中log4j的使用---转载
原文链接:http://www.codeceo.com/article/log4j-usage.html 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供 ...
- 【技术累积】【点】【java】【5】Random和shuffle()
闲聊 妈耶,又这么久没写了..不过最近写其他文章有点多啊... 今天用到Random这个类,竟然还要去查了下... 基本概念 Random类,背后是伪随机数(数学上的东西): 不是很理解,但是基本上而 ...