結果

問題 No.921 ずんだアロー
ユーザー ateate
提出日時 2019-11-18 13:31:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,643 bytes
コンパイル時間 2,494 ms
コンパイル使用メモリ 216,592 KB
実行使用メモリ 10,712 KB
最終ジャッジ日時 2024-04-08 22:34:43
合計ジャッジ時間 4,488 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 1 ms
6,548 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 3 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 1 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 71 ms
10,464 KB
testcase_11 AC 77 ms
10,572 KB
testcase_12 AC 18 ms
6,548 KB
testcase_13 AC 56 ms
9,680 KB
testcase_14 AC 63 ms
10,084 KB
testcase_15 AC 37 ms
6,968 KB
testcase_16 AC 7 ms
6,548 KB
testcase_17 AC 65 ms
10,096 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 76 ms
10,712 KB
testcase_24 AC 30 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
using pll = pair<ll,ll>;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
//constexpr ll MOD = (998244353);
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
void dump(){cout<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cout<<h<<", ";dump(forward<Ts>(t)...);}
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}

void solve1();void solve2();
int main(){
  solve1();
  return 0;
}
template<class T>
class segtree {
private:
  ll n=1;
  T unit;
  std::vector<T> dat;
  std::function<T(T,T)> func;
public:
  segtree(){}
  segtree(std::vector<T> a,T v,std::function<T(T,T)> f){
    build(a,v,f);
  }
  void build(std::vector<T>& a,T v,std::function<T(T,T)> f){
    ll sz=a.size();
    unit=v;
    func=f;
    while(n<sz)n<<=1LL;
    dat.resize(2*n-1,unit);
    for(int i=0;i<sz;++i)dat[i+n-1]=a[i];
    for(int i=n-2;i>=0;--i){
      dat[i]=func(dat[i*2+1],dat[i*2+2]);
    }
  }
  void update(ll idx,T val){
    idx+=n-1;
    dat[idx]=val;
    while(idx){
      idx--;idx>>=1LL;
      dat[idx]=func(dat[idx*2+1],dat[idx*2+2]);
    }
  }
  T query(ll a,ll b,ll k=0,ll l=0,ll r=-1){
    if(r<0)r=n;
    if( b<=l || r<=a )return unit;
    if( a<=l && r<=b )return dat[k];
    else{
      auto left=query(a,b,2*k+1,l,(l+r)/2);
      auto right=query(a,b,2*k+2,(l+r)/2,r);
      return func(left,right);
    }
  }
};

void solve1(){

  ll n;
  cin>>n;
  vector<ll> a(n);
  cin>>a;

  vector<ll> b;
  ll cnt = 1;
  rep(i,n-1){
    if(a[i]!=a[i+1]){
      b.emplace_back(cnt);
      cnt=1;
    }else{
      cnt++;
    }
  }
  b.emplace_back(cnt);
  
  auto indexed = [](vector<ll>const& v){
    vector<pll> a(v.size());
    rep(i,v.size())a[i]={v[i],i};
    return a;
  };

  segtree<pll> seg(indexed(b),pll(0ll,0ll),[](auto a,auto b){return max(a,b);});
  ll ans = 0;
  ll m = b.size();
  while(1){
    ll val,idx;
    tie(val,idx)=seg.query(0,m);
    if(val==0)break;
    ans += val;
    seg.update(idx,pll(0ll,0ll));
    if(idx>0)seg.update(idx-1,pll(0ll,0ll));
    if(idx+1<m)seg.update(idx+1,pll(0ll,0ll));
  }
  cout<<(ans)<<endl;

}
0