結果

問題 No.1467 Selling Cars
ユーザー beetbeet
提出日時 2021-04-04 21:59:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,125 bytes
コンパイル時間 2,721 ms
コンパイル使用メモリ 232,512 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2023-08-28 08:50:52
合計ジャッジ時間 15,849 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,242 ms
4,376 KB
testcase_01 AC 2,304 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename V>
V compress(V vs){
  sort(vs.begin(),vs.end());
  vs.erase(unique(vs.begin(),vs.end()),vs.end());
  return vs;
}
template<typename T>
map<T, int> dict(const vector<T> &vs){
  map<T, int> res;
  for(int i=0;i<(int)vs.size();i++)
    res[vs[i]]=i;
  return res;
}
map<char, int> dict(const string &s){
  return dict(vector<char>(s.begin(),s.end()));
}


template<typename T, typename ...Ts>
vector<T> fusion(vector<T> bs,Ts... ts){
  auto append=[&](auto vs){for(auto v:vs) bs.emplace_back(v);};
  initializer_list<int>{(void(append(ts)),0)...};
  return bs;
}

//INSERT ABOVE HERE
template<typename T>
struct Slope{
  using P = pair<T, T>;
  multiset<P> L,R;
  T offL,offR,entire;
  Slope():offL(0),offR(0),entire(0){}

  inline T relu(T x){return max<T>(0,x);}

  void pushL(T pos,T num){if(num>T(0)) L.emplace(pos-offL,num);}
  void pushR(T pos,T num){if(num>T(0)) R.emplace(pos-offR,num);}

  T posL(){return L.rbegin()->first+offL;}
  T posR(){return R.begin() ->first+offR;}

  void add_x_minus_a(T a,T cnt=T(1)){
    T use(0);
    while(use<cnt and not L.empty() and a<=posL()){
      T pos=posL(),num=L.rbegin()->second;
      L.erase(--L.end());

      T tmp=min(cnt-use,num);
      pushR(pos,tmp);
      pushL(a,tmp);
      pushL(pos,relu(num-tmp));
      entire+=relu(pos-a)*tmp;
      use+=tmp;
    }
    pushR(a,cnt-use);
  }

  void add_a_minus_x(T a,T cnt=T(1)){
    T use(0);
    while(use<cnt and not R.empty() and posR()<=a){
      T pos=posR(),num=R.begin()->second;
      R.erase(R.begin());

      T tmp=min(cnt-use,num);
      pushL(pos,tmp);
      pushR(a,tmp);
      pushR(a,relu(num-tmp));
      entire+=relu(a-pos)*tmp;
      use+=tmp;
    }
    pushL(a,cnt-use);
  }

  // f_{new}(x) =  f_{old}(x + diff)
  void shift(T diff){
    offL-=diff;
    offR-=diff;
  }

  // f_{new}(x) = min_{y<=x} f_{old}(y)
  void apply_cumulative_min(){
    R.clear();
  }

  T get_val(T x){
    T res=entire;
    for(auto[pos,num]:L) res+=relu((pos+offL)-x)*num;
    for(auto[pos,num]:R) res+=relu(x-(pos+offR))*num;
    return res;
  }
};

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int m,n;
  cin>>m>>n;
  auto as=read(m);
  auto bs=read(n);

  auto cs=compress(fusion(as,bs));
  cs.emplace(cs.begin(),0);
  auto dc=dict(cs);

  const int sz = cs.size();
  vector<int> num(sz,0);
  for(int a:as) num[dc[a]]--;

  for(int k=1;k<=m;k++){
    for(int b:bs) num[dc[b]]++;

    Slope<long long> S;
    S.add_a_minus_x(0,1e9);
    for(int i=1;i<sz;i++){
      S.add_x_minus_a(0,cs[i]-cs[i-1]);
      S.add_a_minus_x(0,cs[i]-cs[i-1]);
      S.shift(num[i]);
      S.apply_cumulative_min();
    }
    cout<<S.get_val(0)<<newl;
  }
  return 0;
}
0