专栏文章

7-26作业/重写ren_gao_zu

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@miopfbr3
此快照首次捕获于
2025/12/02 22:59
3 个月前
此快照最后确认于
2025/12/02 22:59
3 个月前
查看原文

作业

U514895 ١١(❛ᴗ❛)【单调栈】-左侧第一个较小值(不重复)

CPP
#include<bits/stdc++.h>
#define int  long long
using namespace std;
const int N =1e6+5;
int n;
int a[N],b[N];
stack<int> st;
int to=0;
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=0;i<=1e6;i++)
		b[i]=-1;
	for(int i=1;i<=n;i++)
	{
		while(st.size()&&a[i]<=a[st.top()])
		{
			st.pop();
		}
		if(st.size())
		{
			b[i]=st.top();
			//if(b[i]!=-1)
			
		}
		st.push(i);
	}
	for(int i=2;i<=n;i++)
		to^=b[i];
	if(n>1000)
	{
		cout<<to;
		return 0;
	}
	for(int i=1;i<=n;i++)
		cout<<b[i]<<" ";
	return 0;
}

U514949 ١١(❛ᴗ❛)【单调栈】-左右两侧第一小于值(存在重复情况)

CPP
#include<bits/stdc++.h>
#define int  long long
using namespace std;
const int N = 1e6 + 5;
int n;
int a[N],b1[N],b2[N];
stack<int>st1,st2;
int to=0;
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=0;i<=1e6;i++)
		b1[i]=-1,b2[i]=-1;
	for(int i=n;i>=1;i--)
	{
		while(st2.size()&&a[i]<=a[st2.top()])
		{
			st2.pop();
		}
		if(st2.size())
		{
			b2[i]=st2.top();
		}
		st2.push(i);
	}
	for(int i=1;i<=n;i++)
	{
		while(st1.size()&&a[i]<=a[st1.top()])
		{
			st1.pop();
		}
		if(st1.size())
		{
			b1[i]=st1.top();

		}
		st1.push(i);
	}

	for(int i=1;i<=n;i++)
		cout<<b1[i]<<" "<<b2[i]<<endl;
	return 0;
}

P1901 发射站

CPP
#include<bits/stdc++.h>
#define int  long long
using namespace std;
const int N = 1e6 + 5;
int n;
int a[N],b1[N],b2[N],v[N];
stack<int>st1,st2;
int to=0,tong[N];
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i]>>v[i];
	for(int i=0;i<=1e6;i++)
		b1[i]=-1,b2[i]=-1;
	for(int i=n;i>=1;i--)
	{
		while(st2.size()&&a[i]>=a[st2.top()])
		{
			st2.pop();
		}
		if(st2.size())
		{
			b2[i]=st2.top();
		}
		st2.push(i);
	}
	for(int i=1;i<=n;i++)
	{
		while(st1.size()&&a[i]>=a[st1.top()])
		{
			st1.pop();
		}
		if(st1.size())
		{
			b1[i]=st1.top();

		}
		st1.push(i);
	}

	for(int i=1;i<=n;i++)
	{
		if(b1[i]!=-1)
			tong[b1[i]]+=v[i];
		if(b2[i]!=-1)
			tong[b2[i]]+=v[i];
	}
	int maxn=0;
	for(int i=1;i<=n;i++)
	{
		maxn=max(maxn,tong[i]);
	}
	cout<<maxn;
		
	return 0;
}

重写

评论

0 条评论,欢迎与作者交流。

正在加载评论...