AcWing 134. 【java】双端队列
原题链接
困难
作者:
tt2767
,
2019-12-08 23:36:03
,
所有人可见
,
阅读 787
// 理解了视频里的意思,不过自己还是无法独立写出来,一知半解中。。。。
import java.util.*;
public class Main{
public class Pair{
int x, y;
public Pair(int x, int y){
this.x=x;
this.y=y;
}
}
void run(){
int n = jin.nextInt();
for (int i = 0 ; i < n ; i++){
list.add(new Pair(jin.nextInt(), i));
}
list.sort((a,b)->Integer.compare(a.x, b.x));
int res = 1;
int last = n+1;
int dir = -1;
int i = 0;
while (i < n){
int j = i;
while (j < n && list.get(j).x == list.get(i).x) j++;
int minPos = list.get(i).y;
int maxPos = list.get(j-1).y;
if (dir == -1){
if (last > maxPos){
last = minPos;
} else {
last = maxPos;
dir = 1;
}
} else {
// if (last > maxPos){
if (last < minPos){
last = maxPos;
} else {
last = minPos;
dir = -1;
res += 1;
}
}
i = j;
}
System.out.println(res);
}
List<Pair> list = new ArrayList<>();
private Scanner jin = new Scanner(System.in);
public static void main(String[] args) throws Exception {new Main().run();}
}