poj 3685 Matrix 二分套二分 经典题型
| Time Limit: 6000MS | Memory Limit: 65536K | |
| Total Submissions: 5724 | Accepted: 1606 |
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
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
long long mid,l,r,n,m;
long long v(long long i,long long j)
{
return i*i+*i+j*j-*j+i*j;
}
long long ok(long long x)
{
long long sum=;
for(long long j=;j<=n;j++)
{
long long l=,r=n+; /*2.这个地方r不能初始化为n,因为假设
n个数全是<x的话,按理讲应该sum+=n的,但是最后r-1之后sum+=(n-1)了,
这是因为v(n,j)>x和v(n,j)=x的效果是不同的,假设v(n,j)>x的话,r仍然为n+1,sum+//=n,而当v(n,j)=x话,r更新为n,sum+=n-1;*/
while(r-l>)
{
long long mid=(l+r)>>;
if(v(mid,j)>=x)
r=mid;
else
l=mid;
}
sum+=(r-);
}
return sum;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%lld %lld",&n,&m);
r=1e12;l=-1e12;
while(r-l>)
{
mid=(l+r)>>;
if(ok(mid)>=m)
r=mid;
else
l=mid;
}
printf("%lld\n",r-);
}
return ;
}
poj 3685 Matrix 二分套二分 经典题型的更多相关文章
- 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 (二分套二分)
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8674 Accepted: 2634 Descriptio ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- 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). 思 ...
随机推荐
- [转帖]linux 下yum使用技巧
linux 下yum使用技巧 https://www.cnblogs.com/galengao/p/5750389.html 本文来自我的github pages博客http://galengao.g ...
- 如何在java中去除中文文本的停用词
1. 整体思路 第一步:先将中文文本进行分词,这里使用的HanLP-汉语言处理包进行中文文本分词. 第二步:使用停用词表,去除分好的词中的停用词. 2. 中文文本分词环境配置 使用的HanLP-汉 ...
- python并发编程-进程理论-进程方法-守护进程-互斥锁-01
操作系统发展史(主要的几个阶段) 初始系统 1946年第一台计算机诞生,采用手工操作的方式(用穿孔卡片操作) 同一个房间同一时刻只能运行一个程序,效率极低(操作一两个小时,CPU一两秒可能就运算完了) ...
- JAVA基础:Java中equals和==的区别
java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号( ...
- Codeforces 1229B. Kamil and Making a Stream
传送门 注意到只要考虑祖先和后代之间的贡献 发现对于一个节点,他和所有祖先最多产生 $log$ 个不同的 $gcd$ 所以每个节点开一个 $vector$ 维护祖先到自己所有不同的 $gcd$ 和这个 ...
- 18 Python之初识面向对象
1. 类与对象 class Car: #类名首字母大写,严格遵守驼峰命名规范 pass #造车 c = Car() #类名() #创建对象 ##出场之后进行改装 c.color = "红色& ...
- python动态添加属性
class A: def __init__(self, info ={}): self.info = info def __getattr__(self, item): return self.inf ...
- PAT Basic 1020 月饼 (25 分)
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...
- host文件介绍
默认位置为%SystemRoot%\system32\drivers\etc\,但也可以改变. 动态目录由注册表键\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSe ...
- 配置Nexus为maven的私服
1.配置Nexus为maven的私服 第一种方式:在项目的POM中如下配置 <repositories> <repository> <id>nexus_public ...