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 ...
随机推荐
- 除了用作缓存数据,Redis还可以做这些
Redis应该说是目前最受欢迎的NoSQL数据库之一了.Redis通常被作为缓存组件,用作缓存数据.不过,除了可以缓存数据,其实Redis可以做的事还有很多.下面列举几例,供大家参考. 1.最新列表 ...
- linux查看某个目录下有哪些文件的命令
分别是ll和ls命令 ll /usr/local/lib ls /usr/local/lib
- ubuntu 16.04 LTS 安装 teamviewer 13
背景介绍 由于需要做现场的远程支持,经协商后在现场的服务器上安装TeamViewer 以便后续操作. 本来以为很简单的一件事,谁知却稍微费了一番周折 :( 记录下来,希望提醒自己的同时也希望能够帮到 ...
- SQL中select与set的区别
转自 : http://www.cnblogs.com/4mylife/archive/2012/10/25/2738466.html 下表列出 SET 与 SELECT 的区别 SELECT S ...
- ffmpeg 播放音频
播放音频,设置好SDL_AudioSpec播放参数,然后由SDL回调函数进行解码和数据的拷贝,解码播放音频无需设置延迟,因为声卡播放音频是阻塞的 int audio_decode_frame(AVCo ...
- linux-centos6/7初始配置
关闭防火墙 chkconfig iptables off centos7下的命令为 systemctl stop firewalld.service #停止Firewall systemctl dis ...
- java中key-value数据有重复KEY如何存储
http://www.iteye.com/problems/87219 Map<Key, List<Value>>, 这个好 师兄厉害,给介绍了个神器:guava
- CSS3基础
内容: 1.圆角 border-radius 2.阴影 text-shadow.box-shadow 3.渐变 linear.radial 4.rgba rgb+alpha opacity 5.tra ...
- 内建函数(builtins)和functools
内建函数 Build-in Function,启动python解释器,输入dir(__builtins__), 可以看到很多python解释器启动后默认加载的属性和函数,这些函数称之为内建函数, 这些 ...
- 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv
使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...