POJ 3685 Matrix (二分套二分)
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 8674 | Accepted: 2634 |
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
POJ Founder Monthly Contest – 2008.08.31, windy7926778
题意:定义矩阵中(i,j)位置的值为(i^2+j^2+i*j+100000*(i-j)
求n*n矩阵中第m小的数
题解:首先打表或者推式子发现列是单调递增的,所以可以考虑二分答案,对于每一列二分找到有几个数比他小,然后就可以nlogn作出总共有几个数比他小,然后就搞定了
代码如下:
#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson ch[x][0]
#define rson ch[x][1]
#define hi puts("hi!");
#define int long long
using namespace std; int n,m; int get(int ith,int jth)
{
return ith*ith+100000ll*ith-100000ll*jth+jth*jth+ith*jth;
} int find(int x,int jth)
{
int l=,r=n,mid;
while(l<=r)
{
mid=(l+r)>>;
if(get(mid,jth)<=x)
{
l=mid;
}
else
{
r=mid-;
}
if(r-l<=)
{
if(get(r,jth)<=x) mid=r;
else mid=l;
break;
}
}
return mid;
} int check(int x)
{
int cnt=;
for(int i=;i<=n;i++)
{
cnt+=find(x,i);
}
if(cnt>=m) return ;
else return ;
} signed main()
{
int ttt;
scanf("%lld",&ttt);
while(ttt--)
{
int ans;
scanf("%lld%lld",&n,&m);
int l=-1e12,r=1e12,mid;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid))
{
ans=mid;
r=mid-;
}
else
{
l=mid+;
}
}
printf("%lld\n",ans);
}
}
POJ 3685 Matrix (二分套二分)的更多相关文章
- poj 3685 Matrix 二分套二分 经典题型
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5724 Accepted: 1606 Descriptio ...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- poj3579 二分套二分
和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...
- POJ-3579 Median---二分第k大(二分套二分)
题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...
- POJ 3685 Matrix 二分 函数单调性 难度:2
Memory Limit: 65536K Total Submissions: 4637 Accepted: 1180 Description Given a N × N matrix A, ...
- poj 3685 Matrix 【二分】
<题目链接> 题目大意: 给你一个n*n的矩阵,这个矩阵中的每个点的数值由 i2 + 100000 × i + j2 - 100000 × j + i × j 这个公式计算得到,N( ...
- 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 ...
- 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). 思 ...
- POJ 3233 Matrix Power Series --二分求矩阵等比数列和
题意:求S(k) = A+A^2+...+A^k. 解法:二分即可. if(k为奇) S(k) = S(k-1)+A^k else S(k) = S(k/2)*(I+A^(k/2)) ...
随机推荐
- JS代码压缩格式化在线地址
JS在线格式化: http://tool.oschina.net/codeformat/js JS在线压缩: http://dean.edwards.name/packer/
- mysql master or master copy
双主复制: 在两台server配置my.cnf [root@localhost mysql]# egrep -v "^$|^#" /etc/my.cnf datadir = /my ...
- 读书笔记--Linux Shell脚本攻略
总结的来说,这本书很实践性和实用性强,都是给的具体的例子,直接可以在终端操作实践,比单纯只看不动手务实多了,另外就是,这本书涵盖的内容也比较广,从文本操作到服务器管理到远程ssh等等,都给出来作者挑选 ...
- 535. Encode and Decode TinyURL
▶ 要求给出一种对 URL 网址进行压缩和解压的算法,例如 https://leetcode.com/problems/design-tinyurl ←→ http://tinyurl.com/4e9 ...
- 配置vmWare10(桥接模式),使得虚拟机成为网络中的一台独立的主机
受到启发的原文:https://www.cnblogs.com/liongis/p/3265458.html 第一步:配置虚拟网络 第二步:配置桥接(VMnet0) 第三步:给虚拟机配置桥接模式 第四 ...
- 「小程序JAVA实战」小程序的微信api菜单操作(67)
转自:https://idig8.com/2018/09/25/xiaochengxujavashizhanxiaochengxudeweixinapicaidancaozuo66/ 菜单按钮的介绍, ...
- java JDBC数据库连接操作
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public clas ...
- JS nodeJs 的日期计算
目录[-] date-utils Static Methods 静态方法 Instance Methods 接口方法 date-utils 前端引用 <script type="tex ...
- open File Browser in shell
[maxosx] open /usr/include [ubuntu] 发现三个,如下: xdg-open xxxx.pdf gnome-open . nautilus . 喜欢把它alias一下 . ...
- oracle 基本函数小例子--查询身高段分数段
select * from student order by id drop table student select * from oracletest create table student( ...