結果

問題 No.1371 交換門松列・松
ユーザー carrot46carrot46
提出日時 2021-02-09 16:39:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 4,000 ms
コード長 2,881 bytes
コンパイル時間 1,799 ms
コンパイル使用メモリ 177,640 KB
実行使用メモリ 8,716 KB
最終ジャッジ日時 2023-09-21 03:54:14
合計ジャッジ時間 5,318 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 43 ms
8,716 KB
testcase_19 AC 42 ms
8,560 KB
testcase_20 AC 44 ms
8,628 KB
testcase_21 AC 44 ms
8,516 KB
testcase_22 AC 44 ms
8,540 KB
testcase_23 AC 68 ms
8,596 KB
testcase_24 AC 69 ms
8,632 KB
testcase_25 AC 69 ms
8,588 KB
testcase_26 AC 69 ms
8,568 KB
testcase_27 AC 69 ms
8,588 KB
testcase_28 AC 69 ms
8,592 KB
testcase_29 AC 69 ms
8,520 KB
testcase_30 AC 69 ms
8,684 KB
testcase_31 AC 69 ms
8,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

template <class T> class BIT {
    //T has operator "+=" and can be initialized with 0.
    unsigned int n;
    vector<T> bitree;
public:
    BIT(unsigned long _n) : bitree(_n + 1, 0) {
        n = _n;
    }

    void add(int id, T x) {
        ++id;
        while (id <= n) {
            bitree[id] += x;
            id += id & -id;
        }
    }

    T sum(int id) {
        ++id;
        T temp(0);
        while (id > 0) {
            temp += bitree[id];
            id -= id & -id;
        }
        return temp;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N;
    vec a(N + 2);
    vector<P> low, high;
    reps(i, 1, N + 1) cin>>a[i];
    a[0] = (a[1] > a[2] ? 0 : N + 1);
    a[N+1] = (a[N-1] < a[N] ? 0 : N + 1);
    reps(i, 1, N + 1){
        if(a[i] < a[i+1]) low.emplace_back(a[i], min(a[i-1], a[i+1]));
        else high.emplace_back(a[i], max(a[i-1], a[i+1]));
    }
    ll res = 0;

    sort(ALL(low));
    {
        BIT<int> bit(N + 10);
        for (P p : low) {
            res += bit.sum(N + 5) - bit.sum(p.first);
            bit.add(p.second, 1);
        }
    }

    sort(ALL(high)); reverse(ALL(high));
    {
        BIT<int> bit(N + 10);
        for(P p : high){
            res += bit.sum(p.first);
            bit.add(p.second, 1);
        }
    }

    reverse(ALL(high));
    BIT<int> bit(N + 10);
    for(P &p : low) {
        swap(p.first, p.second);
        bit.add(p.second, 1);
    }
    sort(ALL(low));
    int lsz = low.size(), hsz = high.size();
    int i{}, j{};
    const P inf(INF, INF);
    while(i < lsz || j < hsz){
        P p = inf, q = inf;
        if(i < lsz) p = low[i];
        if(j < hsz) q = high[j];
        if(p.first < q.first){
            bit.add(p.second, -1);
            ++i;
        }else{
            //cout<<i<<' '<<j<<' '<< bit.sum(N + 5) - bit.sum(q.second)<<endl;
            res += bit.sum(N + 5) - bit.sum(q.second);
            ++j;
        }
    }
    cout<<res<<endl;
}
0