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;
}
相关推荐
亲爱的非洲野猪14 分钟前
动态规划进阶:状态机DP深度解析
算法·动态规划
dragoooon3426 分钟前
[hot100 NO.91~95]
算法
windows_633 分钟前
【无标题】
算法
jojo_zjx44 分钟前
GESP 24年12月2级 数位和
c++
自由的好好干活1 小时前
PCI9x5x驱动移植支持PCI9054在win7下使用3
c++·驱动开发
踢足球09291 小时前
寒假打卡:2026-01-24
数据结构·算法
亲爱的非洲野猪1 小时前
动态规划进阶:多维DP深度解析
算法·动态规划
AlenTech1 小时前
197. 上升的温度 - 力扣(LeetCode)
算法·leetcode·职场和发展
橘颂TA2 小时前
【Linux 网络】TCP 拥塞控制与异常处理:从原理到实践的深度剖析
linux·运维·网络·tcp/ip·算法·职场和发展·结构与算法
tobias.b2 小时前
408真题解析-2010-9-数据结构-折半查找的比较次数
java·数据结构·算法·计算机考研·408真题解析