Matrix(二分套二分)
Matrix
http://poj.org/problem?id=3685
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 8943 | Accepted: 2738 |
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 <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std; ll n,k;
ll cal(ll i,ll j){
return i*i+*i+j*j-*j+i*j;
} bool Check(ll num){
ll sum=;
for(ll i=;i<=n;i++){
ll L=,R=n,mid;
while(L<=R){
mid=L+R>>;
if(cal(mid,i)<=num){
L=mid+;
}
else{
R=mid-;
}
}
sum+=R;
}
return sum>=k;
} int main(){ int T;
cin>>T;
while(T--){
cin>>n>>k;
ll L=-1e18,R=1e18,mid;
while(L<=R){
mid=L+R>>;
if(Check(mid)){
R=mid-;
}
else{
L=mid+;
}
}
cout<<L<<endl;
} }
Matrix(二分套二分)的更多相关文章
- poj 3685 Matrix 二分套二分 经典题型
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5724 Accepted: 1606 Descriptio ...
- poj3579 二分套二分
和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...
- POJ-3579 Median---二分第k大(二分套二分)
题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- POJ 3685 Matrix (二分套二分)
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8674 Accepted: 2634 Descriptio ...
- Matrix (二分套二分
Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i ...
- Matrix [POJ3685] [二分套二分]
Description 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. Input 第一行输 ...
- poj3685 二分套二分
F - 二分二分 Crawling in process... Crawling failed Time Limit:6000MS Memory Limit:65536KB 64bit ...
- 二分套二分 hrbeu.acm.1211Kth Largest
Kth Largest TimeLimit: 1 Second MemoryLimit: 32 Megabyte Description There are two sequences A and ...
随机推荐
- 转载-MyBatis学习总结
MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合 孤傲苍狼 2015-02-07 00:09 阅读:89825 评论:54 MyBatis学习总结(七)——Myba ...
- docker 基础操作
1. 安装docker 系统centos 7.2 yum -y install docker-io service docker start 安装完毕后执行 docker version 或者dock ...
- 通过Vim+少量插件配置一个高效简洁的IDE
最近本人在看<TCP/IP Illustrated Volume2:The Implementation>这本书,自然要下载4.4BSD-Lite的源代码配合书本一起研读.以前学习Vim的 ...
- [ffmpeg_3.3.2]demuxing_decoding.c
分析ffmpeg3.3.2的example: 由于ffmpeg文档比较少,而且API变化表较大,所以个人首先从ffmpeg自带的demo开始分析,分析(demuxing_decoding.c) 1:首 ...
- 字符串,hash
字符串1.有序的字符的集合,不可变2.s.swapcase() 大变小,小变大3.s.capitalize() 第一个大写4.s.casefold() 返回将字符串中所有大写字符转换为小写后生成的字符 ...
- js数组的初始化
方法一: var myarray = new Array(66,80,90,77,59); 方法二: var myarray = [66,80,90,77,59]; 方法三: var myarray= ...
- face,Pool
.a 在使用Python进行系统管理时,特别是同时操作多个文件目录或者远程控制多台主机,并行操作可以节约大量的时间.如果操作的对象数目不大时,还可以直接使用Process类动态的生成多个进程 ,十几个 ...
- uva-10112-计算几何
题意:给你一些点,求这些点组成的三角形面积最大,而且三角形内不能包含其他点 #include <iostream> #include <math.h> #include< ...
- python you-get 下载视频
python使用you-get模块下载视频 pip install you-get # 安装先 怎么用 进入命令行: you-get url 暂停下载:ctrl + c ,继续下载重复 y ...
- centos启用root账号登陆telnet
1,shutdown iptables或是放行23端口 2,shutdown selinux或是设置放行; 3,yum -y install telnet telnet-server 4,vim /e ...