POJ3685 Matrix —— 二分
题目链接:http://poj.org/problem?id=3685
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 7378 | Accepted: 2187 |
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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; LL n, m; bool test(LL tmp)
{
LL sum = ;
for(LL i = ; i<=n; i++) //枚举i。当i已确定时, 剩下的式子就是关于j的一元二次方程。求解两个根。
{
LL a = , b = i-, c = 1LL*i*i+1LL*i*-tmp;
if(1LL*b*b-4LL*a*c<) continue; //无实数根时, 下一个i
LL x1 = max( 1LL, (LL)ceil((-b-sqrt(1LL*b*b-4LL*a*c))/(*a)) ); //左根向上取整,最小只能为1。
LL x2 = min( 1LL*n, (LL)floor((-b+sqrt(1LL*b*b-4LL*a*c))/(*a)) ); //右根向下取整,最大只能为n
sum += max( 0LL, x2-x1+ ); //区间内有多少个整数
}
return sum>=m;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld", &n, &m);
LL l = -2e10, r = 2e10;
while(l<=r) //二分答案
{
LL mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%lld\n", l);
}
}
错误代码:(求最大的数,使得小于它的数的个数<m。为题目所求的上一个数)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; LL n, m; bool test(LL tmp)
{
LL sum = ;
for(LL i = ; i<=n; i++) //枚举i。当i已确定时, 剩下的式子就是关于j的一元二次方程。求解两个根。
{
LL a = , b = i-, c = 1LL*i*i+1LL*i*-tmp;
if(1LL*b*b-4LL*a*c<=) continue;
LL x1 = max( 1LL, (LL)ceil((-b-sqrt(1LL*b*b-4LL*a*c))/(*a)) ); //左根向上取整,最小只能为1。
LL x2 = min( 1LL*n, (LL)floor((-b+sqrt(1LL*b*b-4LL*a*c))/(*a)) ); //右根向下取整,最大只能为n
sum += max( 0LL, x2-x1+ ); //区间内有多少个整数
}
return sum<m;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld", &n, &m);
LL l = -2e10, r = 2e10;
while(l<=r) //二分答案
{
LL mid = (l+r)>>;
if(test(mid))
l = mid + ;
else
r = mid - ;
}
printf("%lld\n", r);
}
}
POJ3685 Matrix —— 二分的更多相关文章
- POJ3685 Matrix(嵌套二分)
同行元素递减,同列元素递增,采用嵌套二分的方法 #include<cstdio> #include<iostream> #include<cstdlib> #inc ...
- Codeforces 549H. Degenerate Matrix 二分
二分绝对值,推断是否存在对应的矩阵 H. Degenerate Matrix time limit per test 1 second memory limit per test 256 megaby ...
- POJ 3685 Matrix 二分 函数单调性 难度:2
Memory Limit: 65536K Total Submissions: 4637 Accepted: 1180 Description Given a N × N matrix A, ...
- 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 ...
- poj 3685 Matrix 二分套二分 经典题型
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5724 Accepted: 1606 Descriptio ...
- POJ 3685:Matrix 二分
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5489 Accepted: 1511 Descriptio ...
- 74. Search a 2D Matrix(二分查找,剑指offer 1)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- hdu 2119 Matrix(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2119 Matrix Time Limit: 5000/1000 MS (Java/Others) ...
随机推荐
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- Mysql 使用存储过程添加新字段
-- 1, 注意SQL 语句开始处不要空格 -- 2, 在使用 [--] 进行注释时,后面请加空格 USE `test`; -- lastUpdateTime drop procedure if ex ...
- MySQL 游戏排行榜
今天在坛子上看到了,顺便写下来. 有两种方法: 1.效率不高,因为有子查询.但是简洁.而且我对SOCRES表做了INDEX.所以性能上也差不了多少. mysql> show create tab ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- webstorm取消自动保存并标识修改的文件为星星标记
a.取消自动保存是去掉一下两个勾选. b.标记星星要勾选下面的选项. c.最终效果.
- HTTP请求的缓存(Cache)机制
原文地址:http://small.aiweimeng.top/index.php/archives/58.html 先来一张图: ####下面简单的来描述一下HTTP Cache机制: 当资源资源第 ...
- 2019年北航OO第3单元(JML)总结
1 JML语言的理论基础及应用工具链 1.1 JML语言 Java建模语言(JML)是一种行为接口规范语言,可用于指定Java模块的行为.它结合了Eiffel的"契约设计(design by ...
- dropwizard问题记录1:如何进行mvn package打包,如何在项目目录下运行
dropwizard的helloworld入门教程,跟着教程一步步写很容易,但是最后打包时暴露了自己底子太差的缺陷 mvn package操作 之前工作中完全没有接触过这种打包方式,都是直接打war包 ...
- http://www.cnblogs.com/shihaiming/
原文:http://www.bubuko.com/infodetail-917303.html 右击项目,点击Run as,如下图: 即可看到有很多现有的maven命令,点击即可运行,并在控制台可以看 ...
- 标准IO函数以及基本知识点总结
什么是标准IO呢?有哪些特点? 标准IO是标准c库提供的对文件操作的函数接口.他的特点是:1 带缓存,2 大部分都调用系统接口函数实现.(c库就是一种实现好的函数接口,作用是屏蔽下层细节.提供上层接口 ...