結果

問題 No.1606 Stuffed Animals Keeper
ユーザー eQe
提出日時 2025-03-15 15:00:42
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 25 ms / 3,000 ms
コード長 6,648 bytes
コンパイル時間 7,629 ms
コンパイル使用メモリ 334,312 KB
実行使用メモリ 25,856 KB
最終ジャッジ日時 2025-03-15 15:00:52
合計ジャッジ時間 8,810 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

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 fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)
#define schrodinger(p,c) (p?c:remove_cvref_t<decltype(c)>{})
#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;
using ull=unsigned long long;
using ulll=__uint128_t;
using lll=__int128_t;
istream&operator>>(istream&i,ulll&x){ull t;i>>t;x=t;return i;}
ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<<x/10)<<ll(x%10);}
istream&operator>>(istream&i,lll&x){ll t;i>>t;x=t;return i;}
ostream&operator<<(ostream&o,const lll&x){return o<<(x<0?"-":"")<<ulll(x>0?x:-x);}
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};}
const string newline{char(10)};
const string space{char(32)};
constexpr lll pow(lll x,ll n){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,n>>=1;return r;}
constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;}
constexpr ll log10_floor(auto x){return size10(x)-1;}
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...});}

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<bool is_negative=false>struct infinity{
  template<integral T>constexpr operator T()const{return pow(10,log10_floor(numeric_limits<T>::max()))*(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();}

  void emplace_front(const V&x={}){this->emplace(this->begin(),x);}

  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 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;
  }
};
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;}

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

  ll S=a.transform([&](ll e){return ll(e==0);}).sum();

  vec<pair<ll,ll>>v;
  a.emplace_front(2);
  fo(i,N+1){
    if(a[i]==2){
      v.eb(pair<ll,ll>{});
      continue;
    }
    if(a[i]==0)v.back().a++;
    if(a[i]==1)v.back().b++;
  }

  ll C=v.size();
  // dp[i][j]:i個目の区間まで見て0にすると決めた区間長の合計がjであるとき,
  //          0にすると決めた区間に含まれていた1の個数の合計の最小値
  //          = 必要な操作回数の最小値(1にすると決めた区間に含まれていた0とswapさせれば良いので)
  vec dp(C+1,S+1,(ll)oo);
  dp[0][0]=0;
  fo(i,C){
    fo(j,S+1)if(dp[i][j]!=oo){
      amin(dp[i+1][j],dp[i][j]);
      if(j+v[i].a+v[i].b<=S)amin(dp[i+1][j+v[i].a+v[i].b],dp[i][j]+v[i].b);
    }
  }
  pp(dp[C][S]!=oo?dp[C][S]:-1);
}}
0