#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long n, arr[N], nxt[N], upd[N], cur, ans;
stack<int> stck;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n;
for(int i = 1; i <= n; i++) cin >> arr[i];
arr[n + 1] = -1;
stck.push(n + 1);
for(int i = n; i >= 1; i--){
while(arr[stck.top()] >= arr[i]) stck.pop();
nxt[i] = stck.top();
stck.push(i);
}
for(int i = 1; i <= n; i++){
cur += upd[i];
if(cur == arr[i]) continue;
else{
upd[nxt[i]] -= arr[i] - cur;
ans++;
cur += arr[i] - cur;
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
1620 KB |
Output is correct |
5 |
Correct |
7 ms |
2132 KB |
Output is correct |
6 |
Correct |
11 ms |
2484 KB |
Output is correct |
7 |
Correct |
11 ms |
3028 KB |
Output is correct |