POJ:3685-Matrix
Matrix
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 7879 Accepted: 2374
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^2 + 100000 × i + j^2 - 100000 × j + i × j,问在整个矩阵中第m大的值是多少。
- 刚开始分解组合这个表达式弄了半天发现没啥用,后来才发现这个表达式就是用来观察单调性的,当j不变的时候i是单调递增的,然后这样就可以按照有序性来进行二分了。
- 先枚举一个值(O(logn)),然后遍历每一列(O(n)),在每一列中二分查找比枚举的那个值小的有多少个(O(log(n))),这样总的时间复杂度就是O(n(logn)^2);
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long ll;
ll va(ll r, ll c){
ll sum = r*r + 100000*r + c*c - 100000*c + r*c;
return sum;
}
bool checke_col(ll ans,ll n,ll m) {
ll num = 0;
for(int j=1;j<=n;j++) {
ll l = 0, r = n+1;
while(r - l > 1) {
ll mid = (l + r) >> 1;
if(va(mid,j) < ans)
l = mid;
else
r = mid;
}
num += l;
}
return num < m;
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
ll n,m;
scanf("%lld%lld",&n,&m);
ll l = -100000*n, r = n*n + 100000*n + n*n + n*n;
while(r - l > 1){
ll mid = (l + r) / 2;
if(checke_col(mid,n,m)) l = mid;
else r = mid;
}
printf("%lld\n",l);
}
return 0;
}
POJ:3685-Matrix的更多相关文章
- poj:4091:The Closest M Points
poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...
- 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 (二分套二分)
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 【二分】
<题目链接> 题目大意: 给你一个n*n的矩阵,这个矩阵中的每个点的数值由 i2 + 100000 × i + j2 - 100000 × j + i × j 这个公式计算得到,N( ...
- POJ 3685 Matrix 二分 函数单调性 难度:2
Memory Limit: 65536K Total Submissions: 4637 Accepted: 1180 Description Given a N × N matrix A, ...
- 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的 ...
- POJ:3020-Antenna Placement(二分图的最小路径覆盖)
原题传送:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Descri ...
- POJ:2695-The Pilots Brothers' refrigerator
题目链接:http://poj.org/problem?id=2965 The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limi ...
随机推荐
- XHML教会我的一些东西-4
今天把“河畔林语”的项目看完了.我自己也试着做了一下,这算是自己写的第一个项目吧.虽然全部是模仿,而且经常一边看写好的一边自己写,还是自己不够成熟呀. 不知道为什么,我用我的电脑进http://www ...
- 登录mysql数据库出现 : ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO) ER或者忘记密码
1. 在安装mysql的文件目录中找到配置文件my.ini ,然后右击用记事本打开 2. 打开后,搜索mysqld关键字 找到后,在mysqld下面添加skip-grant-tabl ...
- 让DIV的滚动条自动滚动到最底部
一个在线聊天窗口,在做最后的修饰时,需要对获得的信息即时滚动以保证用户总能看到最新消息. 我得出的结论是:在选中div时,必须用原生js,jQuery不起作用 <!DOCTYPE> < ...
- GCC & Maker
All we did must depend on compiler, and then What we did can run on machine. What does compiler do b ...
- SQL2008R2 清空日志
SQLSERVER2008之前版本执行的SQL语句: DUMP TRAN 数据库名 WITH NO_LOG SQLSERVER2008-R2版本执行的SQL语句: ALTER DATABASE 数据库 ...
- selenium并行的使用
Selenium Grid Parallel Test(C#) Author: Mars (hnynes)Email: hnynes@gmail.comMSN: hnynes@gmail.co ...
- centos7 mod_gearman 3.0.1 打包rpm
wget https://github.com/sni/mod_gearman/archive/v3.0.1.tar.gz -O /root/rpmbuild/SOURCES/mod_gearman- ...
- 有趣的回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- May 7th 2017 Week 19th Sunday
A chain is no stronger than its weakest link. 链条的坚固程度取决于它最薄弱的环节. The same as the well-known buckets ...
- ARM实验5 —— 按键中断实验
key_int按键中断实验 实验内容: 通过开发板上的按键中断控制led灯进行跑马灯并打印信息. 通过简单事例说明猎户座4412处理器的GIC中断处理的应用,设置key2按键连接的引脚为中断模式,当识 ...