POJ3685 Matrix(嵌套二分)
同行元素递减,同列元素递增,采用嵌套二分的方法
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef __int64 LL; LL n, m;
LL F(LL l, LL r){
return l * l + 100000 * l + r * r - 100000 * r + l *r;
} LL solve(LL x){
LL sum = 0;
for(int i = 1; i <= n; i++){
int l = 1, r = n + 1;
while(l < r){
LL mid = (l + r)>>1;
if(F(mid, i) > x){
r = mid;
}else{
l = mid + 1;
}
}
sum += l - 1; }
return sum;
} int main(){
int t;
cin>>t;
while(t--){
cin>>n>>m;
LL l = -100000*n, r = n*n+100000*n+n*n+n*n;
while(l < r){
LL mid = (l + r) >> 1;
if(solve(mid) < m){
l = mid + 1; }else{ r = mid;
}
}
cout<<l<<'\n';
} return 0;
}
POJ3685 Matrix(嵌套二分)的更多相关文章
- poj3685(嵌套二分)
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 4658 Accepted: 1189 Descriptio ...
- POJ3685 Matrix —— 二分
题目链接:http://poj.org/problem?id=3685 Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissio ...
- LeetCode Search a 2D Matrix(二分查找)
题意: 有一个矩阵,每行都有序,每行接在上一行尾后仍然有序.在此矩阵中查找是否存在某个数target. 思路: 这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了.由于用vector ...
- fafu 1568 Matrix(二分匹配+二分)
Description: You are given a matrix which <= n <= m <= ). You are supposed to choose n el ...
- [LeetCode] 74 Search a 2D Matrix(二分查找)
二分查找 1.二分查找的时间复杂度分析: 二分查找每次排除掉一半不合适的值,所以对于n个元素的情况来说: 一次二分剩下:n/2 两次:n/4 m次:n/(2^m) 最坏情况是排除到最后一个值之后得到结 ...
- 【POJ - 3685】Matrix(二分)
Matrix Descriptions 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. In ...
- poj 3685 Matrix 【二分】
<题目链接> 题目大意: 给你一个n*n的矩阵,这个矩阵中的每个点的数值由 i2 + 100000 × i + j2 - 100000 × j + i × j 这个公式计算得到,N( ...
- Matrix(二分套二分)
Matrix http://poj.org/problem?id=3685 Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8 ...
- Search a 2D Matrix——两度二分查找
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- phpcms调用某个栏目下的所有二级栏目
在html中直接这样调用就可以了,subcat中的值为父栏目的catid {loop subcat(93) $r}{/loop} 注意subcat方法的参数,如果只写一个参数,也就是父栏目id,那该父 ...
- mysql同一台服务器上不同数据库中个别表内容同步
>>>>>>soft_wsx>>>>>>--数据备份与还原>>同步备用服务器--1.完全备份主数据库--2.使用带S ...
- SQLServer:FUNCTION/CURSOR/PROCEDURE/TRIGGER
一.FUNCTION:在sqlserver2008中有3中自定义函数:标量函数/内联表值函数/多语句表值函数,首先总结下他们语法的异同点:同点:1.创建定义是一样的: ...
- SharePoint更改密码
stsadm –o updatefarmcredentials –userlogin DomainName\UserName -password NewPassword –local 1. 通过管理 ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- XML文件的读取----cElementTree
XML文件如下: <?xml version="1.0" encoding="UTF-8"?> <tokenxml> <token ...
- nyoj163_Phone List_字典树
Phone List 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a list of phone numbers, determine if it i ...
- iOS-UIView 之 layoutMargins & preservesSuperviewLayoutMargins 解惑
这里先看下苹果给出的解释: iOS8.0之后,uiview默认layoutMargins 为(8,8,8,8),也可以自己指定,仅适用于自动布局:当添加子view到父view上时,这样设置好约束 默认 ...
- modbus协议讲义
Modbus 一个工业上常用的通讯协议.一种通讯约定.Modbus协议包括RTU.ASCII.TCP.其中MODBUS-RTU最常用,比较简单,在单片机上很容易实现.虽然RTU比较简单,但是看 ...
- 【leetcode】 Search a 2D Matrix (easy)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...