当前位置:网站首页>AcWing 244. Enigmatic cow (tree array + binary search)
AcWing 244. Enigmatic cow (tree array + binary search)
2022-07-04 08:09:00 【Jacob* ̄▽ ̄*】
The question :
Ideas :
We use it h[]
I can infer the height of all cows ,a[i]
Indicates the current i
How many cows are low on the left of the cow ,c[]
Represents a tree array .
We found that , if The first i
On the left side of the cow a[i]
A cow is shorter than it , So its height h[i]
Two conditions have to be met :
① In numbers
1~n
Middle ranking The firsta[i] + 1
Small .② Not in the
h[i+1],h[i+2],…,h[n]
in There have been .( Because we use reverse order traversal ,
h[i+1]~h[n]
These arei
The height of all cows on the right of the cow has been determined , Of course not )
General process :
We Traverse each number in reverse order a[i]
:
① Each time from Of the remaining numbers Find No
k=a[i]+1
Small number ( This is the law we found above ).② Delete The number of .
analysis :
The answer can already be found by performing the above two steps , But the range of observation data :n<=1e5
, step ① in Because when traversing each number, you have to find the target value from the remaining numbers , need O(n^2)
Level of complexity , We obviously need to optimize , Consider using Tree array plus two points To optimize .
First, let a[1]~a[n]
All for 1
, Express We can use this height once more , Use Tree array c
stay O(logn)
Within the time complexity of maintenance a[1]~a[n]
Of The prefix and ( Tree arrays are used to maintain prefixes and ), If you want to Delete a number Subtract directly from this position 1 that will do ( Single point modification , It's also O(logn),add(x, -1)
)
In the subject ,ask(x)
Indicates the interval 1~x
in How many numbers are left , If we want to be in the rest ask(x)
Find the number k
Small number , What should be looked for is the smallest x
, bring ask(x)=k
.
That's the step ② It is equivalent to finding the smallest x
, bring ask(x)=k
.(ask(x)<k
Express 1~x
Moderate deficiency k
Number , Surely not )
because ask(x)
Obviously monotonous , Therefore, we can use dichotomy to find the above search process .
To sum up : in other words , We want to Maintain one in real time 01
Sequence , Support Query the first k
individual 1
The location of ( Binary search left boundary ), as well as Modify a value in the sequence .
(01
Sequence It's constantly changing , therefore Left and right boundaries of binary search It always includes the whole 1~n
Region )
Time complexity :
O(n(logn^2))
( Tree arrays and binary constants are very small , Maybe faster than the balance tree )
Code :( The code details are reflected in the above problem solution )
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int h[N], a[N], c[N];
int n;
inline int lowbit(int x) {return x&(-x);}
int ask(int x)
{
int ans = 0;
while(x) {ans+=c[x], x-=lowbit(x);}
return ans;
}
int add(int x, int y)
{
for(; x<=n; x+=lowbit(x)) c[x]+=y;
}
int main()
{
cin>>n;
add(1, 1);
for(int i=2;i<=n;++i)
{
scanf("%d", &a[i]);
add(i, 1);
}
for(int i=n; i>=1; --i)
{
int k = a[i]+1;
int l = 1, r = n;//01 The sequence is constantly changing , Therefore, the left and right boundaries of binary search always contain the whole 1~n Region
while(l<r)
{
int mid = l+r>>1;
if(ask(mid)>=k) r = mid;
else l = mid + 1;
}
h[i] = l;
add(l, -1);
}
for(int i=1;i<=n;++i) printf("%d\n", h[i]);
return 0;
}
边栏推荐
- L1-021 important words three times (5 points)
- Comparison between applet framework and platform compilation
- Oceanbase is the leader in the magic quadrant of China's database in 2021
- How to send mail with Jianmu Ci
- zabbix监控系统邮件报警配置
- Li Kou today's question -1200 Minimum absolute difference
- 促进OKR落地的工作总结该如何写?
- JVM中堆概念
- Practice (9-12 Lectures)
- Moher College phpmailer remote command execution vulnerability tracing
猜你喜欢
【Go基础】1 - Go Go Go
zabbix 5.0监控客户端
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
Ecole bio rushes to the scientific innovation board: the annual revenue is 330million. Honghui fund and Temasek are shareholders
NPM run build error
Take you to master the formatter of visual studio code
如何用MOS管来实现电源防反接电路
In the era of low code development, is it still needed?
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
随机推荐
R language uses cforest function in Party package to build random forest based on conditional inference trees, uses varimp function to check feature importance, and uses table function to calculate co
R language ggplot2 visualization: ggplot2 visualization grouping box diagram, place the legend and title of the visualization image on the top left of the image and align them to the left, in which th
Unity write word
【性能測試】一文讀懂Jmeter
[test de performance] lire jmeter
Div hidden in IE 67 shows blank problem IE 8 is normal
Unity-Text上标平方表示形式+text判断文本是否为空
Introduction to neural network (Part 2)
How to improve your system architecture?
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
Activiti常见操作数据表关系
Moher college phpMyAdmin background file contains analysis traceability
Use preg_ Match extracts the string into the array between: & | people PHP
OKR vs. KPI 一次搞清楚这两大概念!
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
MySQL 数据库 - 函数 约束 多表查询 事务
Preliminary study on temporal database incluxdb 2.2
21 examples of strategic goals to promote the rapid development of your company
SSRF vulnerability exploitation - attack redis
Oceanbase is the leader in the magic quadrant of China's database in 2021