Submission #67786024


Source Code Expand

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<class T>
using vec = vector<T>;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef KoRoVa
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif

const int max_n = -1, inf = 1000111222;



void test_case() {
    int n;
    cin >> n;
    vector <int> a(n);
    for (auto &i : a) cin >> i;
    ll ans = 0;
//    set <pii> val, pos;
//    for (int i = 0; i < n; i++) {
//        pos.insert({i, a[i]});
//        val.insert({a[i], i});
//    }
//    while (len(val) > 1) {
//        int cur_pos = val.begin()->second;
//        int cur_val = val.begin()->first;
////        LOG(cur_pos, cur_val);
//        auto it = pos.find({cur_pos, cur_val});
//        if (next(it) != pos.end() && next(it)->second == cur_val) {
//            auto gg = next(it);
//            val.erase(val.begin());
//            val.erase(make_pair(gg->second, gg->first));
//            pos.erase(it);
//            pos.erase(gg);
//            val.insert({cur_val + 1, cur_pos});
//            pos.insert({cur_pos, cur_val + 1});
//        }
//        else {
//            ++ans;
//            val.erase(val.begin());
//            pos.erase(it);
//            val.insert({cur_val + 1, cur_pos});
//            pos.insert({cur_pos, cur_val + 1});
//        }
//    }
    vector <pii> b(n);
    for (int i = 0; i < n; i++) {
        b[i] = make_pair(a[i], i);
    }
    sort(all(b));
    int cnt = 0, last = 0;
    set <int> pos;
    vector <int> active(n);
    for (int i = 0; i < n; i++) {
        pos.insert(i);
    }

    set <pii> save;

    auto make_op = [&] (int id, int val) {
//        LOG(id, val, ans);



        ans += (val - last) * 1ll * cnt;

        auto it = pos.find(id);

//        LOG(it == pos.end());


        if (it != pos.begin() && active[*prev(it)] == 1) {
            save.insert({val + 1, id});
            pos.erase(prev(it));
            --cnt;
            active[id] = 2;
        }
        else if (next(it) != pos.end() && active[*next(it)] == 1) {
            save.insert({val + 1, id});
            pos.erase(next(it));
            --cnt;
            active[id] = 2;
        }
        else {
            ++cnt;
            active[id] = 1;
        }







        last = val;

    };
    for (int i = 0; i < n; i++) {
        save.insert(b[i]);
    }
    while (!save.empty()) {
        int id = save.begin()->second;
        int val = save.begin()->first;
        save.erase(save.begin());
        make_op(id, val);


    }
//    for (int i = 0; i < n || !save.empty();) {
//
//
//
//
//
//
//        int id = b[i].second;
//        make_op(id, b[i].first);
//
//        ++i;
//    }
    cout << ans << '\n';
    LOG(ans);
}



int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;

     cin >> t;

    while (t--) test_case();

    return 0;
}

/*
KoRoVa!
*/

Submission Info

Submission Time
Task A - Merge and Increment
User Denisov
Language C++ 20 (gcc 12.2)
Score 700
Code Size 3857 Byte
Status AC
Exec Time 213 ms
Memory 25056 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 1
AC × 64
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 01_small_08.txt, 01_small_09.txt, 01_small_10.txt, 01_small_11.txt, 01_small_12.txt, 01_small_13.txt, 01_small_14.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 03_sorted_00.txt, 03_sorted_01.txt, 03_sorted_02.txt, 03_sorted_03.txt, 03_sorted_04.txt, 03_sorted_05.txt, 04_almost_sorted_00.txt, 04_almost_sorted_01.txt, 04_almost_sorted_02.txt, 04_almost_sorted_03.txt, 04_almost_sorted_04.txt, 04_almost_sorted_05.txt, 05_same_00.txt, 05_same_01.txt, 05_same_02.txt, 06_corner_00.txt, 06_corner_01.txt, 06_corner_02.txt, 07_hack_1_00.txt, 07_hack_1_01.txt, 07_hack_1_02.txt, 07_hack_1_03.txt, 07_hack_1_04.txt, 08_hack_2_00.txt, 08_hack_2_01.txt, 08_hack_2_02.txt, 08_hack_2_03.txt, 08_hack_2_04.txt, 09_hack_3_00.txt, 09_hack_3_01.txt, 09_hack_3_02.txt, 09_hack_3_03.txt, 09_hack_3_04.txt, 09_hack_3_05.txt, 09_hack_3_06.txt, 09_hack_3_07.txt, 09_hack_3_08.txt, 09_hack_3_09.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3420 KiB
01_small_00.txt AC 34 ms 3332 KiB
01_small_01.txt AC 34 ms 3488 KiB
01_small_02.txt AC 25 ms 3472 KiB
01_small_03.txt AC 40 ms 3396 KiB
01_small_04.txt AC 39 ms 3472 KiB
01_small_05.txt AC 41 ms 3480 KiB
01_small_06.txt AC 40 ms 3616 KiB
01_small_07.txt AC 45 ms 3616 KiB
01_small_08.txt AC 51 ms 3612 KiB
01_small_09.txt AC 33 ms 3448 KiB
01_small_10.txt AC 33 ms 3428 KiB
01_small_11.txt AC 34 ms 3428 KiB
01_small_12.txt AC 35 ms 3532 KiB
01_small_13.txt AC 47 ms 3404 KiB
01_small_14.txt AC 60 ms 3452 KiB
02_random_00.txt AC 161 ms 22268 KiB
02_random_01.txt AC 187 ms 24960 KiB
02_random_02.txt AC 155 ms 21888 KiB
02_random_03.txt AC 188 ms 25000 KiB
02_random_04.txt AC 179 ms 24164 KiB
02_random_05.txt AC 187 ms 24916 KiB
02_random_06.txt AC 106 ms 16604 KiB
02_random_07.txt AC 184 ms 24908 KiB
02_random_08.txt AC 104 ms 16460 KiB
02_random_09.txt AC 186 ms 24920 KiB
03_sorted_00.txt AC 100 ms 24960 KiB
03_sorted_01.txt AC 108 ms 24960 KiB
03_sorted_02.txt AC 107 ms 24824 KiB
03_sorted_03.txt AC 100 ms 24912 KiB
03_sorted_04.txt AC 107 ms 24948 KiB
03_sorted_05.txt AC 108 ms 24952 KiB
04_almost_sorted_00.txt AC 99 ms 24964 KiB
04_almost_sorted_01.txt AC 106 ms 24912 KiB
04_almost_sorted_02.txt AC 106 ms 24920 KiB
04_almost_sorted_03.txt AC 101 ms 24928 KiB
04_almost_sorted_04.txt AC 106 ms 24864 KiB
04_almost_sorted_05.txt AC 108 ms 24932 KiB
05_same_00.txt AC 142 ms 24964 KiB
05_same_01.txt AC 146 ms 24940 KiB
05_same_02.txt AC 147 ms 24900 KiB
06_corner_00.txt AC 147 ms 24916 KiB
06_corner_01.txt AC 120 ms 24964 KiB
06_corner_02.txt AC 99 ms 24936 KiB
07_hack_1_00.txt AC 146 ms 24932 KiB
07_hack_1_01.txt AC 143 ms 24900 KiB
07_hack_1_02.txt AC 152 ms 24960 KiB
07_hack_1_03.txt AC 144 ms 24920 KiB
07_hack_1_04.txt AC 143 ms 24912 KiB
08_hack_2_00.txt AC 158 ms 24896 KiB
08_hack_2_01.txt AC 156 ms 24912 KiB
08_hack_2_02.txt AC 161 ms 24928 KiB
08_hack_2_03.txt AC 157 ms 24900 KiB
08_hack_2_04.txt AC 156 ms 24940 KiB
09_hack_3_00.txt AC 142 ms 24956 KiB
09_hack_3_01.txt AC 142 ms 25056 KiB
09_hack_3_02.txt AC 159 ms 24908 KiB
09_hack_3_03.txt AC 158 ms 24928 KiB
09_hack_3_04.txt AC 189 ms 25004 KiB
09_hack_3_05.txt AC 185 ms 24920 KiB
09_hack_3_06.txt AC 211 ms 24828 KiB
09_hack_3_07.txt AC 210 ms 24896 KiB
09_hack_3_08.txt AC 213 ms 24960 KiB
09_hack_3_09.txt AC 212 ms 24948 KiB