답안 #1013924

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1013924 2024-07-04T08:14:03 Z BackNoob Real Mountains (CCO23_day1problem2) C++14
12 / 25
250 ms 3928 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
 
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e6 + 3;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
    if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
    if (a < b) {a = b; return true;} return false;
}

int n;
int a[mxN];
int b[mxN];
bool vis[mxN];
pair<int, int> seg[mxN];

int getsum(int l, int r) {
    if(l > r) return 0;
    return (1LL * (r + l) * (r - l + 1) / 2) % mod;
}

void add(int &a, int b) {
    a += b;
    if(a >= mod)
        a -= mod;
}
 
void solve()
{
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];

    int mx = *max_element(a + 1, a + n + 1);
    int p = -1;
    for(int i = 1; i <= n; i++) if(a[i] == mx) p = i;

    int curmax = -1;
    for(int i = 1; i <= p; i++) {
        maximize(curmax, a[i]);
        b[i] = curmax;
    }
    curmax = -1;
    for(int i = n; i >= p; i--) {
        maximize(curmax, a[i]);
        b[i] = curmax;
    }

    

    int res = 0;
    int timer = 0;
    while(true) {
        ++timer;
        bool ok = true;
        for(int i = 1; i <= n; i++) if(a[i] != b[i]) ok = false;
        if(ok == true) break;

        // for(int i = 1; i <= n; i++) cout << a[i] << ' ';
        // cout << endl;

        int minval = inf;
        for(int i = 1; i <= n; i++) {
            if(a[i] != b[i]) {
                minval = min(minval, a[i]);
            }
        }

        vector<int> pos;
        for(int i = 1; i <= n; i++) if(a[i] != b[i]) {
            if(a[i] == minval) pos.pb(i);
        }

        // for(auto it : pos) cout << it << ' ';
        // cout << endl;


        if(sz(pos) > 1) {
            int p1 = pos[0];
            int p2 = pos.back();

            int l1 = inf;
            int r1 = inf;
            for(int i = 1; i < p1; i++) if(a[i] > a[p1]) {
                minimize(l1, a[i]);
            }
            for(int i = p1 + 1; i <= n; i++) if(a[i] > a[p1]) {
                minimize(r1, a[i]);
            }

            int l2 = inf;
            int r2 = inf;
            for(int i = 1; i < p2; i++) if(a[i] > a[p2]) {
                minimize(l2, a[i]);
            }
            for(int i = p2 + 1; i <= n; i++) if(a[i] > a[p2]) {
                minimize(r2, a[i]);
            }

            // cout << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << endl;

            int minval = min({l1, r1, l2, r2});
            if(r1 <= l2) {
                add(res, getsum(a[p1], minval - 1));
                add(res, 1LL * l1 * (minval - a[p1]) % mod);
                add(res, 1LL * r1 * (minval - a[p1]) % mod);


                add(res, getsum(a[p2], minval - 1));
                add(res, getsum(a[p2] + 1, minval));
                add(res, 1LL * r2 * (minval - a[p2]) % mod);

            }
            else {
                add(res, getsum(a[p2], minval - 1));
                add(res, 1LL * l2 * (minval - a[p2]) % mod);
                add(res, 1LL * r2 * (minval - a[p2]) % mod);
                // cout << res << endl;

                add(res, getsum(a[p1], minval - 1));
                add(res, 1LL * l1 * (minval - a[p1]) % mod);
                add(res, getsum(a[p1] + 1, minval));
            }

            for(int i = 1; i + 1 < sz(pos); i++) {
                int x = pos[i];
                add(res, 2LL * getsum(a[x] + 1, minval) % mod);
                add(res, getsum(a[x], minval - 1));
            }
            for(auto it : pos) a[it] = minval;
        }
        else {
            int p1 = pos[0];

            int l1 = inf;
            int r1 = inf;
            for(int i = 1; i < p1; i++) if(a[i] > a[p1]) {
                minimize(l1, a[i]);
            }
            for(int i = p1 + 1; i <= n; i++) if(a[i] > a[p1]) {
                minimize(r1, a[i]);
            }
            int minval = min(l1, r1);
            add(res, getsum(a[p1], minval - 1));
            add(res, 1LL * l1 * (minval - a[p1]) % mod);
            add(res, 1LL * r1 * (minval - a[p1]) % mod);


            for(auto it : pos) a[it] = minval;
        }
    }
    

    cout << res;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

 
    int tc = 1;
    //cin >> tc;
    while(tc--) {
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 6 ms 348 KB Output is correct
17 Correct 6 ms 544 KB Output is correct
18 Correct 8 ms 600 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 4 ms 568 KB Output is correct
26 Correct 5 ms 600 KB Output is correct
27 Correct 5 ms 348 KB Output is correct
28 Correct 5 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 6 ms 348 KB Output is correct
17 Correct 6 ms 544 KB Output is correct
18 Correct 8 ms 600 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 4 ms 568 KB Output is correct
26 Correct 5 ms 600 KB Output is correct
27 Correct 5 ms 348 KB Output is correct
28 Correct 5 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 230 ms 556 KB Output is correct
36 Correct 215 ms 548 KB Output is correct
37 Correct 213 ms 600 KB Output is correct
38 Correct 217 ms 592 KB Output is correct
39 Correct 214 ms 344 KB Output is correct
40 Correct 1 ms 472 KB Output is correct
41 Correct 1 ms 484 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 203 ms 552 KB Output is correct
44 Correct 192 ms 556 KB Output is correct
45 Correct 182 ms 348 KB Output is correct
46 Correct 212 ms 536 KB Output is correct
47 Correct 202 ms 544 KB Output is correct
48 Correct 196 ms 556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 6 ms 348 KB Output is correct
17 Correct 6 ms 544 KB Output is correct
18 Correct 8 ms 600 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 4 ms 568 KB Output is correct
26 Correct 5 ms 600 KB Output is correct
27 Correct 5 ms 348 KB Output is correct
28 Correct 5 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 230 ms 556 KB Output is correct
36 Correct 215 ms 548 KB Output is correct
37 Correct 213 ms 600 KB Output is correct
38 Correct 217 ms 592 KB Output is correct
39 Correct 214 ms 344 KB Output is correct
40 Correct 1 ms 472 KB Output is correct
41 Correct 1 ms 484 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 203 ms 552 KB Output is correct
44 Correct 192 ms 556 KB Output is correct
45 Correct 182 ms 348 KB Output is correct
46 Correct 212 ms 536 KB Output is correct
47 Correct 202 ms 544 KB Output is correct
48 Correct 196 ms 556 KB Output is correct
49 Correct 223 ms 592 KB Output is correct
50 Correct 214 ms 564 KB Output is correct
51 Correct 218 ms 824 KB Output is correct
52 Correct 250 ms 596 KB Output is correct
53 Correct 220 ms 596 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 198 ms 568 KB Output is correct
58 Correct 219 ms 564 KB Output is correct
59 Correct 189 ms 596 KB Output is correct
60 Correct 238 ms 568 KB Output is correct
61 Correct 205 ms 604 KB Output is correct
62 Correct 200 ms 564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 6 ms 348 KB Output is correct
17 Correct 6 ms 544 KB Output is correct
18 Correct 8 ms 600 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 4 ms 568 KB Output is correct
26 Correct 5 ms 600 KB Output is correct
27 Correct 5 ms 348 KB Output is correct
28 Correct 5 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Incorrect 19 ms 3928 KB Output isn't correct
36 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 6 ms 348 KB Output is correct
17 Correct 6 ms 544 KB Output is correct
18 Correct 8 ms 600 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 4 ms 568 KB Output is correct
26 Correct 5 ms 600 KB Output is correct
27 Correct 5 ms 348 KB Output is correct
28 Correct 5 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 230 ms 556 KB Output is correct
36 Correct 215 ms 548 KB Output is correct
37 Correct 213 ms 600 KB Output is correct
38 Correct 217 ms 592 KB Output is correct
39 Correct 214 ms 344 KB Output is correct
40 Correct 1 ms 472 KB Output is correct
41 Correct 1 ms 484 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 203 ms 552 KB Output is correct
44 Correct 192 ms 556 KB Output is correct
45 Correct 182 ms 348 KB Output is correct
46 Correct 212 ms 536 KB Output is correct
47 Correct 202 ms 544 KB Output is correct
48 Correct 196 ms 556 KB Output is correct
49 Incorrect 19 ms 3928 KB Output isn't correct
50 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 6 ms 348 KB Output is correct
17 Correct 6 ms 544 KB Output is correct
18 Correct 8 ms 600 KB Output is correct
19 Correct 6 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 4 ms 568 KB Output is correct
26 Correct 5 ms 600 KB Output is correct
27 Correct 5 ms 348 KB Output is correct
28 Correct 5 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 230 ms 556 KB Output is correct
36 Correct 215 ms 548 KB Output is correct
37 Correct 213 ms 600 KB Output is correct
38 Correct 217 ms 592 KB Output is correct
39 Correct 214 ms 344 KB Output is correct
40 Correct 1 ms 472 KB Output is correct
41 Correct 1 ms 484 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 203 ms 552 KB Output is correct
44 Correct 192 ms 556 KB Output is correct
45 Correct 182 ms 348 KB Output is correct
46 Correct 212 ms 536 KB Output is correct
47 Correct 202 ms 544 KB Output is correct
48 Correct 196 ms 556 KB Output is correct
49 Correct 223 ms 592 KB Output is correct
50 Correct 214 ms 564 KB Output is correct
51 Correct 218 ms 824 KB Output is correct
52 Correct 250 ms 596 KB Output is correct
53 Correct 220 ms 596 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 198 ms 568 KB Output is correct
58 Correct 219 ms 564 KB Output is correct
59 Correct 189 ms 596 KB Output is correct
60 Correct 238 ms 568 KB Output is correct
61 Correct 205 ms 604 KB Output is correct
62 Correct 200 ms 564 KB Output is correct
63 Incorrect 19 ms 3928 KB Output isn't correct
64 Halted 0 ms 0 KB -