結果

問題 No.2029 Swap Min Max Min
ユーザー eQe
提出日時 2025-03-13 15:03:46
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 8,519 bytes
コンパイル時間 7,488 ms
コンパイル使用メモリ 333,448 KB
実行使用メモリ 18,948 KB
最終ジャッジ日時 2025-03-13 15:03:59
合計ジャッジ時間 12,642 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
namespace my{
#define eb emplace_back
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define RDVL(T,n,...) vec<T>__VA_ARGS__;resizes({n},__VA_ARGS__);lin(__VA_ARGS__)
#define VL(n,...) RDVL(ll,n,__VA_ARGS__)
#define FO(n) for(ll ij=n;ij-->0;)
#define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))
#define of(i,...) for(auto[i,i##stop,i##step]=range(1,__VA_ARGS__);i>=i##stop;i+=i##step)
#define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)
#define ef(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):ranges::reverse_view(a))
#define base_operator(op,type) auto operator op(const type&v)const{auto copy=*this;return copy op##=v;}
#define single_testcase void solve();}int main(){my::io();my::solve();}namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
constexpr auto range(ll s,ll b){ll a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
constexpr auto range(ll s,ll a,ll b,ll c=1){return array{a-s,b,(1-s*2)*c};}
constexpr char newline=10;
constexpr char space=32;
constexpr auto schrodinger(bool p,char c){return string(p,c);}
constexpr auto schrodinger(bool p,auto c){return p*c;}
constexpr auto odd(auto x){return x&1;}
constexpr auto even(auto x){return~x&1;}
constexpr auto parity(auto x){return x&1;}
constexpr auto abs(auto x){return x<0?-x:x;}
bool amin(auto&a,const auto&b){return b<a?a=b,1:0;}
auto max(auto...a){return max(initializer_list<common_type_t<decltype(a)...>>{a...});}
auto min(auto...a){return min(initializer_list<common_type_t<decltype(a)...>>{a...});}

template<class A,class B>struct pair{
  A a;B b;
  pair()=default;
  pair(A a,B b):a(a),b(b){}
  pair(const std::pair<A,B>&p):a(p.first),b(p.second){}
  auto operator<=>(const pair&)const=default;
  pair operator+(const pair&p)const{return{a+p.a,b+p.b};}
  friend istream&operator>>(istream&i,pair&p){return i>>p.a>>p.b;}
  friend ostream&operator<<(ostream&o,const pair&p){return o<<p.a<<space<<p.b;}
};

template<class F=less<>>auto&sort(auto&a,F f={}){ranges::sort(a,f);return a;}
auto&unique(auto&a){sort(a).erase(ranges::unique(a).begin(),a.end());return a;}

template<bool is_negative=false>struct infinity{
  template<integral T>constexpr operator T()const{return numeric_limits<T>::max()/2*(1-is_negative*2);}
  template<floating_point T>constexpr operator T()const{return static_cast<ll>(*this);}
  template<class T>constexpr bool operator==(T x)const{return static_cast<T>(*this)==x;}
  constexpr auto operator-()const{return infinity<!is_negative>();}
  template<class A,class B>constexpr operator pair<A,B>()const{return pair<A,B>{*this,*this};}
};
constexpr infinity oo;

template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;

template<class V>concept vectorial=is_base_of_v<vector<typename remove_cvref_t<V>::value_type>,remove_cvref_t<V>>;
template<class V>constexpr int rank(){if constexpr(vectorial<V>)return rank<typename V::value_type>()+1;else return 0;}
template<class T>struct core_t_helper{using core_t=T;};
template<vectorial V>struct core_t_helper<V>{using core_t=typename core_t_helper<typename V::value_type>::core_t;};
template<class T>using core_t=core_t_helper<T>::core_t;
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>ostream&operator<<(ostream&o,const vector<V>&v){fe(v,e)o<<e<<schrodinger(&e!=&v.back(),vectorial<V>?newline:space);return o;}

template<class V>struct vec;
template<int rank,class T>struct tensor_helper{using type=vec<typename tensor_helper<rank-1,T>::type>;};
template<class T>struct tensor_helper<0,T>{using type=T;};
template<int rank,class T>using tensor=typename tensor_helper<rank,T>::type;

template<class V>struct vec:vector<V>{
  static constexpr int R=rank<vec<V>>();
  using C=core_t<V>;
  using vector<V>::vector;
  vec(const vector<V>&v){vector<V>::operator=(v);}

  vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);}
  void resizes(const auto&...a){*this=make(a...);}
  static auto make(ll n,const auto&...a){if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);else return vec<decltype(make(a...))>(n,make(a...));}

  vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;}
  vec&operator+=(const vec&u){vec&v=*this;assert(v.size()==u.size());fo(i,v.size())v[i]+=u[i];return v;}
  vec&operator-=(const vec&u){vec&v=*this;assert(v.size()==u.size());fo(i,v.size())v[i]-=u[i];return v;}
  base_operator(^,vec)
  base_operator(+,vec)
  base_operator(-,vec)

  vec&operator++(){fe(*this,e)++e;return*this;}
  vec&operator--(){fe(*this,e)--e;return*this;}

  ll size()const{return vector<V>::size();}
  auto iota()const{vec<ll>r(size());std::iota(r.begin(),r.end(),0);return r;}

  auto pop_back(){auto r=this->back();vector<V>::pop_back();return r;}

  auto lower_bound(const V&x)const{return std::lower_bound(this->begin(),this->end(),x);}
  ll arg_lower_bound(const V&x)const{return lower_bound(x)-this->begin();}

  auto scan(const auto&f)const{
    pair<C,bool>r{};
    if constexpr(!vectorial<V>)fe(*this,e)r.b?f(r.a,e),r:r={e,1};
    else fe(*this,e)if(auto s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;
    return r;
  }
  auto sum()const{return scan([](auto&a,const auto&b){a+=b;}).a;}
  auto max()const{return scan([](auto&a,auto b){a<b?a=b:0;}).a;}
  auto min()const{return scan([](auto&a,auto b){b<a?a=b:0;;}).a;}

  template<class F=less<>>auto sort(F f={})const{vec v=*this;ranges::sort(v,f);return v;}
  template<class F=less<>>auto stable_sort(F f={})const{vec v=*this;ranges::stable_sort(v,f);return v;}
  template<class F=less<>>auto sort_index(F f={})const{const vec&v=*this;return iota().stable_sort([&](ll i,ll j){return f(v[i],v[j]);});}

  auto flatten()const{if constexpr(!vectorial<V>)return*this;else{vec<C>res;fe(*this,e)res^=e.flatten();return res;}}
  auto unique()const{auto res=flatten().sort();return my::unique(res);}

  auto transform(const auto&f)const{
    tensor<R,decltype(f(C()))>res(size());
    if constexpr(vectorial<V>)fo(i,size())res[i]=(*this)[i].transform(f);
    else std::transform(this->begin(),this->end(),res.begin(),f);
    return res;
  }

  vec abs()const{return transform([](auto e){return e<0?-e:e;});}
  vec parity()const{return transform([](auto e){return e&1;});}

  auto pos_groups(C M=-1)const{
    assert(min()>=0);
    if(M==-1)M=max();
    vec<vec<ll>>res(M+1,0,0);
    fo(i,size())res[(*this)[i]].eb(i);
    return res;
  }

  auto matching_order(vec b)const{
    auto a=*this;
    assert(a.sort()==b.sort());

    ll n=size();
    auto v=(a^b).unique();
    fo(i,n){
      a[i]=v.arg_lower_bound(a[i]);
      b[i]=v.arg_lower_bound(b[i]);
    }

    vec<vec<ll>>g(v.size());
    vec<ll>res(size());
    of(i,n)g[b[i]].eb(i);
    fo(i,n)res[i]=g[a[i]].pop_back();
    return res;
  }

  ll inversion()const{
    ll res=0;
    atcoder::fenwick_tree<int>s(size());
    ef(sort_index(),i){
      res+=s.sum(0,i);
      s.add(i,1);
    }
    return res;
  }
  ll inversion(const vec&v)const{return matching_order(v).inversion();}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<tensor<sizeof...(A)-2,pack_back_t<A...>>>;
vec(ll)->vec<ll>;

template<class...A>void resizes(const array<ll,common_type_t<A...>::R+1>&s,A&...a){(apply([&](const auto&...b){a.resizes(b...); },s),...);}

void lin(auto&...a){(cin>>...>>a);}
void pp(const auto&...a){ll n=sizeof...(a);((cout<<a<<schrodinger(--n>0,space)),...);cout<<newline;}

ll median_right_index(ll n){return n/2;}

single_testcase
void solve(){
  LL(N);
  VL(N,a);--a;

  fe(a,e)e=(e>=median_right_index(N));

  vec<ll>b(N);
  fo(i,N)b[i]=!parity(i);

  ll X=median_right_index(N);
  ll Y=a.inversion(b); // 10101010 or 1010101
  if(even(N)){
    auto pos1=a.pos_groups()[1];

    // dp[i][j]:i個まで見て,00が登場済みかどうかの真偽値がjである列に1の位置を一致させるためのコストの最小値.
    vec dp(N+1,2,ll(oo));
    dp[1][0]=pos1[0];
    dp[1][1]=0;

    fo(i,1,N){
      fo(j,2)if(dp[i][j]!=oo){
        ll k=even(i)?i/2:i/2+!j;
        ll pre_x=j^odd(i);
        fo(x,2){
          if(j&&(pre_x==0&&x==0))continue;
          ll nj=j|(pre_x==0&&x==0);
          if(x==0)amin(dp[i+1][nj],dp[i][j]);
          else amin(dp[i+1][nj],dp[i][j]+abs(i-pos1[k]));
        }
      }
    }

    amin(Y,dp[N].min());
  }
  pp(X,Y);
}}
0