Submission #67787456


Source Code Expand

#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC diagnostic ignored "-Wpedantic"
#endif

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(52);
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename V>
using table = gp_hash_table<T, V>;

using i128 = __int128;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;

const ll INF = 2e18;
const int inf = 1e9;
const int maxn = 1e5 + 7;
const int MOD = 998244353;
const ld pi = acos(-1);
const int P = 5167;
const int L = 26;
const ld EPS = 0.0005;

template<typename T, typename V>
void fill(T &container, V value) {
    for (auto &c : container)
        c = value;
}

#define int ll

void solve() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; ++i) cin >> v[i];
    vector<pair<int, int> > a;
    for (int i = 0; i < n; ++i) {
        if (i == 0 || v[i] != v[i - 1]) {
            a.push_back({v[i], 0});
        }
        a.back().second++;
    }
    vector<int> val(n), cnt(n);
    set<int> st;
    set<pair<int, int> > st1;
    for (int i = 0; i < a.size(); ++i) {
        val[i] = a[i].first;
        cnt[i] = a[i].second;
        st.insert(i);
        st1.insert({a[i].first, i});
    }
    ll ans = 0;
    while (st.size() > 1) {
        auto pos = st1.begin()->second;
        int num = val[pos], c = cnt[pos];
        auto it = st.lower_bound(pos);
        int Cnt = INF;
        int l = INF, r = INF;
        if (it != st.begin()) {
            auto it1 = it;
            it1--;
            l = val[*it1] - num;
            Cnt = min(Cnt, l);
        }
        it++;
        if (it != st.end()) {
            r = val[*it] - num;
            Cnt = min(Cnt, r);
        }
        while (c > 1 && Cnt > 0) {
            if (c % 2 == 1) {
                ans++;
                c++;
            }
            num++;
            c /= 2;
            Cnt--;
        }
        num += Cnt;
        ans += Cnt;
        if (l <= r) {
            auto it = st.lower_bound(pos);
            it--;
            int dpos = *it;
            st1.erase({val[dpos], dpos});
            st.erase(dpos);
            c += cnt[dpos];
        }
        if (l >= r) {
            auto it = st.lower_bound(pos);
            it++;
            int dpos = *it;
            st1.erase({val[dpos], dpos});
            st.erase(dpos);
            c += cnt[dpos];
        }
        st1.erase({val[pos], pos});
        val[pos] = num;
        cnt[pos] = c;
        st1.insert({val[pos], pos});
    }
    int d = cnt[*st.rbegin()];
    while (d > 1) {
        ans += d % 2;
        d++;
        d /= 2;
    }
    cout << ans << "\n";
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(7);
    int t = 1;
    cin >> t;
    while (t--) solve();
    //stress();
}

Submission Info

Submission Time
Task A - Merge and Increment
User Friendiks
Language C++ 20 (gcc 12.2)
Score 700
Code Size 3385 Byte
Status AC
Exec Time 489 ms
Memory 32980 KiB

Compile Error

Main.cpp: In function ‘void solve()’:
Main.cpp:59:23: warning: comparison of integer expressions of different signedness: ‘ll’ {aka ‘long long int’} and ‘std::vector<std::pair<long long int, long long int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   59 |     for (int i = 0; i < a.size(); ++i) {
      |                     ~~^~~~~~~~~~

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 2 ms 3468 KiB
01_small_00.txt AC 37 ms 3496 KiB
01_small_01.txt AC 37 ms 3484 KiB
01_small_02.txt AC 26 ms 3560 KiB
01_small_03.txt AC 45 ms 3496 KiB
01_small_04.txt AC 45 ms 3484 KiB
01_small_05.txt AC 45 ms 3620 KiB
01_small_06.txt AC 47 ms 3484 KiB
01_small_07.txt AC 60 ms 3540 KiB
01_small_08.txt AC 75 ms 3472 KiB
01_small_09.txt AC 33 ms 3468 KiB
01_small_10.txt AC 31 ms 3548 KiB
01_small_11.txt AC 30 ms 3340 KiB
01_small_12.txt AC 32 ms 3560 KiB
01_small_13.txt AC 46 ms 3344 KiB
01_small_14.txt AC 67 ms 3476 KiB
02_random_00.txt AC 365 ms 29192 KiB
02_random_01.txt AC 441 ms 32980 KiB
02_random_02.txt AC 351 ms 28288 KiB
02_random_03.txt AC 444 ms 32904 KiB
02_random_04.txt AC 426 ms 31712 KiB
02_random_05.txt AC 444 ms 32744 KiB
02_random_06.txt AC 226 ms 21484 KiB
02_random_07.txt AC 451 ms 32904 KiB
02_random_08.txt AC 225 ms 21172 KiB
02_random_09.txt AC 440 ms 32816 KiB
03_sorted_00.txt AC 125 ms 32820 KiB
03_sorted_01.txt AC 134 ms 32744 KiB
03_sorted_02.txt AC 137 ms 32908 KiB
03_sorted_03.txt AC 125 ms 32816 KiB
03_sorted_04.txt AC 140 ms 32816 KiB
03_sorted_05.txt AC 135 ms 32872 KiB
04_almost_sorted_00.txt AC 125 ms 32912 KiB
04_almost_sorted_01.txt AC 136 ms 32744 KiB
04_almost_sorted_02.txt AC 136 ms 32844 KiB
04_almost_sorted_03.txt AC 125 ms 32836 KiB
04_almost_sorted_04.txt AC 135 ms 32888 KiB
04_almost_sorted_05.txt AC 134 ms 32840 KiB
05_same_00.txt AC 8 ms 7848 KiB
05_same_01.txt AC 14 ms 7860 KiB
05_same_02.txt AC 13 ms 7844 KiB
06_corner_00.txt AC 149 ms 32892 KiB
06_corner_01.txt AC 131 ms 32872 KiB
06_corner_02.txt AC 129 ms 32912 KiB
07_hack_1_00.txt AC 13 ms 7792 KiB
07_hack_1_01.txt AC 13 ms 7808 KiB
07_hack_1_02.txt AC 13 ms 7880 KiB
07_hack_1_03.txt AC 14 ms 7864 KiB
07_hack_1_04.txt AC 13 ms 7948 KiB
08_hack_2_00.txt AC 132 ms 26572 KiB
08_hack_2_01.txt AC 131 ms 26536 KiB
08_hack_2_02.txt AC 131 ms 26548 KiB
08_hack_2_03.txt AC 132 ms 26576 KiB
08_hack_2_04.txt AC 131 ms 26392 KiB
09_hack_3_00.txt AC 8 ms 7788 KiB
09_hack_3_01.txt AC 8 ms 7884 KiB
09_hack_3_02.txt AC 198 ms 30240 KiB
09_hack_3_03.txt AC 199 ms 30176 KiB
09_hack_3_04.txt AC 367 ms 32656 KiB
09_hack_3_05.txt AC 359 ms 32552 KiB
09_hack_3_06.txt AC 450 ms 32912 KiB
09_hack_3_07.txt AC 440 ms 32836 KiB
09_hack_3_08.txt AC 485 ms 32888 KiB
09_hack_3_09.txt AC 489 ms 32892 KiB