作业介绍
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, k;
int a[N];
int stk[N], top;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1, step = 1 ; step <= n; step++ ) {
// i 指向当前要入栈的位置
int minn = 2e9, mini = -1;
if (top)
minn = stk[top];
// 检查后面有没有更小的元素
// 能检查的最远的位置
if (top < k) {
int maxx = min(n, i + (k - top) - 1);
for (int j = i; j <= maxx; j++) {
if (a[j] < minn) {
minn = a[j];
mini = j;
}
}
}
if (mini == -1) // 最小值是栈顶,直接输出
cout << stk[top--] << " ";
else {
while (i <= mini)
stk[++top] = a[i++];
cout << stk[top--] << " ";
}
}
return 0;
}
// 链表
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int q, a[N];
int e[N], nxt[N], idx, head, tail;
int id[N]; // 记录每个组别的最后一个元素的位置
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> a[i];
cin >> q;
while (q--) {
string s;
int x;
cin >> s;
if (s == "push") {
cin >> x;
// 先看元素里面是否有组别,有插入在组别最后面
// 没有插入在链表末尾
if (id[a[x]]) { // 插入在 id[a[x]] 下一个位置
idx++;
e[idx] = x;
nxt[idx] = nxt[id[a[x]]];
nxt[id[a[x]]] = idx;
if (tail == id[a[x]])
tail = idx;
id[a[x]] = idx;
} else {
idx++;
e[idx] = x;
// 末尾是 tail
nxt[tail] = idx;
tail = idx;
id[a[x]] = idx;
}
} else {
int cur = nxt[head];
if (cur == tail)
tail = 0;
cout << e[cur] << endl;
// 判断这个组别是否变空了
int g = a[e[cur]]; // 得到组别
if (id[g] == cur)
id[g] = 0;
nxt[head] = nxt[cur];
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, k;
int q[N], h, t = -1;
int a[N];
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
// 求最小值
for (int i = 1; i <= n; i++) {
// 新进来的元素比之前的元素都小,那么之前的就不可能是最小值
// 直接弹出
while (h <= t && a[q[t]] > a[i])
t--;
// 长度超了 , i - q[h]+1 队列长度
if ( h <= t && i - q[h] + 1 > k)
h++;
q[++t] = i;
if (i >= k)
cout << a[q[h]] << " ";
}
puts("");
// 求最大值
h = 0, t = -1; // 清空队列
for (int i = 1; i <= n; i++) {
while (h <= t && a[q[t]] < a[i])
t--;
if ( h <= t && i - q[h] + 1 > k)
h++;
q[++t] = i;
if (i >= k)
cout << a[q[h]] << " ";
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int n, m;
int l[N], r[N];
void init() {
// 1~n 初始双向链表
for (int i = 1; i <= n; ++i) {
l[i] = i - 1;
r[i] = i + 1;
}
r[0] = 1; // 头哨兵0后继是1
l[n + 1] = n; // 尾哨兵n+1前驱是n
}
// 删除数值x对应的节点
void del(int x) {
int L = l[x], R = r[x];
r[L] = R;
l[R] = L;
}
// 将数值x的节点,插入到位置pos的左侧
void insert(int x, int pos) {
int L = l[pos];
r[L] = x;
l[x] = L;
r[x] = pos;
l[pos] = x;
}
int main() {
scanf("%d%d", &n, &m);
init();
while (m--) {
int opt, x, y;
scanf("%d%d", &opt, &x);
if (opt == 1 || opt == 2) {
scanf("%d", &y);
if (x == y)
continue;
del(x);
if (opt == 1) {
// x移到y左边:插入y左侧
insert(x, y);
} else {
// x移到y右边:插入y后继的左侧
insert(x, r[y]);
}
} else {
// opt==3 删除x
del(x);
}
}
if (r[0] == n + 1) {
puts("Empty!");
} else {
for (int i = r[0]; i != n + 1; i = r[i]) {
printf("%d ", i);
}
putchar('\n');
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int q;
int e[N], nxt[N], idx;
int id[N]; // id[x] = k , 元素 x 所处的位置是 k
int opt, x, y;
void add(int x, int y) {
idx++;
e[idx] = y;
x = id[x]; // 获取到 x 所处的位置
nxt[idx] = nxt[x];
nxt[x] = idx;
id[y] = idx; // 记录下来 y 所处的位置
}
// 最开始的时候有数字1
void init() {
idx++;
e[idx] = 1;
id[1] = 1;
nxt[idx] = -1;
}
int main() {
cin >> q;
while (q--) {
cin >> opt >> x;
if (opt == 1) {
cin >> y;
add(x, y);
} else if (opt == 2) {
int t = nxt[id[x]];
if (t == -1) // x 是最后一个元素
cout << 0 << endl;
else
cout << e[t] << endl;
} else {
nxt[id[x]] = nxt[nxt[id[x]]];
}
}
return 0;
}
题目
认领作业后才可以查看作业内容。
- 状态
- 正在进行…
- 题目
- 20
- 开始时间
- 2026-6-25 0:00
- 截止时间
- 2026-8-31 23:59
- 可延期
- 24 小时