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;
}
相关推荐
凡人叶枫11 小时前
Effective C++ 条款07:为多态基类声明 virtual 析构函数
linux·c语言·开发语言·c++
Black蜡笔小新11 小时前
自动化AI算法训练服务器DLTM训推一体工作站赋能多行业智能化升级
人工智能·算法·自动化
凡人叶枫11 小时前
Effective C++ 条款10:令 operator= 返回一个 reference to *this
java·linux·服务器·开发语言·c++·effective c++
王老师青少年编程11 小时前
2026年全国青少年信息素养大赛算法应用主题赛(C++赛项-复赛模拟卷6:文末附答案)
c++·答案·模拟卷·复赛·2026年·青少年信息素养大赛·算法应用主题赛
怪兽学LLM11 小时前
LeetCode 438 找到字符串中所有字母异位词(Python 固定滑动窗口+字符计数解法)
python·算法·leetcode
满怀冰雪11 小时前
第04篇-双指针算法-从有序数组到回文判断的高频解法
java·算法
CC数学建模11 小时前
2026年江西省研究生数学建模竞赛1题:空间数据分析中的过拟合识别完整思路、代码、模型、文章,全网首发高质量分享!
python·算法·数学建模
leo__52012 小时前
MATLAB实现牧羊人算法
开发语言·算法·matlab
视觉小萌新12 小时前
C++利用libmicrohttpd制作交互网页端——C1
java·c++·交互
Gauss松鼠会12 小时前
【GaussDB】GaussDB SMP特性调优详解
java·服务器·前端·数据库·sql·算法·gaussdb