結果

問題 No.2304 Distinct Elements
ユーザー milanis48663220milanis48663220
提出日時 2023-05-12 23:19:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,839 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 125,740 KB
実行使用メモリ 15,276 KB
最終ジャッジ日時 2023-08-19 06:11:51
合計ジャッジ時間 4,948 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 26 ms
7,732 KB
testcase_05 AC 26 ms
7,732 KB
testcase_06 AC 25 ms
7,736 KB
testcase_07 AC 26 ms
7,832 KB
testcase_08 AC 25 ms
7,732 KB
testcase_09 AC 48 ms
15,156 KB
testcase_10 AC 47 ms
15,276 KB
testcase_11 AC 49 ms
15,248 KB
testcase_12 AC 49 ms
15,092 KB
testcase_13 AC 48 ms
15,236 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 1 ms
4,380 KB
testcase_20 WA -
testcase_21 AC 2 ms
4,376 KB
testcase_22 WA -
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 24 ms
6,496 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 20 ms
6,088 KB
testcase_32 AC 40 ms
13,624 KB
testcase_33 AC 4 ms
4,380 KB
testcase_34 AC 40 ms
14,384 KB
testcase_35 AC 44 ms
14,236 KB
testcase_36 AC 4 ms
4,380 KB
testcase_37 AC 10 ms
4,380 KB
testcase_38 AC 29 ms
7,776 KB
testcase_39 AC 29 ms
7,824 KB
testcase_40 AC 16 ms
6,280 KB
testcase_41 AC 9 ms
4,564 KB
testcase_42 AC 19 ms
7,824 KB
testcase_43 AC 32 ms
7,860 KB
testcase_44 AC 37 ms
13,140 KB
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 27 ms
7,716 KB
testcase_50 AC 29 ms
7,800 KB
testcase_51 AC 31 ms
7,844 KB
testcase_52 WA -
testcase_53 AC 31 ms
15,020 KB
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 AC 27 ms
9,616 KB
testcase_58 AC 27 ms
9,532 KB
testcase_59 AC 23 ms
9,612 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using P = pair<ll, ll>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a.begin(), a.end());
    auto calc = [&](){
        vector<ll> from_right(n);
        vector<P> buf = {P(a.back(), a.back())};
        for(int i = n-2; i >= 0; i--){
            auto [l, r] = buf.back();
            ll ans = from_right[i+1];
            if(l > a[i]){
                from_right[i] = ans;
                buf.push_back({a[i], a[i]});
            }else{
                buf.pop_back();
                ans += r-l+1;
                l = a[i];
                r++;
                while(!buf.empty()){
                    auto [l0, r0] = buf.back();
                    if(l0 == r){
                        ans += r0-l0+1;
                        r = r0+1;
                        buf.pop_back();
                    }else{
                        assert(l0 > r);
                        break;
                    }
                }
                from_right[i] = ans;
                buf.push_back({l, r});
            }
            
            // for(auto [l, r]: buf) cout << l << ',' << r << ' ';
            // cout << endl;
        }
        return from_right;
    };
    auto r = calc();
    // print_vector(r);
    for(int i = 0; i < n; i++) a[i] *= -1;
    reverse(a.begin(), a.end());
    auto l = calc();
    for(int i = 0; i < n; i++) a[i] *= -1;
    reverse(a.begin(), a.end());
    reverse(l.begin(), l.end());
    ll ans = 1e18;
    for(int i = 0; i < n; i++) chmin(ans, l[i]+r[i]);
    cout << ans << endl;
}
0