POJ 3685
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 4428 | Accepted: 1102 |
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 <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std; typedef long long ll; int n;
ll m; ll cal(ll i,ll j) {
return i * i + j * j + (i - j) * + i * j;
} bool judge(ll x) {
ll sum = ;
for(int j = ; j <= n; ++j) {
int l = ,r = n;
while(l < r){
int mid = (l + r + ) / ;
if(cal(mid,j) <= x) {
l = mid;
} else {
r = mid - ;
}
}
sum += l;
} return sum >= m; } void solve() {
ll l = -1e12,r = 1e12; //printf(" n = %d l = %lld r = %lld\n",n,l,r); while(l < r) {
ll mid = (l + r) >> ;
if(judge(mid)) r = mid;
else l = mid + ;
} printf("%I64d\n",l);
} int main() {
// freopen("sw.in","r",stdin); int t;
scanf("%d",&t); while(t--) {
scanf("%d%I64d",&n,&m);
solve();
} return ;
}
POJ 3685的更多相关文章
- 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 Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7415 Accepted: 2197 Descriptio ...
- 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 ...
- Divide and conquer:Matrix(POJ 3685)
矩阵 题目大意:矩阵里面的元素按i*i + 100000 * i + j*j - 100000 * j + i*j填充(i是行,j是列),求最小的M个数 这一题要用到两次二分,实在是二分法的经典,主要 ...
- 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 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 矩阵问题 查找第K小的值
题意:N阶矩阵Aij= i2 + 100000 × i + j2 – 100000 × j + i × j,求第M小的元素. 思路:双重二分 考虑到,aij是跟着i递增的,所以i可以作为一个二分搜索 ...
随机推荐
- JTable的DefaultModel方法getValueAt(a,row)
行和列都是从0开始索引的,而且不包括netbeans生成的表格头,而是从数据开始,否则就会报错
- [整理归档]30 common tasks you perform using the GUI that you can do faster in Windows PowerShell
主要内容来自于 http://channel9.msdn.com/Events/TechEd/Australia/2014/DCI316 可以下载PPT以及视频,个人只是整理一下平时常用的 NetWo ...
- 浅谈HAL
参考:http://blog.csdn.net/mr_raptor/article/details/8074549 代码实现:http://blog.csdn.net/mr_raptor/articl ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
- Javascript是一个事件驱动语言
面向原型这种说法我没在网上找到
- SQLite之写一个表
1.首先你需要一个路径. 获取document目录并返回数据库目录 - (NSString *)dataFilePath{ NSArray *paths = NSSearchPathForDirect ...
- iOS学习之UI自定义cell
一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...
- WinForm GDI+自定义控件总结(一)
前言 由于项目的原因好久没写博客了,也正是项目的原因开始系统的学习WinForm,从而接触到自定义控件的开发.自定义控件的开发有一定的难度,对开发者要求比较高,需要了解Windows运行的机制,熟悉w ...
- ListView usage in ERP-DEV
ListView Learning Note how to add double click event to listviewitem in ListView. refer link in stac ...
- c++学习笔记之变量
变量的命名规则:标示符要能体现含义,变量的名字一般用小写,用户自己定义的类一般第一个字母大写,如果标示符有多个单词组成,则需要加下划线.' 变量声明和定义的关系:程序有多个文件组成,有时候需要再多个文 ...