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], ...
随机推荐
- Kubernetes探索学习002--Kubernetes的基本使用
Kubernetes 的基本使用方法 原则:使用YAML文件描述你要部署的API对象! 以部署nginx静态站点为例,具体操作及内容如下 1.编写YAML文件 [root@kubernetes01 ~ ...
- [linux] tmux终端复用神器 [转载]
转载https://www.cnblogs.com/kevingrace/p/6496899.html Tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授 ...
- LeetCode 561. Array Partition I (C++)
题目: Given an array of 2n integers, your task is to group these integers into npairs of integer, say ...
- 20135231 JAVA实验报告三:敏捷开发与XP实践
---恢复内容开始--- JAVA实验报告三:敏捷开发与XP实践 20135231 何佳 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习 ...
- web05-CounterServlet
电影网站:www.aikan66.com 项目网站:www.aikan66.com 游戏网站:www.aikan66.com 图片网站:www.aikan66.com 书籍网站:www.aikan66 ...
- 【搜索】POJ-3009 DFS+回溯
一.题目 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. B ...
- 配置高可用集群(实验) corosyne+pacemaker
环境准备: 一准备三个虚拟机,把/etc/hosts/文件配置好 192.168.43.9 node0 ...
- 如何修改eclipse下注释的颜色
修改注释颜色:window -- preferences -- java --editor --syntax coloring -- comments 中的前两个就是修改注释颜色的.点击右边的colo ...
- SQL SERVER ENTERPRISE EDITION-CORE VS SERVER+CAL – DEMO ON DIFFERENCES
SQL SERVER ENTERPRISE EDITION-CORE VS SERVER+CAL – DEMO ON DIFFERENCES DHARMENDRA KESHARIAugust 03, ...
- LinkedList,HashSet,HashMap
LinkedList底层源码是采用双向链表实现的 private static class Node<E> { E item;//节点值 Node<E> next;//节点后指 ...