题解:P12288 [蓝桥杯 2024 国 Java A] 栈
由于需要支持在添加已有数字时将原有的该数字删除(可以视为将栈中某个元素移到栈顶,即先删去该元素,再将其插入至栈顶),但数据范围不大(
同时,我们可以维护一个
据此,我们可以在
Code:
import java.util.Scanner;
public class Main {
static final int M = 500005, V = 1000005; // 查询数, 值域
public static class dataList { // 使用双向链表实现的数据列表 (用于维护题目所述的栈)
int[] q = new int[M], pre = new int[M], nxt = new int[M], idx = new int[V];
// 存放的值, 每个位置的前驱, 每个位置的后继, 每个值对应的下标 (不存在则为 -1)
int head, tail, tot, ans;
// 开头下标, 结尾下标, 当前用到的下标的最大值, 当前相邻的数中和为奇数的组数
public dataList() { // 构造函数
for(int i = 0; i < M; i++) {
this.q[i] = this.pre[i] = this.nxt[i] = 0;
}
for(int i = 0; i < V; i++) {
this.idx[i] = -1;
}
this.head = this.tail = this.tot = this.ans = 0;
}
public void insert(int x) { // 将 x 插入到首部
this.q[++this.tot] = x;
this.idx[x] = this.tot;
this.pre[this.tot] = 0;
this.nxt[this.tot] = this.head;
if(this.head != 0) {
if((this.q[this.head] + this.q[this.tot]) % 2 == 1) { // 维护答案
this.ans++;
}
this.pre[this.head] = this.tot;
}
this.head = this.tot;
if(this.tail == 0) {
this.tail = this.tot;
}
}
public void erase(int x) { // 删除值为 x 的元素
if(this.idx[x] == -1) {
return;
}
int i = this.idx[x];
this.idx[x] = -1;
if(this.pre[i] != 0) {
if((this.q[this.pre[i]] + this.q[i]) % 2 == 1) { // 维护答案
this.ans--;
}
this.nxt[this.pre[i]] = this.nxt[i];
} else {
this.head = this.nxt[i];
}
if(this.nxt[i] != 0) {
if((this.q[this.nxt[i]] + this.q[i]) % 2 == 1) { // 维护答案
this.ans--;
}
this.pre[this.nxt[i]] = this.pre[i];
} else {
this.tail = this.pre[i];
}
if(this.pre[i] != 0 && this.nxt[i] != 0) {
if((this.q[this.pre[i]] + this.q[this.nxt[i]]) % 2 == 1) { // 维护答案
this.ans++;
}
}
}
/*
public void print() { // 打印队列, 调试用
for(int i = this.head; i != 0; i = this.nxt[i]) {
System.out.print(this.q[i] + " ");
}
System.out.println();
}
*/
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
dataList Q = new dataList();
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
int x = sc.nextInt();
Q.erase(x); // erase 函数中会判断当前 x 是否在 dataList 中, 故此处无需判断
Q.insert(x);
System.out.println(Q.ans);
}
sc.close();
}
}