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;
}
相关推荐
青岛少儿编程-王老师2 小时前
CCF编程能力等级认证GESP—C++8级—20251227
java·开发语言·c++
surtr12 小时前
【算法自用】一些比较有趣的题目
算法·动态规划·概率论·图论
じ☆冷颜〃2 小时前
二分查找的推广及其在排序与链表结构中的关联
网络·windows·经验分享·笔记·算法·链表
白日做梦Q2 小时前
图像去噪算法对比:传统方法与深度学习方法
人工智能·深度学习·算法
GEO AI搜索优化助手2 小时前
数据、AI与人的新协同——构建GEO时代的智能营销引擎
人工智能·算法·搜索引擎·生成式引擎优化·geo搜索优化
夏乌_Wx2 小时前
练题100天——DAY35:棒球比赛+数组的度
数据结构
hd51cc2 小时前
MFC打印技术
c++·mfc
Felven2 小时前
B. The Secret Number
算法
youngee112 小时前
hot100-63买卖股票的最佳时机
数据结构·算法·leetcode