From 5b8eff62bcd5b6ec10c3a3bbbaab081176f47a67 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sun, 18 Aug 2024 13:06:07 +0800 Subject: [PATCH] update --- day14/P5788/P5788.cpp | 17 ++++------------- day14/P5788/P5788.json | 3 ++- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/day14/P5788/P5788.cpp b/day14/P5788/P5788.cpp index 6282989..7a673c6 100644 --- a/day14/P5788/P5788.cpp +++ b/day14/P5788/P5788.cpp @@ -15,19 +15,10 @@ int main(){ for(int i=1;i<=n;i++){ a[i]=readInt(); } - ans[n]=0; - s.push(n); - for(int i=n-1;i>=1;i--){ - if (a[i]>=a[s.top()]) { - ans[i]=0; - s.push(i); - }else{ - ans[i]=s.top(); - while (s.size()>0&&a[s.top()]>a[i]) { - s.pop(); - } - s.push(i); - } + for(int i=n;i>=1;i--){ + while(!s.empty()&&a[s.top()]<=a[i])s.pop(); + ans[i]=s.empty()?0:s.top(); + s.push(i); } for(int i=1;i<=n;i++){ cout<