답안 #1013926

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1013926 2024-07-04T08:15:07 Z BackNoob Real Mountains (CCO23_day1problem2) C++14
16 / 25
5000 ms 19180 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 = 1e6 + 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 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 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 592 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 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 592 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 7 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 7 ms 480 KB Output is correct
17 Correct 6 ms 576 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 6 ms 576 KB Output is correct
20 Correct 2 ms 344 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 348 KB Output is correct
26 Correct 5 ms 348 KB Output is correct
27 Correct 5 ms 344 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 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 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 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 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 592 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 7 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 7 ms 480 KB Output is correct
17 Correct 6 ms 576 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 6 ms 576 KB Output is correct
20 Correct 2 ms 344 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 348 KB Output is correct
26 Correct 5 ms 348 KB Output is correct
27 Correct 5 ms 344 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 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 215 ms 600 KB Output is correct
36 Correct 221 ms 564 KB Output is correct
37 Correct 220 ms 564 KB Output is correct
38 Correct 222 ms 600 KB Output is correct
39 Correct 243 ms 552 KB Output is correct
40 Correct 2 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 588 KB Output is correct
43 Correct 186 ms 556 KB Output is correct
44 Correct 183 ms 560 KB Output is correct
45 Correct 176 ms 596 KB Output is correct
46 Correct 175 ms 592 KB Output is correct
47 Correct 196 ms 556 KB Output is correct
48 Correct 201 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 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 592 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 7 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 7 ms 480 KB Output is correct
17 Correct 6 ms 576 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 6 ms 576 KB Output is correct
20 Correct 2 ms 344 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 348 KB Output is correct
26 Correct 5 ms 348 KB Output is correct
27 Correct 5 ms 344 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 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 215 ms 600 KB Output is correct
36 Correct 221 ms 564 KB Output is correct
37 Correct 220 ms 564 KB Output is correct
38 Correct 222 ms 600 KB Output is correct
39 Correct 243 ms 552 KB Output is correct
40 Correct 2 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 588 KB Output is correct
43 Correct 186 ms 556 KB Output is correct
44 Correct 183 ms 560 KB Output is correct
45 Correct 176 ms 596 KB Output is correct
46 Correct 175 ms 592 KB Output is correct
47 Correct 196 ms 556 KB Output is correct
48 Correct 201 ms 344 KB Output is correct
49 Correct 218 ms 592 KB Output is correct
50 Correct 214 ms 576 KB Output is correct
51 Correct 226 ms 596 KB Output is correct
52 Correct 224 ms 596 KB Output is correct
53 Correct 236 ms 592 KB Output is correct
54 Correct 2 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 213 ms 596 KB Output is correct
58 Correct 193 ms 564 KB Output is correct
59 Correct 196 ms 596 KB Output is correct
60 Correct 209 ms 596 KB Output is correct
61 Correct 208 ms 596 KB Output is correct
62 Correct 204 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 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 592 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 7 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 7 ms 480 KB Output is correct
17 Correct 6 ms 576 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 6 ms 576 KB Output is correct
20 Correct 2 ms 344 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 348 KB Output is correct
26 Correct 5 ms 348 KB Output is correct
27 Correct 5 ms 344 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 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1537 ms 19028 KB Output is correct
36 Correct 1551 ms 19108 KB Output is correct
37 Correct 1496 ms 19120 KB Output is correct
38 Correct 1496 ms 19008 KB Output is correct
39 Correct 1506 ms 19096 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 196 ms 18384 KB Output is correct
43 Correct 185 ms 18376 KB Output is correct
44 Correct 206 ms 18376 KB Output is correct
45 Correct 1002 ms 19072 KB Output is correct
46 Correct 977 ms 19076 KB Output is correct
47 Correct 962 ms 19136 KB Output is correct
48 Correct 1120 ms 19136 KB Output is correct
49 Correct 1099 ms 19180 KB Output is correct
50 Correct 1096 ms 19040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 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 592 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 7 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 7 ms 480 KB Output is correct
17 Correct 6 ms 576 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 6 ms 576 KB Output is correct
20 Correct 2 ms 344 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 348 KB Output is correct
26 Correct 5 ms 348 KB Output is correct
27 Correct 5 ms 344 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 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 215 ms 600 KB Output is correct
36 Correct 221 ms 564 KB Output is correct
37 Correct 220 ms 564 KB Output is correct
38 Correct 222 ms 600 KB Output is correct
39 Correct 243 ms 552 KB Output is correct
40 Correct 2 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 588 KB Output is correct
43 Correct 186 ms 556 KB Output is correct
44 Correct 183 ms 560 KB Output is correct
45 Correct 176 ms 596 KB Output is correct
46 Correct 175 ms 592 KB Output is correct
47 Correct 196 ms 556 KB Output is correct
48 Correct 201 ms 344 KB Output is correct
49 Correct 1537 ms 19028 KB Output is correct
50 Correct 1551 ms 19108 KB Output is correct
51 Correct 1496 ms 19120 KB Output is correct
52 Correct 1496 ms 19008 KB Output is correct
53 Correct 1506 ms 19096 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 196 ms 18384 KB Output is correct
57 Correct 185 ms 18376 KB Output is correct
58 Correct 206 ms 18376 KB Output is correct
59 Correct 1002 ms 19072 KB Output is correct
60 Correct 977 ms 19076 KB Output is correct
61 Correct 962 ms 19136 KB Output is correct
62 Correct 1120 ms 19136 KB Output is correct
63 Correct 1099 ms 19180 KB Output is correct
64 Correct 1096 ms 19040 KB Output is correct
65 Execution timed out 5021 ms 15016 KB Time limit exceeded
66 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 5 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 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 592 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 7 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 7 ms 480 KB Output is correct
17 Correct 6 ms 576 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 6 ms 576 KB Output is correct
20 Correct 2 ms 344 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 348 KB Output is correct
26 Correct 5 ms 348 KB Output is correct
27 Correct 5 ms 344 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 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 215 ms 600 KB Output is correct
36 Correct 221 ms 564 KB Output is correct
37 Correct 220 ms 564 KB Output is correct
38 Correct 222 ms 600 KB Output is correct
39 Correct 243 ms 552 KB Output is correct
40 Correct 2 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 588 KB Output is correct
43 Correct 186 ms 556 KB Output is correct
44 Correct 183 ms 560 KB Output is correct
45 Correct 176 ms 596 KB Output is correct
46 Correct 175 ms 592 KB Output is correct
47 Correct 196 ms 556 KB Output is correct
48 Correct 201 ms 344 KB Output is correct
49 Correct 218 ms 592 KB Output is correct
50 Correct 214 ms 576 KB Output is correct
51 Correct 226 ms 596 KB Output is correct
52 Correct 224 ms 596 KB Output is correct
53 Correct 236 ms 592 KB Output is correct
54 Correct 2 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 1 ms 348 KB Output is correct
57 Correct 213 ms 596 KB Output is correct
58 Correct 193 ms 564 KB Output is correct
59 Correct 196 ms 596 KB Output is correct
60 Correct 209 ms 596 KB Output is correct
61 Correct 208 ms 596 KB Output is correct
62 Correct 204 ms 604 KB Output is correct
63 Correct 1537 ms 19028 KB Output is correct
64 Correct 1551 ms 19108 KB Output is correct
65 Correct 1496 ms 19120 KB Output is correct
66 Correct 1496 ms 19008 KB Output is correct
67 Correct 1506 ms 19096 KB Output is correct
68 Correct 1 ms 348 KB Output is correct
69 Correct 1 ms 348 KB Output is correct
70 Correct 196 ms 18384 KB Output is correct
71 Correct 185 ms 18376 KB Output is correct
72 Correct 206 ms 18376 KB Output is correct
73 Correct 1002 ms 19072 KB Output is correct
74 Correct 977 ms 19076 KB Output is correct
75 Correct 962 ms 19136 KB Output is correct
76 Correct 1120 ms 19136 KB Output is correct
77 Correct 1099 ms 19180 KB Output is correct
78 Correct 1096 ms 19040 KB Output is correct
79 Execution timed out 5021 ms 15016 KB Time limit exceeded
80 Halted 0 ms 0 KB -