跳到正文

个人记录

7.28课堂总结

chengcheng_2015约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月20日
篇幅
约 2 分钟

版本

  1. v1当前

队列:

C++
//定义队列
int q[10010], head = 1, tail = 1;

queue<int>q;
//向队列中加入一个数x
q[tail] = x;
tail++;

q.push(x);
//输出队首元素
cout << q[head] << '\n';

cout << q.front() << '\n';
//将队首弹出
head++;

q.pop();
//输出此时队列元素个数
cout << tail - head << '\n';

cout << q.size() << '\n';

栈:

C++
//定义栈
int st[10010], top = 0;

stack<int>st;
//向栈中加入一个数x
top++;
st[top] = x

st.push(x);
//输出栈顶元素
cout << st[top] << '\n';

cout << st.top() << '\n';
//将栈顶弹出
top--;

st.pop();
//输出此时栈内元素个数
cout << top << '\n';

cout << st.size() << '\n';

课堂总结:

P7505 「Wdsr-2.5」小小的埴轮兵团

错误原因:

用时太长了

考察知识点:

双端队列

正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
int q[300010], a[300010], head = 1, tail = 1;
int main(){
	int n, m, k;
	cin >> n >> m >> k;
	for(int i = 1;i <= n;i++)cin >> a[i];
	sort(a+1,a+n+1);
	for(int i = 1;i <= n;i++){
		q[tail] = a[i];
		tail++;
	}
	long long sum = 0;
	while(m--){
		int op;
		cin >> op;
		if(op == 1){
			int x;
			cin >> x;
			sum += x;
			while(tail>head){
				if(q[tail-1]+sum>k)tail--;
				else break;
			}
		} else if(op == 2){
			int x;
			cin >> x;
			sum -= x;
			while(head<tail){
				if(q[head]+sum<-k)head++;
				else break;
			}
		} else {
			cout << tail - head << '\n';
		}
	}
    return 0;
}
原文