답안 #968046

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968046 2024-04-23T07:21:03 Z phoenix0423 Real Mountains (CCO23_day1problem2) C++17
0 / 25
1 ms 6492 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 1e6 + 1;
const int N = 1e6 + 3;
const int INF = 1e18;
int h[maxn], l[maxn], r[maxn], cl[maxn], cr[maxn];
int top = 0;

signed main(void){
    fastio;
    int n;
    cin>>n;
    for(int i = 0; i < n; i++) cin>>h[i];
    for(int i = 0; i < n; i++) r[i] = -1;
    stack<int> st;
    for(int i = 0; i < n; i++){
        while(!st.empty() && h[st.top()] <= h[i]){
            r[st.top()] = i;
            st.pop();
        }
        if(st.empty()) l[i] = -1;
        else l[i] = st.top();
        st.push(i);
    }
    // for(int i = n - 1; i >= 0; i--){
    //     // while(r[i] != -1 && h[r[i]] == h[i]) r[i] = r[r[i]];
    // }
    vector<pll> e;
    for(int i = 0; i < n; i++) e.eb(h[i], i);
    sort(e.begin(), e.end());
    int ans = 0;
    for(auto [hei, id] : e){
        // cout<<"cur : "<<id<<" "<<hei<<"\n";
        // cout<<l[id]<<" "<<r[id]<<"\n";
        if(l[id] == -1 || r[id] == -1) continue;
        int dif = min(h[l[id]] - h[id], h[r[id]] - h[id]);
        // cout<<"dif : "<<dif<<"\n";
        // cout<<cl[id]<<" "<<cr[id]<<"\n";
        ans += dif * (h[l[id]] + h[id] + h[r[id]]) + dif * (dif - 1) / 2;
        ans += cl[id] * (dif * (h[l[id]] + h[id] * 2) + dif * (dif + 1) / 2 + dif * (dif - 1) / 2);
        ans += cr[id] * (dif * (h[r[id]] + h[id] * 2) + dif * (dif + 1) / 2 + dif * (dif - 1) / 2);
        if(h[l[id]] < h[r[id]]){
            cr[l[id]] += cl[id] + cr[id] + 1;
        }
        else{
            cl[r[id]] += cl[id] + cr[id] + 1;
        }
        // cout<<"res : "<<ans<<"\n";
    }
    cout<<ans<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -