当前位置:网站首页>[2021] number of effective sequences programmed by Tencent autumn recruitment technology post
[2021] number of effective sequences programmed by Tencent autumn recruitment technology post
2022-04-21 19:50:00 【sigd】


If the data in the sequence is not repeated , Then the problem will be much less difficult . This paper introduces three solutions .
(1) Violence enumeration
Violent enumeration subsequence left and right endpoints i and j, Check a[i+1] To a[j-1] Whether there is a smaller value than the endpoint , Complexity O(N^3). Available 20 branch .
(2)RMQ Optimize
RMQ(Range Minimum/Maximum Query) It can be regarded as a search structure , We can find the extremum in the interval quickly , The complexity of each search is O(1) Level . In this way, the above algorithm “ Check a[i+1] To a[j-1] Whether there is a smaller value than the endpoint ” Optimize , The complexity of the whole algorithm O(N^2), Available 70 branch .
Introduce a trick to cheat points , When the author is working out big data , Such as the above 100000 Level data , Often, all data points are the same , Or is it 1,2,3,4.....100000 Such data points . If there is no good way in the end , And plenty of time , You can write code to deal with these special situations . This method does not guarantee effectiveness !!!
(3) Monotonic stack
Wise remark of an experienced person : If you enumerate a[i] It is the small value of sequence , Find the data a[i] The first problem at the left and right ends is much smaller than him , This is a typical monotone stack problem . Readers must master the basic usage of monotone stack before they can read the following problem-solving ideas .
This topic can consider enumerating the second i Elements ,a[i] Find the first one to the left a[i] Small value a[k], here [k,i] The interval must satisfy the condition ,
So can we continue to look to the left for smaller ? After simulation, it is found that it is not possible , For example, the sequence 1 3 6 5 ,5 Find... On the left 3 You can stop , With 5 The next smallest sequence is (3,6, 5),(1,3,6,5) You can't . Of course, it's possible i There is nothing smaller on the left side of the element , In this way, no subsequence satisfying the condition can be found .
The question translates to a[i], Whether there is a smaller value on the left and right sides .
If the data in the sequence is not repeated , According to the above analysis, do it once in both directions with monotone stack , enumeration i The element is the next hour, and the result can be obtained in all cases . If the data is repeated ?
For duplicate data , such as 1,3,1,4,1, The first 5 individual 1 Is a minor element , There are two on the left 1, In monotonous stack processing, if you meet the conditions, you will stop the stack operation . In this case, you need to adjust the lower stack structure , Keep it from storing duplicate elements , But you can know how many repeating elements there are . There are many ways , For example, the stack element uses a structure , Or use a counter (map) Record the number of elements with the same value in the stack ( The code to use map).
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n,a[100005];
ll ans=0;
int main()
{
ios::sync_with_stdio(0),cin.tie(0);
int i,j;
cin>>n;
for(i=1; i<=n; i++)
cin>>a[i];
stack<int>st;
map<int,int>mp;/**< mp Counter , Used to record the number of the same elements in the stack , Ensure that the elements in the stack must be incremented , There won't be the same value */
for(i=1; i<=n; i++)
{
while(st.size()&&a[st.top()]>a[i])
{
mp[a[st.top()]]=0;/**< The stack counter is cleared */
st.pop();
}
if(st.empty())
st.push(i);
else
{
if(a[i]>a[st.top()]) /**< This is the calculation a[i] Is the second smallest value , On the left a[i] The big ones are out of the stack , Stack top ratio a[i] Small */
ans++,st.push(i);
else if(a[i]==a[st.top()])
{/**< Handle stack top and a[i] The same thing , For example, in this case 3 3 3 3, The first 4 individual 3 And some on the left 3 Can match */
ans+=mp[a[st.top()]];/**< Be careful not to enter the stack at this time , The top value of the stack will be mp[a[i]]++ */
if(st.size()>1)/**< 1 3 3 3 3, This is the first time 4 individual 3 Not only on the left 3 Can match , And 1 collocation , Judge whether there is anything better than a[i] Small */
ans++;
}
}
mp[a[i]]++;
}
while(st.size())
st.pop();
for(i=n; i>=1; i--)/**< In reverse, there is no need to deal with duplicate elements */
{
while(st.size()&&a[st.top()]>=a[i])
st.pop();
if(!st.empty())
ans++;
st.push(i);
}
cout<<ans;
return 0;
}
When the author writes the code on the spot, he can solve the problem only once .
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n,a[100005];
ll ans=0;
int main()
{
ios::sync_with_stdio(0),cin.tie(0);
int i,j;
cin>>n;
for(i=1; i<=n; i++)
cin>>a[i];
stack<int>st;
map<int,ll>mp;/**< mp Counter , Used to record the number of the same elements in the stack , Ensure that the elements in the stack must be incremented , There won't be the same value */
for(i=1; i<=n; i++)
{
while(st.size()&&a[st.top()]>a[i])
{/**< The following sentence is actually a calculation a[i] Is the minimum case , Because all out of stack elements , Their values are greater than a[i]
Why mp, Example sequence 3 3 3 3 1, At this time, there is only one in the stack 3, however mp[3]=4, and 1 And the one on the left 4 individual 3 Can form a sequence */
ans+=mp[a[st.top()]];
mp[a[st.top()]]=0;/**< The stack counter is cleared */
st.pop();
}
if(st.empty())
st.push(i);
else
{
if(a[i]>a[st.top()]) /**< This is the calculation a[i] Is the second smallest value , On the left a[i] The big ones are out of the stack , Stack top ratio a[i] Small */
ans++,st.push(i);
else if(a[i]==a[st.top()])
{ /**< Handle stack top and a[i] The same thing , For example, in this case 3 3 3 3, The first 4 individual 3 And some on the left 3 Can match */
ans+=mp[a[st.top()]];/**< Be careful not to enter the stack at this time , The top value of the stack will be mp[a[i]]++ */
if(st.size()>1)/**< 1 3 3 3 3, This is the first time 4 individual 3 Not only on the left 3 Can match , And 1 collocation , Judge whether there is anything better than a[i] Small */
ans++;
}
}
mp[a[i]]++;
}
cout<<ans;
return 0;
}
版权声明
本文为[sigd]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211943222494.html
边栏推荐
- Redefinition of global variables in C language in multiple files
- 【转载】devm_xxx机制
- leetcode344. Reverse string
- Interesting souls are the same. It takes only 2 steps to slide the computer off
- 剑指Offer:[第29天 动态规划(困难)]--->n个骰子的点数
- getchar,putchar,EOF
- “最难就业季“中的大学生就业:本硕过半有着落 高职生成香饽饽
- Redis installation and configuration startup
- 05. Prototype mode
- 新冠无情人间有情,欣隆农业保民生共抗疫——慰问抗疫一线及爱老助困送欣隆酵醒鸡蛋蔬菜进社区公益行动
猜你喜欢
![Leetcode goat Latin [analog string] the way of leetcode in heroding](/img/da/3761b46aec6a56f2f30e0fd91c7f42.png)
Leetcode goat Latin [analog string] the way of leetcode in heroding

Interface component telerik UI for WPF Getting Started Guide - color theme generator

开源许可证热门及冷门问题接地气探讨

【Gradle】问题解析+下载安装+环境配置+验证安装

联想公布ESG新进展:承诺2025年全线计算机产品100%含有再生塑料

高性能分布式缓存Redis--- Redis底层结构和缓存原理 --- 持续更新

URL下载网络资源

Interesting souls are the same. It takes only 2 steps to slide the computer off

URL to download network resources

Yijia announced that its products will enter oppo stores in the future and will launch the group's new resources
随机推荐
使用CMake构建/CMake命令参考
js监测手机屏幕旋转(横屏还是竖屏)
juc-Queue接口以及其实现类
一键安装ROS和rosdep(NO 墙)
杰理之使用不可屏蔽中断的开关中断函数【篇】
剑指Offer:[第29天 动态规划(困难)]--->n个骰子的点数
Nodejs notes 1
WLAN Qpower 介绍
高端制造业企业信息化解决方案,工业电商平台设备、数据、体系预测性维护
Building / importing targets using cmake
SAP MTS/MTO/ETO专题之七:Q+M模式前后台操作
【玩转Lighthouse】使用腾讯云轻量实现微信支付业务
阿里IOT
如何判断Int型值的第nbit位是否是1还是0
GBase 8a对 double 数值进行 round 取舍结果不是四舍五入问题分析及解决方案
【Gradle】问题解析+下载安装+环境配置+验证安装
int count= cmd.ExecuteNonQuery();附近有一条语法错误
getchar,putchar,EOF
SAP MTS / MTO / ETO topic 7: front and rear operation in Q + M mode
Building / building reusable QML modules using cmake