POJ 3685 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);
}
}
POJ 3685 Matrix (二分套二分)的更多相关文章
- poj 3685 Matrix 二分套二分 经典题型
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5724 Accepted: 1606 Descriptio ...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- poj3579 二分套二分
和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...
- POJ-3579 Median---二分第k大(二分套二分)
题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...
- 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 Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that e ...
- POJ 3233 Matrix Power Series 二分+矩阵乘法
链接:http://poj.org/problem?id=3233 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思 ...
- POJ 3233 Matrix Power Series --二分求矩阵等比数列和
题意:求S(k) = A+A^2+...+A^k. 解法:二分即可. if(k为奇) S(k) = S(k-1)+A^k else S(k) = S(k/2)*(I+A^(k/2)) ...
随机推荐
- OneProxy常用参数说明
5.2.OneProxy常用参数说明 OneProxy的所有可用参数可通过oneproxy --help-all查看.所有参数均可以写入文件中,由OneProxy启动时加载 5.2.1.基本参数 -- ...
- 转转转!!Spring MVC控制器用@ResponseBody声明返回json数据报406的问题
本打算今天早点下班,结果下午测试调试程序发现一个问题纠结到晚上才解决,现在写一篇博客来总结下. 是这样的,本人在Spring mvc控制层用到了@ResponseBody标注,以便返回的数据为json ...
- struts2学习(1)struts2 helloWorld
一.struts2简介: 二.helloWorld: 1)工程结构: HelloWorldAction.java: package com.cy.action; import com.opensymp ...
- Bootstrap-Other:Less 教程
ylbtech-Bootstrap-Other:Less 教程 1.返回顶部 1. 2. 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. http://www.runoob. ...
- 笔记本制作centos qcow2格式文件
笔记本win7先通过vbox安装好centos6.5 然后打开cmd命令行在c:\Program Files\Oracle\VirtualBox下执行 vboxmanage clonehd --for ...
- flask的第一个例子
对django有了一些基础了解,现在看看flask,然后如果有时间可以看看 web.py 那么咱们开始flask的第一个例子 先安装flask pip install flask 看官方的教程 我也把 ...
- asp.net 不用控件,自动登录(用于和其他系统对接的时候,自动登录系统,用户体验好)
if (System.Web.Security.Membership.ValidateUser("admin", "123456")) { //这句话很重要,他 ...
- 15 并发编程-(IO模型)
一.IO模型介绍 1.阻塞与非阻塞指的是程序的两种运行状态 阻塞:遇到IO就发生阻塞,程序一旦遇到阻塞操作就会停在原地,并且立刻释放CPU资源 非阻塞(就绪态或运行态):没有遇到IO操作,或者通过某种 ...
- 前端开发之jQuery位置属性和筛选方法
主要内容: 1.jQuery的位置属性及实例 (1)位置属性 (2)实例 --- 仿淘宝导航栏 2.jQuery的筛选方法及实例 (1)筛选方法 (2)实例一:嵌套选项卡 (3)实例二:小米官网滑动 ...
- 对List集合进行排序
一.说明 使用Collections工具类的sort方法对list进行排序 新建比较器Comparator 二.代码 排序: import java.util.ArrayList; import ja ...