POJ 3685 Matrix (二分套二分)
2024-08-31 01:41:05
Matrix
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 8674 | Accepted: 2634 |
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
POJ Founder Monthly Contest – 2008.08.31, windy7926778
题意:定义矩阵中(i,j)位置的值为(i^2+j^2+i*j+100000*(i-j)
求n*n矩阵中第m小的数
题解:首先打表或者推式子发现列是单调递增的,所以可以考虑二分答案,对于每一列二分找到有几个数比他小,然后就可以nlogn作出总共有几个数比他小,然后就搞定了
代码如下:
#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson ch[x][0]
#define rson ch[x][1]
#define hi puts("hi!");
#define int long long
using namespace std; int n,m; int get(int ith,int jth)
{
return ith*ith+100000ll*ith-100000ll*jth+jth*jth+ith*jth;
} int find(int x,int jth)
{
int l=,r=n,mid;
while(l<=r)
{
mid=(l+r)>>;
if(get(mid,jth)<=x)
{
l=mid;
}
else
{
r=mid-;
}
if(r-l<=)
{
if(get(r,jth)<=x) mid=r;
else mid=l;
break;
}
}
return mid;
} int check(int x)
{
int cnt=;
for(int i=;i<=n;i++)
{
cnt+=find(x,i);
}
if(cnt>=m) return ;
else return ;
} signed main()
{
int ttt;
scanf("%lld",&ttt);
while(ttt--)
{
int ans;
scanf("%lld%lld",&n,&m);
int l=-1e12,r=1e12,mid;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid))
{
ans=mid;
r=mid-;
}
else
{
l=mid+;
}
}
printf("%lld\n",ans);
}
}
最新文章
- android-studio设置代理
- I2C总线和S5PV210的I2C总线控制器
- mybatis xml 中的特殊符转义字符号和模糊查询
- SharpDX之Direct2D教程II——加载位图文件和保存位图文件
- Docker系列(四)Dockerfile
- Codeforces 553D Nudist Beach(图论,贪心)
- javascript组成概述认识
- git 提示error setting certificate verify locations 解决方案
- nodeJs配置
- 学习 Spring (五) Aware 接口
- MATLAB 统计元素出现的次数
- 操作系统介绍-操作系统历史,IO,进程的三态,同步异步阻塞非阻塞
- JavaScript学习 - 基础(六) - DOM基础操作
- js增减日期
- Gentoo rc-update service ‘net.eth0′ does not exist
- JavaScript 判断输入是否为中文的函数
- NO.5:自学python之路------标准库,正则表达式
- 移动 App 接入 QQ 登录/分享 图文教程
- cogs696 longest prefix
- Zstack中任务,事件,消息之间的关系
热门文章
- C#多线程编程之:异步方法调用
- Makefile编写 三 伪目标的作用
- 关于Fragment框架,说的够清晰了。。。
- C++ 实例化对象 p->;printX()
- Java-Runoob-高级教程-实例-环境设置实例:2.Java 实例 – Java 如何运行一个编译过的类文件?
- Bootstrap-CL:分页
- phpcms 实现动态价格
- SPM——How to use github
- OpenCL Hello World
- 【CentOS 6.5】【转】新版本linux生成xorg.conf