Submission #67785945


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define mp make_pair
#define all(x) x.begin(), x.end()
#define x first
#define y second

void solve() { 
    
    int n;
    cin >> n;

    vector<ll> A(n);
    for(auto& i : A) cin >> i;

    ll ans = 0;
    stack<int> st;

    for(auto f : A){
        vector<int> tmp;
        while(!st.empty() && st.top() < f){
            tmp.push_back(st.top()); st.pop();
        }
        
        if (!tmp.empty()){
            ans += f - tmp[0] - (int)tmp.size() + 1;
            int tval = f;
            while(!st.empty() && st.top() == tval){
                st.pop();
                tval++;
            }
            st.push(tval);
        }

        int val = f;
        while(!st.empty() && st.top() == val){
            st.pop();
            val++;
        }
        st.push(val);
    }

    vector<ll> tmp;
    while(!st.empty()) {
        tmp.push_back(st.top()); st.pop();
    }

    ans += tmp.back() - tmp[0];
    if ((int)tmp.size() >= 2){
        ans -= (int)tmp.size() - 2;
    }
    cout << ans << '\n';
}

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

    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Submission Info

Submission Time
Task A - Merge and Increment
User JYJin
Language C++ 20 (gcc 12.2)
Score 700
Code Size 1300 Byte
Status AC
Exec Time 29 ms
Memory 7656 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 3460 KiB
01_small_00.txt AC 15 ms 3448 KiB
01_small_01.txt AC 15 ms 3404 KiB
01_small_02.txt AC 11 ms 3448 KiB
01_small_03.txt AC 29 ms 3336 KiB
01_small_04.txt AC 27 ms 3448 KiB
01_small_05.txt AC 24 ms 3532 KiB
01_small_06.txt AC 23 ms 3408 KiB
01_small_07.txt AC 20 ms 3432 KiB
01_small_08.txt AC 19 ms 3492 KiB
01_small_09.txt AC 22 ms 3468 KiB
01_small_10.txt AC 18 ms 3392 KiB
01_small_11.txt AC 16 ms 3436 KiB
01_small_12.txt AC 16 ms 3472 KiB
01_small_13.txt AC 14 ms 3464 KiB
01_small_14.txt AC 14 ms 3428 KiB
02_random_00.txt AC 15 ms 4376 KiB
02_random_01.txt AC 18 ms 4656 KiB
02_random_02.txt AC 15 ms 4460 KiB
02_random_03.txt AC 17 ms 4572 KiB
02_random_04.txt AC 17 ms 4564 KiB
02_random_05.txt AC 18 ms 4664 KiB
02_random_06.txt AC 11 ms 4092 KiB
02_random_07.txt AC 18 ms 4592 KiB
02_random_08.txt AC 11 ms 4052 KiB
02_random_09.txt AC 17 ms 4496 KiB
03_sorted_00.txt AC 15 ms 4580 KiB
03_sorted_01.txt AC 14 ms 7480 KiB
03_sorted_02.txt AC 14 ms 7656 KiB
03_sorted_03.txt AC 15 ms 4632 KiB
03_sorted_04.txt AC 14 ms 7564 KiB
03_sorted_05.txt AC 14 ms 7580 KiB
04_almost_sorted_00.txt AC 15 ms 4576 KiB
04_almost_sorted_01.txt AC 14 ms 7408 KiB
04_almost_sorted_02.txt AC 14 ms 7544 KiB
04_almost_sorted_03.txt AC 15 ms 4636 KiB
04_almost_sorted_04.txt AC 14 ms 7532 KiB
04_almost_sorted_05.txt AC 14 ms 7508 KiB
05_same_00.txt AC 8 ms 4496 KiB
05_same_01.txt AC 13 ms 4620 KiB
05_same_02.txt AC 12 ms 4644 KiB
06_corner_00.txt AC 12 ms 4628 KiB
06_corner_01.txt AC 13 ms 6456 KiB
06_corner_02.txt AC 15 ms 4628 KiB
07_hack_1_00.txt AC 12 ms 4576 KiB
07_hack_1_01.txt AC 12 ms 4632 KiB
07_hack_1_02.txt AC 13 ms 4492 KiB
07_hack_1_03.txt AC 12 ms 4628 KiB
07_hack_1_04.txt AC 13 ms 4636 KiB
08_hack_2_00.txt AC 17 ms 4492 KiB
08_hack_2_01.txt AC 16 ms 4632 KiB
08_hack_2_02.txt AC 15 ms 4624 KiB
08_hack_2_03.txt AC 15 ms 4640 KiB
08_hack_2_04.txt AC 15 ms 4564 KiB
09_hack_3_00.txt AC 7 ms 4620 KiB
09_hack_3_01.txt AC 8 ms 4500 KiB
09_hack_3_02.txt AC 12 ms 4608 KiB
09_hack_3_03.txt AC 12 ms 4584 KiB
09_hack_3_04.txt AC 13 ms 4576 KiB
09_hack_3_05.txt AC 13 ms 4576 KiB
09_hack_3_06.txt AC 14 ms 4728 KiB
09_hack_3_07.txt AC 14 ms 4584 KiB
09_hack_3_08.txt AC 15 ms 4652 KiB
09_hack_3_09.txt AC 15 ms 4628 KiB