60分
#include<iostream>
#include<stack>
using namespace std;
const int N=3000010;
int a[N],f[N];
stack<int> s;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=n;i>=1;i--)
{
while(!s.empty()&&a[s.top()]<=a[i]) s.pop();
if(s.empty()) f[i]=0;
else f[i]=s.top();
s.push(i);
}
for(int i=1;i<=n;i++) cout<<f[i]<<' ';
return 0;
}
100分
#include<iostream>
#include<stack>
using namespace std;
const int N=3000010;
int a[N],f[N];
stack<int> s;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=n;i>=1;i--)
{
while(!s.empty()&&a[s.top()]<=a[i]) s.pop();
if(s.empty()) f[i]=0;
else f[i]=s.top();
s.push(i);
}
for(int i=1;i<=n;i++) printf("%d ",f[i]);
return 0;
}
由此可见
用scanf()是一个好习惯……