当前位置:网站首页>Lesson 1: finding the minimum of a matrix
Lesson 1: finding the minimum of a matrix
2022-07-07 09:32:00 【Fight! Sao Nian!】
subject :AcWing 1452. Find the minimum of the matrix
Given a n×n Matrix , The matrix contains n×n individual Different from each other The integer of .
Define the minimum : If the value of a number is smaller than that of all adjacent numbers , Then this value is called the minimum .
The adjacent numbers of a number refer to the four adjacent numbers in the upper, lower, left and right directions , Also note , The number at the boundary or corner may have less than four adjacent numbers .
Ask for in O(nlogn) Find the position of any minimum within the time complexity , And output its rows and columns .
The matrix in this question is hidden , You can use our preset int function query To get the value of a certain position in the matrix .
for example ,query(a,b) You can get the... In the matrix a Xing di b The value of the position of the column .
Be careful :
The rows and columns of the matrix are from 0 Numbered starting .
query() Function cannot be called more than (n+2)×⌈log2n⌉+n.
The answer is not unique , Output the position of any minimum value .
Data range
1≤n≤300, The integers in the matrix are int Within the scope of .
sample input :
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sample output :
[0, 0]
Topic analysis :
The first way to think about it is to start from the starting point and find something smaller than yourself , Keep looking for , Then we will definitely find the minimum ( Because the points are different )
But the worst case is to visit n2 Time .
The second method is dichotomy , First find the minimum value in the middle column , Then look at the left and right
1. If v<l,v<r that v Is the minimum
2. If l<v, Then there is a minimum on the left
3. If r<v, Then there is a minimum on the right
Why? ?
Suppose the right r<v, So in the middle mid This column is better than r Be big , Then we use the first method to find that it is definitely impossible to cross mid This column ( Because I will look for something better than r Small number ), So in mid There must be a minimum on the right , This reduces the amount of search by half , Such repetition will finally find .
// Forward declaration of queryAPI.
// int query(int x, int y);
// return int means matrix[x][y].
class Solution {
public:
vector<int> getMinimumValue(int n) {
typedef long long LL;
const LL INF = 1e15;
int l=0,r=n-1;
while(l<r) // Dichotomy
{
int mid = l + r >> 1;
LL val=INF;
int k=0; // Which row to save is the smallest
for(int i=0;i<n;i++)
{
int t=query(i,mid);
if(t<val)
{
val=t;
k=i;
}
}
LL left=mid?query(k,mid-1):INF; // Query left
LL right=mid+1<n?query(k,mid+1):INF; // Query the right
if(val<left&&val<right)return {
k,mid}; // If the current point is the minimum
else if(left<val)r=mid-1;
else l=mid+1;
}
int k;
LL val=INF;
for(int i=0;i<n;i++)
{
int t=query(i,l);
if(t<val)
{
val=t;
k=i;
}
}
return {
k,l};
}
};
边栏推荐
- 在EXCEL写VBA连接ORACLE并查询数据库中的内容
- Pick up the premise idea of programming
- Regularly modify the system time of the computer
- Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
- 印象笔记终于支持默认markdown预览模式
- nlohmann json
- Unity shader (learn more about vertex fragment shaders)
- Zen - batch import test cases
- Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
- Run can start normally, and debug doesn't start or report an error, which seems to be stuck
猜你喜欢
超十万字_超详细SSM整合实践_手动实现权限管理
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
Information Security Experiment 1: implementation of DES encryption algorithm
Implementation of corner badge of Youmeng message push
Dynamics 365Online ApplicationUser创建方式变更
Mysql database index study notes
Huawei hcip datacom core_ 03day
How to use clipboard JS library implements copy and cut function
Sublime Text4 download the view in bower and set the shortcut key
答案在哪里?action config/Interceptor/class/servlet
随机推荐
Some pit avoidance guidelines for using Huawei ECS
How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data
JS inheritance prototype
CMD startup software passes in parameters with spaces
Jmeters use
如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
四、机器学习基础
[4G/5G/6G专题基础-146]: 6G总体愿景与潜在关键技术白皮书解读-1-总体愿景
When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
Locust performance test 4 (custom load Policy)
The configuration and options of save actions are explained in detail, and you won't be confused after reading it
H5网页播放器EasyPlayer.js如何实现直播视频实时录像?
Run can start normally, and debug doesn't start or report an error, which seems to be stuck
Pytest installation (command line installation)
The use of recycling ideas
Add new item after the outbound delivery order of SAP mm sto document is created?
# Arthas 简单使用说明
Kubernetes cluster capacity expansion to add node nodes
scrapy爬虫mysql,Django等
Install pyqt5 and Matplotlib module