Submission #782779

# Submission time Handle Problem Language Result Execution time Memory
782779 2023-07-14T09:15:55 Z math_rabbit_1028 즐거운 채소 기르기 (JOI14_growing) C++14
45 / 100
1000 ms 1168 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, arr[303030];
vector<int> com;

int tree[1010101];
ll ans, tot1[303030], tot2[303030], cnt1[303030], cnt2[303030];

void init(int v, int st, int ed) {
    if (st == ed) {
        tree[v] = 0;
        return;
    }
    int mid = (st + ed) / 2;
    init(2 * v, st, mid);
    init(2 * v + 1, mid + 1, ed);
    tree[v] = tree[2 * v] + tree[2 * v + 1];
}

void update(int v, int st, int ed, int idx, int val) {
    if (st == idx && ed == idx) {
        tree[v] += val;
        return;
    }
    if (idx < st || idx > ed) return;
    int mid = (st + ed) / 2;
    update(2 * v, st, mid, idx, val);
    update(2 * v + 1, mid + 1, ed, idx, val);
    tree[v] = tree[2 * v] + tree[2 * v + 1];
}

int get(int v, int st, int ed, int lt, int rt) {
    if (st > rt || ed < lt) return 0;
    if (lt <= st && ed <= rt) return tree[v];
    int mid = (st + ed) / 2;
    return get(2 * v, st, mid, lt, rt) + get(2 * v + 1, mid + 1, ed, lt, rt);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++) cin >> arr[i];

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < i; j++) tot1[i] += (arr[i] < arr[j]);
        for (int j = n; j > i; j--) tot2[i] += (arr[i] < arr[j]);
    }

    for (int i = 1; i <= n; i++) com.push_back(arr[i]);
    sort(com.begin(), com.end());
    com.erase(unique(com.begin(), com.end()), com.end());
    for (int i = 1; i <= n; i++) {
        arr[i] = lower_bound(com.begin(), com.end(), arr[i]) - com.begin();
    }

    init(1, 0, com.size() - 1);
    for (int i = 1; i <= n; i++) {
        cnt1[i] = get(1, 0, com.size() - 1, arr[i] + 1, com.size() - 1);
        update(1, 0, com.size() - 1, arr[i], 1);
    }

    init(1, 0, com.size() - 1);
    for (int i = n; i >= 1; i--) {
        cnt2[i] = get(1, 0, com.size() - 1, arr[i] + 1, com.size() - 1);
        update(1, 0, com.size() - 1, arr[i], 1);
    }

    for (int i = 1; i <= n; i++) {
        ans += min(cnt1[i], cnt2[i]);
        assert(cnt1[i] == tot1[i]);
    }

    cout << ans << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 336 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 11 ms 476 KB Output is correct
5 Correct 18 ms 692 KB Output is correct
6 Correct 17 ms 596 KB Output is correct
7 Correct 16 ms 600 KB Output is correct
8 Correct 19 ms 576 KB Output is correct
9 Correct 19 ms 548 KB Output is correct
10 Correct 18 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1072 ms 1168 KB Time limit exceeded
2 Halted 0 ms 0 KB -