POJ 3685:Matrix 二分
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 5489 | Accepted: 1511 |
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
给了一个N*N的矩阵,每个位置上的数由该位置的下标i,j决定。然后问这个矩阵中第m小的数。
通过这道题好好总结了一下二分,总算是深刻理解了一下。
第一个二分枚举答案,第二个二分,通过公式可知,函数跟j不是单调的关系,但跟i是单调的关系,所以每次枚举j,然后二分i的值。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define maxn 1e12
typedef long long ll; ll m, n;
ll le, ri, mid; ll cal(ll i, ll j)
{
return i*i + 100000 * i + j*j - 100000 * j + i*j;
} ll check(ll x)
{
ll i, le, ri, mid, cnt; ll temp;
cnt = 0;
for (i = 1; i <= n; i++)
{
le = 1;//mid不能取到0,所以这里的le取1
ri = n + 1;//因为mid可能要取到n,所以这里的ri要取到比n大的数 mid = le + (ri - le) / 2;
while (le < ri)
{
temp = cal(mid, i);
if (cal(mid, i) < x)
{
le = mid + 1;
}
else
{
ri = mid ;
}
mid = le + (ri - le) / 2;
}
cnt += (mid - 1);//经过计算,这里始终是多计算了一个1,所以要在这里把它扣掉
}
return cnt;
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int test;
scanf("%d", &test); while (test--)
{
scanf("%lld%lld", &n, &m);
le = -maxn;
ri = maxn; mid = le + (ri - le) / 2;
while (le < ri)
{
if (check(mid) < m)//检查小于 mid 的个数
{
le = mid + 1;
}
else
{
ri = mid;
}
mid = le + (ri - le) / 2;
}
printf("%lld\n", mid-1);//有m个小于mid的数,所以第m大的数就是mid-1
} //system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3685:Matrix 二分的更多相关文章
- POJ 3685 Matrix (二分套二分)
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8674 Accepted: 2634 Descriptio ...
- poj 3685 Matrix 二分套二分 经典题型
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5724 Accepted: 1606 Descriptio ...
- 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(二分搜索之查找第k大的值)
Description Given a N × N matrix A, whose element × i + j2 - × j + i × j, you are to find the M-th s ...
- POJ - 3685 Matrix
二分kth,答案满足的条件为:m ≤ 小于等于x的值数cntx.x和cntx单调不减,随着x增大,条件成立可表示为:0001111. 本地打一个小型的表可以发现列编号j固定时候,目标函数f(i,j)似 ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- POJ3685 Matrix —— 二分
题目链接:http://poj.org/problem?id=3685 Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissio ...
- poj 2318 叉积+二分
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13262 Accepted: 6412 Description ...
- POJ poj 2155 Matrix
题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...
随机推荐
- [PHP]PHP中申明 declare(strict_types=1)的作用
strict_types=1 针对参数类型开启严格模式,进行数据类型检验,默认是弱类型校验哪个文件写了declare,哪个文件中的所有代码就需要检查 declare(strict_types=1); ...
- Vulnhub_DC8 记录
目录 DC8 经验 & 总结 步骤流水 信息搜集 80端口 获取Shell 提权 DC8 经验 & 总结 对页面的功能和对应的url要敏感. 所有的功能都要测试,要雨露均沾. 提示说的 ...
- 条件语句(if语句)的用法
if语句是实现分支结构的常用分支语句之一,另外还有条件运算符.switch语句等. if语句的功能是:根据给定条件,选择程序执行方向. if语句的基本格式 “if语句”又称条件语句,其基本格式为: ...
- springboot中关于datasource的配置
datasource spring.dao.exceptiontranslation.enabled: 是否开启PersistenceExceptionTranslationPostProcessor ...
- #5649,list¶llel
// チケット5649 START // 画面項目.アカウント種別が0.1以外の場合のみ if(!CommonConstants.ACCOUNT_TYPE_SYSTEM_NEXT.equals(for ...
- Linux命令:yum命令
YUM: Yellowdog Update Modifier,rpm的前端程序,可解决软件包相关依赖性,可在多个库之间定位软件包,up2date的替代工具 一.yum命令用法 yum repolist ...
- nginx sendfile 相关知识
https://blog.csdn.net/wm_1991/article/details/51916027
- 分布式事务中间件 TCC-Transaction 源码分析 —— 项目实战
https://blog.csdn.net/lldouble/article/details/79455172
- 使用package.json安装模块
node.js模块的安装可以使用npm安装,如下: $ npm install <Module Name> 每个项目的根目录下面,一般都需要一个package.json文件,定义了这个项目 ...
- redhat 7.6 apache 服务简单安装-01
rpm -qa | grep httpd //该命令查看apache是否安装,下面图片是已安装,未安装不会显示任何内容 yum install httpd -y ...