补充一句,这是c++不是c
by asd890123 @ 2024-04-09 20:43:06
您要不要先学一下单调栈这种数据结构再来打这道题?
by 2023wangzhaolan @ 2024-04-10 22:01:36
@[asd890123](/user/1074084) 看不到题目名称吗?
by 水星湖 @ 2024-04-19 23:32:05
@[asd890123](/user/1074084)
很简单
```cpp
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e6+10;
int n,a[maxn],f[maxn];
stack<int>stk;
int main(){
cin >>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++){
while(!stk.empty()&&a[stk.top()]<a[i]){
f[stk.top()]=i;
stk.pop();
}
stk.push(i);
}
while(!stk.empty()){
f[stk.top()]=0;
stk.pop();
}
for(int i=1;i<=n;i++) printf("%d ",f[i]);
return 0;
}
by aochiao @ 2024-05-04 12:35:22
@[asd890123](/user/1074084) 求关注QwQ
by aochiao @ 2024-05-04 12:36:14
感谢@[aochiao](/user/1258313)
by asd890123 @ 2024-05-04 21:06:55
@[aochiao](/user/1258313)
```
#include <cstdio>
#include <cctype>
#include <stack>
using namespace std;
namespace QuikIO{
int p1,p2;
const int maxn = 1e6;
char in[maxn];
#define getchar() (p1 == p2 && (p1 = 0,p2 = fread(in,1,maxn,stdin)),p1 == p2 ? EOF : in[p1++])
int read(){
int d = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch))
d *= 10,d += ch - '0',ch = getchar();
return d;
}
#undef getchar
}using QuikIO::read;
const int maxn = 3e6 + 10;
stack <int> stk;
int n,a[maxn],f[maxn];
int main(){
n = read();
for (int i = 1;i <= n;i++) a[i] = read();
for (int i = 1;i <= n;i++){
while (!stk.empty() && a[stk.top()] < a[i])
f[stk.top()] = i,stk.pop();
stk.push(i);
}
while(!stk.empty()) f[stk.top()] = 0,stk.pop();
for (int i = 1;i <= n;i++) printf("%d ",f[i]);
return 0;
}
```
帮你优化了一下码风和读入,帮你把时间从405ms降到205ms了,顺便1个月了,我水平有提高,单调栈已经学了
by asd890123 @ 2024-05-04 21:14:23
@[asd890123](/user/1074084) 恭喜!!!
by aochiao @ 2024-05-08 14:51:47
@[asd890123](/user/1074084)
这道题标准做法当然是直接用单调栈,但是蒟蒻总是喜欢没事找事,于是就希望用~~递推~~前缀和来做做看,结果全部TLE,还不如直接暴力枚举。。。不知道怎么回事
by DyingEncoder @ 2024-10-27 19:45:56