2345安全卫士
(蛋小黄(蒟蒻))
1
#include <bits/stdc++.h>
using namespace std;
int a[3000005],f[3000005];
stack<int> st;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=n;i>=1;i--){
while(a[i]>a[st.top()]&&!st.empty()){
st.pop();
}
if(!st.empty()){
f[i]=st.top();
}
st.push(i);
}
for(int i=1;i<=n;i++){
cout<<f[i]<<" ";
}
}
送积分。题面:
1 个赞
彭英哲
(pyz51)
2
把这个改成 !st.empty()&&a[i]>a[st.top()]
1 个赞
彭英哲
(pyz51)
6
因为如果栈是空的,那么这里会先执行a[i]>a[st.top()],st.top()没有返回值
3 个赞
2345安全卫士
(蛋小黄(蒟蒻))
7
// 改了WA10
#include <bits/stdc++.h>
using namespace std;
int a[3000005],f[3000005];
stack<int> st;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=n;i>=1;i--){
while(!st.empty()&&a[i]>a[st.top()]){
st.pop();
}
if(!st.empty()){
f[i]=st.top();
}
st.push(i);
}
for(int i=1;i<=n;i++){
cout<<f[i]<<" ";
}
}
1 个赞
2345安全卫士
(蛋小黄(蒟蒻))
18
我不需要把我的代码变成第二个你的代码,我需要将他变成第一个我的代码,此贴结
1 个赞
彭英哲
(pyz51)
19
是调代码而不是AC,如果这样的话,老师为什么还要给我们讲题而不是直接把代码贴给我们
1 个赞
杨思越
(灵珠敖丙)
20
但问题是我的建议有用啊?
楼主不采纳应该不算我的事吧……
1 个赞