【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 ...
随机推荐
- 关于STM32的I2C硬件DMA实现
关于STM32的I2C硬件DMA实现 网上看到很多说STM32的I2C很难用,但我觉得还是理解上的问题,STM32的I2C确实很复杂,但只要基础牢靠,并没有想象中的那么困难. 那么就先从基础说起,只说 ...
- spring-boot-configuration-processor 是干啥用的
spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了 引入pom依赖 <de ...
- Python IDE Ⅱ
3.设置Pydev 安装完成后,还需要设置一下PyDev,选择Window -> Preferences来设置PyDev.设置Python的路径,从Pydev的Interpreter - Pyt ...
- Educational Codeforces Round 13 D. Iterated Linear Function 逆元+公式+费马小定理
D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes input s ...
- c语言if-else的效率比较
闲着没事测试下if-else的执行效率 测试环境:Mac pro i7 2.3Ghz ...编译器gcc 4.9,代码没有进行优化-O0: 测试代码:c代码1: int main(){ int n=1 ...
- 基于CentOS 7下最小化安装的操作系统搭建Zabbix3.0环境
环境说明 系统版本:CentOS Linux release 7.3.1611 (Core) 内核版本:3.10.0-514.el7.x86_64 Httpd版本:Apache/2.4.6 (Cent ...
- django 问题总结(八)
1.第一步创建项目,不成功,命令不报错一直不创建文件夹 django-admin.py startproject mysite2 原因: django-admin.py ,py文件的默认打开方式不对, ...
- initData()
void initData(){ int i, j; //加载数据时让用户等待,一般情况加载数据比较快 printf("游戏加载中,请稍后........."); //遍历地图中的 ...
- 深入理解Java的三大特性之多态
世界上最美丽的东西,看不见也摸不着,要靠心灵去感受. ——海伦·凯勒 面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响类使用的情况下改变类的内部结构,并保护数据.对 ...
- 黑马vue---19、v-for中key的使用注意事项
黑马vue---19.v-for中key的使用注意事项 一.总结 一句话总结: 必须 在使用 v-for 的同时,指定 唯一的 字符串/数字 类型 :key 值 <p v-for="i ...