noi-2025年12月23号作业

**题目:**P1739 表达式括号匹配

网址: https://www.luogu.com.cn/problem/P1739

**思路:**如果遇到(,就cnt++,代表(的个数多了一个。如果遇到),这个时候就需要拿出来一个(进行匹配,如果没有的话,就一定是NO,否则就cnt--,代表(的个数少了一个。

**知识点:**栈的思想

代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;


int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	string str;
	cin>>str;
	int cnt=0;
	for(auto it:str)
	{
	  if(it=='(')
	  cnt++;
	  else if(it==')'){
	  	if(cnt==0)
	  	{
	  	  cout<<"NO"<<'\n';
		  return 0;	
		}
		cnt--;
	  }	
	} 
	if(cnt!=0)
	{
		cout<<"NO"<<'\n';
	}else cout<<"YES"<<'\n';
	return 0;
}

**题目:**P14325 [JOI2022 预选赛 R2] 图书馆 2 / Library 2

网址: https://www.luogu.com.cn/problem/P14325

**思路:**我们定义一个stack,如果是READ操作,就输出最上面的字符串并pop,否则就把输入push到stack的最上面

**知识点:**stack的定义和运用

代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;


int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int q;
	cin>>q;
	stack<string>st;
	while(q--)
	{
		string op;
		cin>>op;
		if(op=="READ")
		{
			cout<<st.top()<<'\n';
			st.pop();
		}else{
			st.push(op);
		}
	}
	return 0;
}

**题目:**B3614 【模板】栈

网址: https://www.luogu.com.cn/problem/solution/B3614

**思路:**因为0≤x<2^64,所以使用unsigned long long,我们定义一个数组a来模拟栈,cnt代表当前有多少个元素。

**知识点:**使用一个数组来模拟栈

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int maxn=1e6+100;
ull a[maxn],cnt;
void solve()
{
	int n;
	cin>>n;
	cnt=0;
	while(n--)
	{
		string op;
		cin>>op;
		if(op=="push")
		{
			ull x;
			cin>>x;
			a[++cnt]=x;
		}else if(op=="pop")
		{
			if(cnt==0)
			cout<<"Empty"<<'\n';
			else cnt--;
		}else if(op=="query")
		{
			if(cnt==0)
			cout<<"Anguei!"<<'\n';
			else cout<<a[cnt]<<'\n';
		}else if(op=="size")
		{
			cout<<cnt<<'\n';
		}
	}
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    solve();
	return 0;
}

**题目:**B3758 [信息与未来 2021] 括号序列

网址: https://www.luogu.com.cn/problem/B3758

**思路:**我们首先使用vis数组来对可以匹配的位置进行标记,那么标记完之后,剩下没标记的地方就需要我们进行添加。

**知识点:**简单思维,栈的运用

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int maxn=1e6+100;
int vis[maxn];
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	string str;
	cin>>str;
	stack<int>st;
	for(int i=0;i<str.length();i++)
	{
		if(str[i]=='(')
		st.push(i);
		else{
			if(st.size()!=0)
			{
				vis[i]=1;
				vis[st.top()]=1;
				st.pop();
			}
			
		}
	}
	string ans="";
	for(int i=0;i<str.length();i++)
	{
		if(vis[i])
		ans+=str[i];
		else{
			if(str[i]==')')
			{
				ans+='(';
				ans+=str[i];
			}else{
				ans+=str[i];
				ans+=')';
			}
		}
	}
	cout<<ans;
	return 0;
}

**题目:**P1165 日志分析

网址: https://www.luogu.com.cn/problem/P1165

**思路:**我们使用一个stack来存储到目前为止的最大值,如果当前这个操作要入库,那么当前的最大值就是max(以前的最大值,当前的输入x)。

**知识点:**单调栈

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
const int maxn=1e6+100;

int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	stack<ll>st;
	for(int i=1;i<=n;i++)
	{
		int op;
		cin>>op;
		if(op==0)
		{
		   ll x;
		   cin>>x;
		   ll tp=0;
		   if(st.size())
		   tp=st.top();
		   st.push(max(tp,x));
		}else if(op==1)
		{
			if(st.size())
			st.pop();
		}else{
			if(st.size())
			cout<<st.top()<<'\n';
			else cout<<0<<'\n';
		}
	}
	return 0;
}

**题目:**P4387 【深基15.习9】验证栈序列

网址: https://www.luogu.com.cn/problem/P4387

**思路:**我们模拟一遍就行。

**知识点:**简单思维,栈的运用

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
const int maxn=2e5+100;

int n;
ll a[maxn],b[maxn];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	for(int i=1;i<=n;i++)
	cin>>b[i];
	stack<int>st;
	int k=1;
	for(int i=1;i<=n;i++)
	{
		st.push(a[i]);
		while(k<=n&&st.size()&&b[k]==st.top())
		{
			st.pop();
			k++;
		}
	}
	if(st.size())
	cout<<"No"<<'\n';
	else cout<<"Yes"<<'\n';
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int T;
	cin>>T;
	while(T--)
	solve();
	return 0;
}

**题目:**P7870 「Wdoi-4」兔已着陆

网址: https://www.luogu.com.cn/problem/P7870

**思路:**我们使用一个stack来存储起始位置和结束位置,对于k,我们一个个对栈的顶部元素进行判断。

**知识点:**简单思维,栈的运用

代码:

cpp 复制代码
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
using namespace std;
const int maxn=2e5+100;


int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	stack<pll>st;
	while(n--)
	{
		int op;
		cin>>op;
		if(op==1)
		{
			ll l,r;
			cin>>l>>r;
			st.push({l,r});
		}else{
			ll k;
			cin>>k;
			ll res=0;
			while(k)
			{
			   pll p=st.top();
			   st.pop();
			   ll l=p.fi,r=p.se;	
			   ll len=r-l+1;
			   
			   if(k>=len)
			   {
			   	 res+=(l+r)*(r-l+1)/2;
			   	 k-=len;
			   }else{
			   	 ll r2=r-k;
			   	 st.push({l,r2});
			   	 res+=(r2+1+r)*(k)/2;
			   	 k=0;
			   }
			} 
			cout<<res<<'\n';
		}
	}
	return 0;
}
相关推荐
爱理财的程序媛1 天前
openclaw 盯盘实践
算法
端平入洛1 天前
auto有时不auto
c++
MobotStone1 天前
Google发布Nano Banana 2:更快更便宜,图片生成能力全面升级
算法
颜酱1 天前
队列练习系列:从基础到进阶的完整实现
javascript·后端·算法
用户5757303346241 天前
两数之和:从 JSON 对象到 Map,大厂面试官到底在考察什么?
算法
程序猿追1 天前
“马”上行动:手把手教你基于灵珠平台打造春节“全能数字管家”
算法
ZPC82102 天前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC82102 天前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
琢磨先生David2 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode