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 ...
随机推荐
- Android Studio 配置Gradle
一, 问题:①换个新电脑安装完Android Sutdio第一次打开一个工程巨慢怎么办?② 手动配置Gradle Home为什么总是无效?③ 明明已经下载了Gradle,配置了gradle home, ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- JS闭包(1)
1.首先看一段代码: var a = 1; function fn1(){ var b = 2; function fn2(){ console.log(a); console.log(b); } } ...
- 理解错误的 Arrays.asList()
简介 Arrays.asList() 作用是将一个数组转换为一个List 集合. String[] myArray = { "Apple", "Banana", ...
- 在webView中的返回键
在写webView中我们按一下返回键,退到上一个我们浏览的网页,到第一个页面时,按两下退出程序,且按一下时提示你在按一下退出程序 只要加上这个方法即可 public void onBackPresse ...
- ➡️➡️➡️leetcode 需要每天打卡,养成习惯
目录 待完成的 完成的 0204 0203 以前 java 的 ! 的操作 不像 c 那样自由,!不要使用在int 变量上 c ^ 是异或操作 体会:c中,malloc 后的新建的数组,默认不是0(j ...
- 用C/C++创建windows服务程序
转载:https://blog.csdn.net/chenyujing1234/article/details/8023816 一.演示过程下方代码演示了如何使用vs(C/C++)创建windows服 ...
- Controller层注解
/** * Copyright © 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite< ...
- bfc作用
作用 1.清浮动 2.不被浮动元素覆盖 3.阻止父子margin传递 触发条件: 1.float不为none 2.position不为static或relative 3.display:inline- ...
- redis介绍及搭建
redis介绍 Redis最适合所有数据in-momory的场景,虽然Redis也提供持久化功能,但实际更多的是一个disk-backed(以写入磁盘的方式进行同步,实现持久化)的功能,跟传统意义上的 ...