結果

問題 No.2304 Distinct Elements
ユーザー milanis48663220milanis48663220
提出日時 2023-05-12 23:16:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,799 bytes
コンパイル時間 1,544 ms
コンパイル使用メモリ 124,324 KB
実行使用メモリ 15,292 KB
最終ジャッジ日時 2023-08-19 06:08:20
合計ジャッジ時間 5,837 ms
ジャッジサーバーID
(参考情報)
judge15 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 30 ms
7,856 KB
testcase_05 AC 30 ms
7,704 KB
testcase_06 AC 30 ms
7,732 KB
testcase_07 AC 30 ms
7,904 KB
testcase_08 AC 30 ms
7,852 KB
testcase_09 AC 56 ms
15,072 KB
testcase_10 AC 56 ms
15,148 KB
testcase_11 AC 55 ms
15,156 KB
testcase_12 AC 54 ms
15,152 KB
testcase_13 AC 55 ms
15,292 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 1 ms
4,384 KB
testcase_20 WA -
testcase_21 AC 2 ms
4,380 KB
testcase_22 WA -
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 26 ms
6,416 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 22 ms
6,040 KB
testcase_32 AC 43 ms
13,420 KB
testcase_33 AC 5 ms
4,388 KB
testcase_34 AC 45 ms
13,680 KB
testcase_35 AC 50 ms
14,896 KB
testcase_36 AC 5 ms
4,380 KB
testcase_37 AC 11 ms
4,384 KB
testcase_38 AC 33 ms
7,700 KB
testcase_39 AC 34 ms
7,700 KB
testcase_40 AC 17 ms
6,216 KB
testcase_41 AC 10 ms
4,532 KB
testcase_42 AC 22 ms
7,820 KB
testcase_43 AC 35 ms
7,988 KB
testcase_44 AC 41 ms
13,656 KB
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 2 ms
4,504 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 30 ms
7,832 KB
testcase_50 AC 33 ms
7,700 KB
testcase_51 AC 33 ms
7,732 KB
testcase_52 WA -
testcase_53 AC 35 ms
15,276 KB
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 AC 31 ms
9,676 KB
testcase_58 AC 31 ms
9,604 KB
testcase_59 AC 26 ms
9,676 KB
testcase_60 AC 2 ms
4,384 KB
testcase_61 AC 2 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{
                        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