【POJ - 3685】Matrix(二分)
Matrix
Descriptions
有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值.
Input
第一行输入T代表测试组数.
每个测试用例包含2个数字N,M表示在N阶方阵找出第M大值, N(1 ≤ N ≤ 50,000) and M(1 ≤ M≤ N × N). 每两个测试用例之间可能有空行
Output
输出方阵的第M小值
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
题目链接
https://vjudge.net/problem/POJ-3685
在main函数中
设mid为N阶方阵中第sum小的数,若sum<M,则mid小了
若sum>M,则mid大了
在judge函数中
(mid,j)即为(i,j),一列一列的找有mid个数比value小,再把每一列的mid加一起,即可得出在N阶方阵中比value小的数的个数sum
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 100000+5
using namespace std;
ll T,n,m;
ll fun(ll i,ll j)//求值
{
return i*i+*i+j*j-j*+i*j;
}
ll judge(ll value)
{
ll l,r,mid,sum;
sum=;
for(ll j=;j<=n;j++)
{
l=,r=n+;
while(r-l>)
{
mid=(l+r)/;
if(fun(mid,j)<value)
l=mid;
else
r=mid;
}
sum+=l;
}
return sum;
}
int main()
{
scanf("%lld",&T);
while(T--)
{
scanf("%lld",&n);
scanf("%lld",&m);
ll l,r,mid;
l=-1e12,r=1e12;//设置上下界
while(r-l>)
{
mid=(l+r)/;
if(judge(mid)<m)
l=mid;
else
r=mid;
}
printf("%lld\n",l);
}
return ;
}
【POJ - 3685】Matrix(二分)的更多相关文章
- 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 二分 函数单调性 难度: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(二分搜索之查找第k大的值)
Description Given a N × N matrix A, whose element × i + j2 - × j + i × j, you are to find the M-th s ...
- POJ - 3685 Matrix
二分kth,答案满足的条件为:m ≤ 小于等于x的值数cntx.x和cntx单调不减,随着x增大,条件成立可表示为:0001111. 本地打一个小型的表可以发现列编号j固定时候,目标函数f(i,j)似 ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
- POJ3685 Matrix —— 二分
题目链接:http://poj.org/problem?id=3685 Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissio ...
- poj 2318 叉积+二分
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13262 Accepted: 6412 Description ...
- POJ poj 2155 Matrix
题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...
随机推荐
- ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to this MySQL server
[root@zstedu mysql]# mysql --protocol=tcp -h 127.0.0.1 -uroot -pEnter password: ERROR 1130 (HY000): ...
- BZOJ 4522: [Cqoi2016]密钥破解 (Pollard-Rho板题)
Pollard-Rho 模板 板题-没啥说的- 求逆元出来后如果是负的记得加回正数 CODE #include<bits/stdc++.h> using namespace std; ty ...
- Acwing-287-积蓄程度(树上DP, 换根)
链接: https://www.acwing.com/problem/content/289/ 题意: 有一个树形的水系,由 N-1 条河道和 N 个交叉点组成. 我们可以把交叉点看作树中的节点,编号 ...
- okclient2详细介绍
在 Java 程序中经常需要用到 HTTP 客户端来发送 HTTP 请求并对所得到的响应进行处理.比如屏幕抓取(screen scraping)程序通过 HTTP 客户端来访问网站并解析所得到的 HT ...
- Python基础--基于ubuntu linux基础知识点
part1:Python编译 1.直接在终端编译-----在ubuntu中Python是在home文件夹下的,输入Python(Python2编译,想要Python3直接输入python3) 一般推荐 ...
- 用php把excel数据导入数据库
PHPExcel是一个PHP类库,用来帮助我们简单.高效实现从Excel读取Excel的数据和导出数据到Excel. 先下载PHPExcel类库· 读取文件源码: <?php header(&q ...
- Navicat创建连接
https://blog.csdn.net/suprezheng/article/details/90037702 以下是不用创建直接可用的
- learning memchr func
extern void *memchr(const void *buf, int ch, size_t count); 用法:#include <string.h> 功能:从buf所指 ...
- [Luogu] 引水入城
https://www.luogu.org/problemnew/show/P1514 bfs + 线段覆盖 #include<bits/stdc++.h> using namespace ...
- python-解决pip安装速度慢的问题--豆瓣镜像
https://www.cnblogs.com/ZhangRuoXu/p/6370107.html https://blog.csdn.net/tianguiyuyu/article/details/ ...