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)) ...
随机推荐
- java web 程序---留言板
思路:一个form表单,用户提交留言 一个页面显示留言内容.用到Vector来存取信息并显示 cas.jsp <body> <form action="fei.jsp&qu ...
- QT 控制LED实验
1.实验准备 在PC 机D:盘下创建文件夹qt-led,将光盘qt_led_exp 文件夹下的images 文件夹拷贝到E:盘下qt-led 文件夹qt-led 内 2.新建工程 新建一个Empty ...
- spring 源码如何导入到eclipse
spring 源码如何导入到eclipse spring源码解析——spring源码导入eclipse spring framework源码下载并导入eclipse
- 6.5笔记-DQL高级查询
一.高级查询 Exists Drop table if exists result; 子查询有返回结果: EXISTS子查询结果为TRUE 子查询无返回结果: EXISTS子查询结果为FALSE, 外 ...
- Input设置只读属性
input设置为只读一般用于查看详情: 格式为: (1)单纯的input框 <div class="form-group"> <label for="c ...
- OpenCL 直方图
▶ 计算直方图,由原子计数和规约计算两部分组成 ● 最简单的版本,代码 // kernel.cl #pragma OPENCL EXTENSION cl_khr_local_int32_base_at ...
- mysql 获取一段时间的数据
用 sql 获取一段时间内的数据: SELECT * FROM EDI.edi_history WHERE timestampdiff(day, SYSDATE(), create_time_loc) ...
- Rhythmk 一步一步学 JAVA (22) JAVA 网络编程
1.获取主机信息 @Test public void GetDomainInfo() throws UnknownHostException { String domain = "www.b ...
- 「小程序JAVA实战」小程序视频上传方法的抽象复用(57)
转自:https://idig8.com/2018/09/23/xiaochengxujavashizhanxiaochengxushipinshangchuanfangfadechouxiangfu ...
- .net 架构
.net Webservice 三层架构,BLL(业务逻辑层),DAL(数据访问层)sql语句.MODEL模型层也就是实体层Entity(数据库字段和类的定义的映射). UI层(Web/Form)界面 ...