noi-2026年3月17号作业

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

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

**思路:**我们使用一个一维的数组来模拟栈,栈只有从顶部才能加入数据

**知识点:**栈

代码:

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;
}

**题目:**B3616 【模板】队列

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

**思路:**我们使用一个一维的数组来模拟队列,L代表队首,R代表队尾。

知识点:队列

代码:

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

ll n,m,a[maxn];
ll ans,sum;
int L=1000000,R=1000000-1;
void solve()
{
    int q;
    cin>>q;
    while(q--)
    {
    	int op;
    	cin>>op;
    	if(op==1)
    	{
    		ll x;
    		cin>>x;
    		a[++R]=x;
		}else if(op==2)
		{
			if(L>R)
			cout<<"ERR_CANNOT_POP"<<'\n';
			else L++;
		}else if(op==3)
		{
			if(L>R)
			cout<<"ERR_CANNOT_QUERY"<<'\n';
			else cout<<a[L]<<'\n';
		}else if(op==4)
		{
			cout<<R-L+1<<'\n';
		}
	}
}
int main(){
	
    ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	_=1;
//	cin>>_;
	while(_--)
    solve();
	return 0;
}

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

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

思路:

**知识点:**栈

代码:

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

ll n,m,a[maxn];
ll ans,sum;
string str;
void solve()
{
    cin>>str;
    int len=str.length();
    int cnt=0;
    for(int i=0;i<len;i++)
    {
    	if(str[i]=='(')
    	cnt++;
    	else if(str[i]==')')
    	cnt--;
    	if(cnt<0)
    	{
    		cout<<"NO"<<'\n';
    		return;
		}
	}
	if(cnt!=0)
	{
		cout<<"NO"<<'\n';
		return;
	}
	cout<<"YES"<<'\n';
}
int main(){
	
    ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	_=1;
//	cin>>_;
	while(_--)
    solve();
	return 0;
}

**题目:**P1449 后缀表达式

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

**思路:**我们按照题目的意思去模拟就行

**知识点:**后缀表达式,模拟

代码:

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
#define db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn];
ll n,m;

void solve()
{
   cin>>str;
   int len=str.length();
   stack<ll>st;
   int j;
   for(int i=0;i<len;i=j)
   {
   	  j=i+1;
   	  if(str[i]=='@')
   	  break;
	  if(str[i]=='/')
	  {
	     ll v1=st.top();
		 st.pop();
		 ll v2=st.top();
		 st.pop();
		 st.push(v2/v1);	
	  }else if(str[i]=='+')
	  {
	  	 ll v1=st.top();
		 st.pop();
		 ll v2=st.top();
		 st.pop();
		 st.push(v2+v1);
	  }else if(str[i]=='-')
	  {
	  	 ll v1=st.top();
		 st.pop();
		 ll v2=st.top();
		 st.pop();
		 st.push(v2-v1);
	  }else if(str[i]=='*')
	  {
	  	 ll v1=st.top();
		 st.pop();
		 ll v2=st.top();
		 st.pop();
		 st.push(v2*v1);
	  }else{
	  	ll v=str[i]-'0';
	  	while(j<len&&str[j]>='0'&&str[j]<='9')
	  	{
	  	   v=v*10+(str[j]-'0');
		   j++;	
		}
		j++; 
		st.push(v);
	  }
   } 
   cout<<st.top(); 
	
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

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

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

**思路:**我们按照输入的顺序一直入栈,入栈的同时判断是否可以出栈,如果最后还有元素的话,这么说明一定是NO

**知识点:**模拟

代码:

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
#define db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn];
ll n,m;
ll 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<ll>st;
   int k=1;
   for(int i=1;i<=n;i++)
   {
   	  st.push(a[i]);
   	  while(k<=n&&st.size()&&st.top()==b[k])
   	  {
   	     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=1;
	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

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

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

**思路:**我们每次把还存在的货物的最大值放进去

**知识点:**模拟,思维

代码:

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
#define db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn];
ll n,m;
void solve()
{
   cin>>n;
   ll mx=0;
   stack<ll>st;
   while(n--)
   {
     int op;
	 cin>>op;
	 if(op==0)
	 {
	   	ll x;
		cin>>x;
		if(st.size())
		st.push(max(x,st.top()));
		else st.push(x);
	 }else if(op==1)
	 {
	    if(st.size())
		st.pop();	
	 }else if(op==2)
	 {
	    if(st.size())
		cout<<st.top()<<'\n';
		else cout<<0<<'\n'; 
	 } 
   } 
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

**题目:**P1886 【模板】单调队列 / 滑动窗口

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

**思路:**单调队列模板题

**知识点:**单调队列

代码:

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
#define db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=1e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn];
ll n,m,k;
struct node_mx{
	ll val,pos;
	bool operator <(const node_mx& nd)const
	{
		return val<nd.val;
	}
};
struct node_mn{
	ll val,pos;
	bool operator <(const node_mn& nd)const
	{
		return val>nd.val;
	}
};
void solve()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
    	cin>>a[i];
	}
	priority_queue<node_mx>qmx;
	priority_queue<node_mn>qmn;
	vector<ll>ansmn,ansmx;
	for(int i=1;i<=n;i++)
	{
		if(i<k)
		{
			qmx.push({a[i],i});
			qmn.push({a[i],i});
			continue;
		}
		qmx.push({a[i],i});
		qmn.push({a[i],i});
		while(qmx.size()&&qmx.top().pos<=i-k)
		qmx.pop();
		while(qmn.size()&&qmn.top().pos<=i-k)
		qmn.pop();
		ansmn.pb(qmn.top().val);
		ansmx.pb(qmx.top().val);
	}
	for(auto it:ansmn)
	cout<<it<<" ";
	cout<<'\n';
	for(auto it:ansmx)
	cout<<it<<" ";
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

**题目:**P5788 【模板】单调栈

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

**思路:**单调栈模板题

**知识点:**单调栈

代码:

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
#define db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=3e6+100;
const ll mode2=1e9+7; 

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

**题目:**P1440 求m区间内的最小值

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

**思路:**单调栈模板题

**知识点:**单调栈

代码:

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
#define db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=2e6+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn];
ll n,m; 
struct node{
	ll val,pos;
	bool operator<(const node&nd)const
	{
		return val>nd.val;
	}
};
void solve()
{
    cin>>n>>m;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	
	priority_queue<node>q;
	for(int i=1;i<=n;i++)
	{
	   if(q.size()==0)
	   {
	     cout<<0<<'\n';
		 q.push({a[i],i}); 
		 continue;	
	   }	
	   while(q.size()&&q.top().pos<i-m)
	   q.pop();
	   cout<<q.top().val<<'\n'; 
	   q.push({a[i],i});
	} 
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}

**题目:**P1311 [NOIP 2011 提高组] 选择客栈

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

**思路:**我们对每一种格调,记录一下有多少个店它右边有店的最低消费是小于等于p的。

**知识点:**思维题

代码:

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
#define db double 
#define eb emplace_back
#define pb push_back
using namespace std;
const int maxn=2e5+100;
const ll mode2=1e9+7; 

int vis[maxn];
string str; 
ll a[maxn],b[maxn],cnt[100]; 
ll n,m,k,p;
void solve()
{
    cin>>n>>k>>p;
    ll ans=0,last=0;
	for(int i=1;i<=n;i++)
	{
	   cin>>a[i]>>b[i];	
	} 
	for(int i=1;i<=n;i++)
	{
		if(b[i]<=p)
		{
			for(int j=last+1;j<=i;j++)
			cnt[a[j]]++;
			last=i;
			ans+=cnt[a[i]]-1;
		}else{
			ans+=cnt[a[i]];
		}
	}
	cout<<ans;
	
}
int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T=1;
//	cin>>T;
	while(T--)
	solve();  
	return 0; 
}
相关推荐
也曾看到过繁星2 小时前
初识c++
开发语言·c++
2401_874732532 小时前
泛型编程与STL设计思想
开发语言·c++·算法
季远迩2 小时前
54.螺旋矩阵(中等)
算法
今儿敲了吗2 小时前
44| 汉诺塔问题
数据结构·c++·笔记·学习·算法·深度优先
yuyuzururu2 小时前
进程通信实验报告
c++
米粒12 小时前
力扣算法刷题 Day 15
算法·leetcode·职场和发展
程序员小崔日记2 小时前
一道基础计算题卡在 40 分,求助判题规则问题
java·算法·竞赛
愣头不青2 小时前
543.二叉树的直径
java·算法
此方ls2 小时前
机器学习聚类算法二——DBSCAN(Density-Based Spatial Clustering of Applications with Noise)
算法·机器学习·聚类