“&&”对于判断条件的位置也有要求?
查看原帖
“&&”对于判断条件的位置也有要求?
524782
2_6HogCycle楼主2022/5/11 20:55
#include<iostream>
#include<stack>
using namespace std;
int a[3000001],n,p[3000001];
stack<int>s;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=n;i>0;i--){
		while(!s.empty()&&a[s.top()]<=a[i]) s.pop();					
		if(!s.empty())p[i]=s.top();
		else p[i]=0;
		s.push(i);
	}
	for(int i=1;i<=n;i++)cout<<p[i]<<" ";
	return 0;
}

这是AC代码,本地运行成功

可是可是,当我将!s.empty()与a[s.top()]<=a[i]换个位置时

#include<iostream>
#include<stack>
using namespace std;
int a[3000001],n,p[3000001];
stack<int>s;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=n;i>0;i--){
		while(a[s.top()]<=a[i]&&!s.empty()) s.pop();					
		if(!s.empty())p[i]=s.top();
		else p[i]=0;
		s.push(i);
	}
	for(int i=1;i<=n;i++)cout<<p[i]<<" ";
	return 0;
}

本地运行不出来,exe文件停止运行,luogu上全RE

请教一下dalao,这是怎么回事啊

2022/5/11 20:55
加载中...