poj 3685 二分
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 7415 | Accepted: 2197 |
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
//首先二分答案mid,然后在矩阵中找小于mid的个数和m比较,i*i+j*j+i*j+1e5*(i-j) 是关于i递增的即矩阵的每一列都是递增的,
//所以可以枚举列数二分行数来找。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const ll qq=;
ll t,n,m;
ll solve(ll p)
{
ll sum=;
for(ll j=;j<=n;j++){
ll l=,r=n,ans=;
while(l<=r){
ll i=(l+r)>>;
if((i*i+j*j+i*j+qq*(i-j))<p) { ans=i;l=i+; }
else r=i-;
}
sum+=ans;
}
return sum;
}
int main()
{
scanf("%lld",&t);
while(t--){
scanf("%lld%lld",&n,&m);
ll l=-1e13,r=1e13,ans;
while(l<=r){
ll mid=(l+r)>>;
ll tmp=solve(mid);
if(tmp<=m-) { ans=mid;l=mid+; }
else r=mid-;
}
printf("%lld\n",ans);
}
return ;
}
poj 3685 二分的更多相关文章
- 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 - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- 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 二分
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5489 Accepted: 1511 Descriptio ...
- 【POJ - 3685】Matrix(二分)
Matrix Descriptions 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. In ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
随机推荐
- jQuery控制a标签不可用
$('.disableCss').removeAttr('href');//去掉a标签中的href属性 $('.disableCss').removeAttr('onclick');//去掉a标签中的 ...
- CS小分队第二阶段冲刺站立会议(6月3日)
昨日成果:完成了主界面按钮移动交换位置 遇到问题:最后的时候发现仅交换了按钮在数据库中的信息,对于按钮的链接忘记交换了 今日计划:解决这个问题,对这个冲刺阶段的成果进行整理
- class 3 求数组中的最大值(单元测试)
1.问题引出: int Largest(int list[], int length) { int i,max; ; i < (length – ); i ++ ) { if(list[i] & ...
- WebGL七点二
与上一节相比这一节相当做了些整合和整理,目的是提高了读写效率和减少代码量,就是做了优化.这里我们只需要在顶点着色器中定义一个用于从js传递参数的变量u_MvcMatrix一看就知道是uniform变量 ...
- nodeJS + webStrome
一.配置开发环境: 1.先安装node (1).访问http://nodejs.org打开安装包,正常安装,点击next即可. 为了测试是否安装成功,打开命令提示符,输入node,则进入node.js ...
- java集合ArrayList
基于jdk_1.8.0 关于List,主要是有序的可重复的数据结构.jdk主要实现类有ArrayList(底层使用数组).LinkedList(底层使用双向链表) ArrayList: (一)继承关系 ...
- 团队作业4——第一次项目冲刺(Alpha版本)2017.11.16
1.当天站立式会议照片 本次会议在5号公寓3楼召开,本次会议内容:①:熟悉每个人想做的模块.②:根据老师的要求将项目划分成一系列小任务.③:在上次会议内容完成的基础上增加新的任务. 2.每个人的工作 ...
- python字符串操作、文件操作,英文词频统计预处理
1.字符串操作: 解析身份证号:生日.性别.出生地等. 凯撒密码编码与解码 网址观察与批量生成 解析身份证号:生日.性别.出生地等 def function3(): print('请输入身份证号') ...
- Scrum 项目4.0&&5.0
MY—HR 成员: 角色分配 学号 博客园 4.0团队贡献分 5.0团队贡献分 丘惠敏 PM项目经理 201406114203 http://www.cnblogs.com/qiuhuimin/ 19 ...
- 0428-Scrum团队成立
------------------------------3.0------------------------------------------ 一.项目要求 5.Scrum团队成立 5.1 团 ...