Submission #67787614


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

long long solve() {
    int N;
    cin >> N;

    long long ans = 0;
    vector<int> vec(N);
    map<int, vector<pair<int, int>>> mp;
    vector<int> rht_end(N);
    for (int i = 0; i < N; i++) {
        cin >> vec[i];
        mp[vec[i]].push_back(make_pair(i, i));
        rht_end[i] = i;
    }

    int mergecnt = 0;
    while (mergecnt < N - 1) {
        auto& fst = *mp.begin();
        sort(fst.second.begin(), fst.second.end());
        for (int i = 1; i < fst.second.size(); i++) {
            pair<int, int>& lft = fst.second[i - 1];
            pair<int, int>& rht = fst.second[i];
            if (lft.second == rht.first - 1) {
                vec[lft.first]++;
                mp[fst.first + 1].push_back(make_pair(lft.first, rht.second));
                rht_end[rht.second] = lft.first;
                lft = make_pair(-2, -2);
                rht = make_pair(-2, -2);
                mergecnt++;
            }
        }

        if (mergecnt == N - 1) {
            break;
        }

        for (int i = 0; i < fst.second.size(); i++) {
            pair<int, int> p = fst.second[i];
            if (p.first != -2) {
                int nxt = INT_MAX;
                if (p.first != 0) {
                    int l = rht_end[p.first - 1];
                    nxt = vec[l];
                }
                if (p.second != N - 1) {
                    nxt = min(nxt, vec[p.second + 1]);
                }
                vec[p.first] = nxt;
                ans += nxt - fst.first;
                mp[nxt].push_back(p);
            }
        }
        mp.erase(mp.begin());
    }
    
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
        cout << solve() << "\n";
    }
}

Submission Info

Submission Time
Task A - Merge and Increment
User AQT
Language C++ 20 (gcc 12.2)
Score 700
Code Size 1874 Byte
Status AC
Exec Time 185 ms
Memory 26588 KiB

Compile Error

Main.cpp: In function ‘long long int solve()’:
Main.cpp:23:27: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   23 |         for (int i = 1; i < fst.second.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~
Main.cpp:40:27: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   40 |         for (int i = 0; i < fst.second.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 1 ms 3608 KiB
01_small_00.txt AC 32 ms 3536 KiB
01_small_01.txt AC 32 ms 3500 KiB
01_small_02.txt AC 32 ms 3488 KiB
01_small_03.txt AC 42 ms 3456 KiB
01_small_04.txt AC 41 ms 3540 KiB
01_small_05.txt AC 43 ms 3432 KiB
01_small_06.txt AC 43 ms 3616 KiB
01_small_07.txt AC 49 ms 3480 KiB
01_small_08.txt AC 56 ms 3504 KiB
01_small_09.txt AC 31 ms 3472 KiB
01_small_10.txt AC 30 ms 3484 KiB
01_small_11.txt AC 28 ms 3548 KiB
01_small_12.txt AC 28 ms 3504 KiB
01_small_13.txt AC 32 ms 3488 KiB
01_small_14.txt AC 38 ms 3544 KiB
02_random_00.txt AC 153 ms 23592 KiB
02_random_01.txt AC 183 ms 26520 KiB
02_random_02.txt AC 145 ms 23084 KiB
02_random_03.txt AC 183 ms 26532 KiB
02_random_04.txt AC 170 ms 25692 KiB
02_random_05.txt AC 183 ms 26588 KiB
02_random_06.txt AC 98 ms 17576 KiB
02_random_07.txt AC 185 ms 26480 KiB
02_random_08.txt AC 97 ms 17596 KiB
02_random_09.txt AC 181 ms 26480 KiB
03_sorted_00.txt AC 78 ms 26412 KiB
03_sorted_01.txt AC 77 ms 26508 KiB
03_sorted_02.txt AC 75 ms 26488 KiB
03_sorted_03.txt AC 77 ms 26488 KiB
03_sorted_04.txt AC 75 ms 26544 KiB
03_sorted_05.txt AC 78 ms 26552 KiB
04_almost_sorted_00.txt AC 78 ms 26540 KiB
04_almost_sorted_01.txt AC 75 ms 26532 KiB
04_almost_sorted_02.txt AC 75 ms 26488 KiB
04_almost_sorted_03.txt AC 78 ms 26524 KiB
04_almost_sorted_04.txt AC 76 ms 26480 KiB
04_almost_sorted_05.txt AC 76 ms 26572 KiB
05_same_00.txt AC 16 ms 8004 KiB
05_same_01.txt AC 21 ms 7980 KiB
05_same_02.txt AC 20 ms 8080 KiB
06_corner_00.txt AC 32 ms 8088 KiB
06_corner_01.txt AC 77 ms 26504 KiB
06_corner_02.txt AC 80 ms 26532 KiB
07_hack_1_00.txt AC 23 ms 6768 KiB
07_hack_1_01.txt AC 25 ms 7264 KiB
07_hack_1_02.txt AC 20 ms 6876 KiB
07_hack_1_03.txt AC 22 ms 6692 KiB
07_hack_1_04.txt AC 22 ms 7976 KiB
08_hack_2_00.txt AC 31 ms 7296 KiB
08_hack_2_01.txt AC 32 ms 6956 KiB
08_hack_2_02.txt AC 31 ms 6972 KiB
08_hack_2_03.txt AC 33 ms 7232 KiB
08_hack_2_04.txt AC 32 ms 7288 KiB
09_hack_3_00.txt AC 16 ms 8004 KiB
09_hack_3_01.txt AC 15 ms 7968 KiB
09_hack_3_02.txt AC 33 ms 6760 KiB
09_hack_3_03.txt AC 33 ms 6740 KiB
09_hack_3_04.txt AC 51 ms 7896 KiB
09_hack_3_05.txt AC 51 ms 7940 KiB
09_hack_3_06.txt AC 63 ms 7564 KiB
09_hack_3_07.txt AC 64 ms 7736 KiB
09_hack_3_08.txt AC 85 ms 8532 KiB
09_hack_3_09.txt AC 85 ms 8532 KiB