当前位置:网站首页>AcWing 134. 双端队列
AcWing 134. 双端队列
2022-07-17 18:41:00 【hys__handsome】
(困难)队列+贪心
一、思路
这个问题很难直接求解,因为在不知道后面会有哪些数的情况下,我们作出的局部决策很可能造成某个数P与Q在同一队列中,这样只要后面出现了介于P和Q之间的数,不管放到哪里都会造成无解(无法把队列连成一个有序序列)。这种情况启发我们,必须考虑按照数值的大小顺序而不是读入的顺序进行计算。
因为sherry最后会把队列连成一个非降序列,所以我们不妨反过来思考,先把N个数A[1~N]从小到大排序,然后分成尽量少的几段,让每一段对应原问题中一个合法的双端队列。
二、做法
1、步骤
- 将所有元素升序排序。
- 接着计算具有单谷性质(元素 i d id id大小呈现U形分布)的段数,即为答案。为了方便计算单股性质的段数,需要将原来的升序序列切成每段元素值都相同的小段,然后根据当前是上升还是下降的状态区分地去一一去拼接。
2、拼接手法
需要手动去画图更容易理解。 l a s t last last 是最后一个元素的 i d id id ,如果当前是下降的,它就是下降这一段最小的 i d id id ,如果当前是上升的那么他就是上升这一段最大的 i d id id 。
- 设定初始状态是下降的。
- 如果当前状态是下降的:
- 若 l a s t last last 是大于这小段的最大 i d id id,那么拼接后仍是下降的,接着更新 l a s t last last 成这小段的最小 i d id id。
- 若 l a s t last last 是小于这小段的最大 i d id id,那么拼接后为上升的并形成了一个单谷,接着更新 l a s t last last 成这小段的最大 i d id id。
- 如果当前状态是上升的:
- 若 l a s t last last 是大于这小段的最小 i d id id,那么拼接后,前面那一段形成了一个单谷,然后从这小段开始新生成一个是下降转态的预备单谷,接着更新 l a s t last last 成这小段的最小 i d id id。
- 若 l a s t last last 是小于这小段的最小 i d id id,那么拼接后仍是上升的,接着更新 l a s t last last 成这小段的最大 i d id id。
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 200010;
pair<int,int> a[N];
int main(){
cin.tie(0)->sync_with_stdio(false);
int n; cin>>n;
for(int i = 0; i < n; i++) {
cin>>a[i].first;
a[i].second = i;
}
sort(a,a+n);
//dir=-1表示下降,反之上升
//而last表示当前双端队列拼接后的最后一个元素下标
int i = 0, last = n, dir = -1, res = 1;
for(int i = 0; i < n; ){
int j = i;
while(j < n && a[i].first == a[j].first) j++;
int minn = a[i].second, maxx = a[j-1].second;
if(dir == -1) {
//下降时
if(last > maxx) last = minn;
else {
dir = 1;
last = maxx;
}
} else {
//上升时
if(last < minn) last = maxx;
else {
dir = -1;
last = minn;
res++;
}
}
i = j;
}
cout<<res;
return 0;
}
参考
https://www.acwing.com/solution/content/27971/
边栏推荐
- QT use qlisview to realize QQ login history list
- Simulation of overvoltage protection (OVP) circuit based on PMOS
- ssh无密钥登录
- How to upgrade Flink job gracefully?
- Module 7 (Architecture Design of King glory mall)
- Code after annotation of hands-on deep learning (Second Edition) [continuous update]
- Onvif protocol related: 4.1.1 WS username token method to obtain wsusernametokenbean
- perl 命令批量替换文件中的一些内容
- 响应式织梦模板酒窖类网站
- 【码蹄集新手村 600 题】运算符 / 在不同的运算顺序中的类型转换
猜你喜欢

(PC+WAP)织梦模板服装礼服类网站
![[JS reverse crawler] - Youdao translation JS reverse practice](/img/cd/a44445422e2911b005519b5ec30ec0.jpg)
[JS reverse crawler] - Youdao translation JS reverse practice

响应式织梦模板物流货运服务类网站

Health prevention guide 3: health care

忘掉Postman,Apifox更好用

Attachment handling of SAP Fiori

torch. utils. data. Dataloader description

onvif协议相关:2.1.2 none方式获取截图url

【js逆向爬虫】-有道翻译js逆向实战

Responsive Zhimeng template logistics and freight service website
随机推荐
弹性负载均衡将访问流量自动分发到多台云服务器,扩展应用系统对外的服务能力,提高应用程序安全性。
Deep learning from getting started to giving up the 100 day challenge
STL string查找子串
基于PMOS的过压保护(OVP)电路仿真
命令行的一些常用操作命令及常见错误的解决办法
【腾讯蓝鲸】第七届 7·24 运维日节日祝福送上~ 快来许愿~
(PC+WAP)织梦模板服装礼服类网站
onvif协议相关:4.1.1 WS-Username token方式获取WSUsernameTokenBean
Codeforce:g. good key, bad key [greed]
"Technology podcast month" day 10: meta podcast: talk about Podcasting
[pumpkin Book ml] (task2) mathematical derivation of linear model (least squares estimation, generalized Rayleigh quotient, maximum likelihood estimation, etc.)
Onvif protocol related: common class description
onvif协议相关:4.1.2 WS-Username token方式获取token
响应式织梦模板酒窖类网站
【6.15】Codeforces Round #798 (Div. 2)
这些年我开源的几个小项目
大家好,问一下数据库没开始binlog如何实时同步么,有没有好的方案
Codeforce:a. difference operations [mathematical thinking]
Transphorm's surface mount packaging product series adds industry standard to-263 (D2Pak) packaging products to expand the advantages of supergan platform
onvif协议相关:3.1.1 Digest方式获取Authorization