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)) ...
随机推荐
- 【python】Beautiful Soup的使用
1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...
- .NET后台访问其他站点代码整理
HttpWebRequest request = WebRequest.Create(@"http://localhost:8080/mail/SendMail") as Http ...
- delete删除属性
/* 删除实例属性 */ function MyObject() { this.name = "我是实例的name"; } var obj = new MyObject(); al ...
- SqlServer快速获得表总记录数(大数据量)
--第1种 执行全表扫描才能获得行数 SELECT count(*) FROM BUS_tb_UserGradePrice --第2种 执行扫描全表id不为空的,获得行数 select count(u ...
- 引用变量类型的加载顺序(类名+引用名=new +类名();)
程序如下: 运行结果如下: 以上结果说明:同一个引用名称(可以把它当做变量的一种类型)可能指代不同的对象,依据同一个引用是否处于同一个初始化的层次,决定是否在完成: static Cup c1=new ...
- apache配置https和http的时候You don't have permission to access / on this server.
You don't have permission to access / on this server. 是由于没有设置访问目录 今天配置httpd-ssl.conf的时候 发现这个问题 由于默认 ...
- 「小程序JAVA实战」小程序的个人信息作品,收藏,关注(66)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudegerenxinxizuopinshoucangguanzhu65 ...
- python greenlet 背景介绍与实现机制
最近开始研究Python的并行开发技术,包括多线程,多进程,协程等.逐步整理了网上的一些资料,今天整理一下greenlet相关的资料. 并发处理的技术背景 并行化处理目前很受重视, 因为在很多时候,并 ...
- Discuz 任务、道具、任务和验证类插件制作
一.广告类脚本位置:source/class/adv/adv_name.php语言包位置:source/language/adv/lang_name.php <?php class adv_na ...
- FoxPro 打开文件及使用SQL查询
set exclusive off OPEN DATABASE t:\tpswin\comp1\time.dbc SHARED select empl_no,date,in, bout, bin, o ...