poj3685 二分套二分
Crawling in process... Crawling failed Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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矩阵,每一个位置都有一个值,这个值由该点的该点的行列标决定,问你第m小的元素是多少。
思路分析:比赛时都贴上二分标签了,最后还没敲出来,首次我们要二分答案,然后check,check函数里面按列
进行枚举,因为在j确定的情况下函数关于i递增,然后直接二分查找刚好小于等于mid的元素在每一列的位置,累加
return,判断返回的数与m的关系,继续二分
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int inf=1e9;
typedef long long ll;
const int C=;
ll f(ll x,ll y)
{
return (x*x+C*x+y*y-C*y+x*y);
}
ll n,m;
ll checknum(ll num)
{
ll cnt=;
for(int i=;i<=n;i++)//枚举列,因为在列数确定的情况下表达式关于i是递增的
{
ll sl=,sr=n;
int ant=;
while(sl<=sr)
{
ll smid=(sl+sr)>>;
if(f(smid,i)<=num)
{
ant=smid;
sl=smid+;
}
else sr=smid-;
}
cnt+=ant;
}
return cnt;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
ll l=-*n,r=n*n+*n+n*n+n*n;
ll ans;
while(l<=r)
{
ll mid=(l+r)>>;
//cout<<mid<<endl;
if(checknum(mid)>=m)
{
ans=mid;
//cout<<ans<<endl;
r=mid-;
}
else l=mid+;
// cout<<ans<<endl;
}
printf("%lld\n",ans);
}
}
poj3685 二分套二分的更多相关文章
- poj3579 二分套二分
和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...
- POJ-3579 Median---二分第k大(二分套二分)
题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- poj 3685 Matrix 二分套二分 经典题型
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5724 Accepted: 1606 Descriptio ...
- Matrix [POJ3685] [二分套二分]
Description 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. Input 第一行输 ...
- 二分套二分 hrbeu.acm.1211Kth Largest
Kth Largest TimeLimit: 1 Second MemoryLimit: 32 Megabyte Description There are two sequences A and ...
- 51nod 1105(第K大数 二分套二分)
题目链接:http://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=620811 参考自:https://blog.csdn.net/f_ ...
- POJ 3685 Matrix (二分套二分)
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8674 Accepted: 2634 Descriptio ...
- Gym 101064 D Black Hills golden jewels 【二分套二分/给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合】
D. Black Hills golden jewels time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- python 安装中的错误解决
最近使用python3.5,其中遇到安装中的错误,现在记录下来,提个醒 1 安装setuptools 出现缺少 zlib模块 解决: 1 yum install zlib 2 yum install ...
- v9站点自定义变量
打开 \phpcms\modules\admin\templates\site_edit.tpl.php 文件,找到最后一个 </fieldset> ,在他后面添加一下代码:<!-- ...
- Css 八卦
<div class="box"> <div class="right_d"></div> <span class=& ...
- C语言+ODBC+SQL 连接
第一步:配置ODBC. ①.在控制面板找到ODBC,或者在控制面板上搜索ODBC.如图: ②.点击ODBC的添加按钮,选择SQL Server,这是会出现创建SQL Server的新数据源的对话框,我 ...
- printk和printf的区别
内核使用printk()打印! 应用层使用printf()打印! &&& 大部分常用的C库函数在Linux内核中都已经得到了实现.在所有没有实现的函数中,最著名的就数print ...
- 搭建splinter+python环境时遇到的错误
因为不想用urllib2了,没有用过splinter,今天就想试试,毕竟后者支持的功能更人性化/自动化. 1,安装splinter 安装过程很简单,安装了pip的话,执行: $ [sudo] pip ...
- 树形dp入门练习(hdu1011+hdu1061)
hdu1011 和 hdu1561类似,给定每个节点的花费以及价值,并且子节点必须在父亲节点取到以后才可以被取到 相当于是在树上进行的01背包 dp时考虑每一个子树 root和它的每一个儿子,状态转移 ...
- Java中4种权限的理解
1. 包访问权限 (1)包的理解:将一组相关的.有意义的类文件组织在一起(即相应的.java文件放在一个文件夹下)就构成了包或者类库.(每个类文件的开头都包含一个所属包的声明“package pac ...
- hdu 4841 圆桌问题(STL vector)
Problem Description 圆桌上围坐着2n个人.其中n个人是好人,另外n个人是坏人.如果从第一个人开始数数,数到第m个人,则立即处死该人:然后从被处死的人之后开始数数,再将数到的第m个人 ...
- 自定义input file样式
自定义input file样式:一般都是通过隐藏input,通过定义label来实现.这种做法要注意的是label的for属性要指定input对应的id; <!DOCTYPE html> ...