답안 #709747

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709747 2023-03-14T11:22:53 Z Spade1 Fancy Fence (CEOI20_fancyfence) C++14
12 / 100
1 ms 364 KB
#include<bits/stdc++.h>
//#include "grader.h"
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
using namespace std;

const int N = 1e5 + 10;
const int M = 1e9+7;
stack<pll> s;

ll h[N], w[N];
ll add(ll a, ll b) {return (((a+b)%M)+M)%M;}
ll mul(ll a, ll b) {return (a%M*b%M)%M;}

ll power(ll a, ll b) {
    if (b == 0) return 1;
    ll ret = power(a, b/2);
    if (b&1) return mul(mul(ret, ret), a);
    else return mul(ret, ret);
}

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) cin >> h[i];
    for (int i = 1; i <= n; ++i) cin >> w[i];

    ll ans = 0;
    ll inv = power(2, M-2);
    s.push({0, 0});
    for (int i = 1; i <= n; ++i) {
        while (s.top().st > h[i]) {
            int H = s.top().st;
            int W = s.top().nd;
            s.pop();
            if (h[i] > s.top().st) {
                H -= h[i];
                s.push({h[i], W});
            }
            else {
                auto [a, b] = s.top(); s.pop();
                s.push({a, b+W});
                H -= s.top().st;
            }
            ans = add(ans, mul(mul(mul(H, H+1), inv), mul(mul(W, W+1), inv)));
            ans = add(ans, mul(mul(H, s.top().st), mul(mul(W, W+1), inv)));
        }
        s.push({h[i], w[i]});
    }
    while (s.size() > 1) {
        int H = s.top().st;
        int W = s.top().nd;
        s.pop();
        auto [a, b] = s.top(); s.pop();
        s.push({a, b+W});
        H -= s.top().st;
        ans = add(ans, mul(mul(mul(H, H+1), inv), mul(mul(W, W+1), inv)));
        ans = add(ans, mul(mul(H, s.top().st), mul(mul(W, W+1), inv)));
    }
    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Compilation message

fancyfence.cpp: In function 'void solve()':
fancyfence.cpp:46:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   46 |                 auto [a, b] = s.top(); s.pop();
      |                      ^
fancyfence.cpp:59:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   59 |         auto [a, b] = s.top(); s.pop();
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -