結果

問題 No.1467 Selling Cars
ユーザー mugen_1337mugen_1337
提出日時 2021-04-06 12:01:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,841 ms / 4,000 ms
コード長 3,609 bytes
コンパイル時間 2,677 ms
コンパイル使用メモリ 220,068 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-30 23:19:55
合計ジャッジ時間 46,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,722 ms
4,376 KB
testcase_01 AC 1,901 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2,338 ms
4,380 KB
testcase_04 AC 2,303 ms
4,376 KB
testcase_05 AC 2,343 ms
4,380 KB
testcase_06 AC 2,340 ms
4,376 KB
testcase_07 AC 2,345 ms
4,376 KB
testcase_08 AC 2,368 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1,486 ms
4,380 KB
testcase_12 AC 1,768 ms
4,380 KB
testcase_13 AC 1,675 ms
4,376 KB
testcase_14 AC 176 ms
4,380 KB
testcase_15 AC 347 ms
4,376 KB
testcase_16 AC 431 ms
4,376 KB
testcase_17 AC 1,358 ms
4,376 KB
testcase_18 AC 1,662 ms
4,376 KB
testcase_19 AC 2,630 ms
4,376 KB
testcase_20 AC 3,264 ms
4,380 KB
testcase_21 AC 3,250 ms
4,380 KB
testcase_22 AC 3,841 ms
4,376 KB
testcase_23 AC 3,438 ms
4,380 KB
testcase_24 AC 3,303 ms
4,380 KB
testcase_25 AC 54 ms
4,376 KB
testcase_26 AC 966 ms
4,376 KB
testcase_27 AC 1,037 ms
4,376 KB
testcase_28 AC 1,299 ms
4,504 KB
testcase_29 AC 269 ms
4,380 KB
testcase_30 AC 306 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 998244353
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}


/*
え,もしかしてbeetさんのこれ,ヤバイ?
試させてください.なんでもします
*/
template<typename T>
struct Slope{
  using P = pair<T, T>;
  priority_queue<P, vector<P>, less<P>> L;
  priority_queue<P, vector<P>, greater<P>> R;
  T offset,entire;
  Slope():offset(0),entire(0){}

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

  template<typename PQ>
  inline void push(PQ &pq,T pos,T num){
    if(num!=T(0)) pq.emplace(pos,num);
  }

  template<typename From,typename To,typename Compare>
  void fix(T a,T cnt,From &from,To &to,Compare comp){
    T use(0);
    while(use<cnt and not from.empty() and comp(a,from.top().first)){
      auto[pos,num]=from.top();from.pop();
      T tmp=min(cnt-use,num);
      push(to,pos,tmp);
      push(from,pos,relu(num-tmp));
      push(from,a,tmp);
      entire+=max(a-pos,pos-a)*tmp;
      use+=tmp;
    }
    push(to,a,cnt-use);
  }

  void add_x_minus_a(T a,T cnt=T(1)){
    a-=offset;
    fix(a,cnt,L,R,[](T x,T y){return x<y;});
  }

  void add_a_minus_x(T a,T cnt=T(1)){
    a-=offset;
    fix(a,cnt,R,L,[](T x,T y){return x>y;});
  }

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

  // f_{new}(x) = min_{y<=x} f_{old}(y)
  void apply_cumulative_min(){
    while(!R.empty()) R.pop();
  }

  T get_val(T x){
    x-=offset;
    T res=entire;
    auto vectorize=[](auto pq){
      vector<P> vp;
      vp.reserve(pq.size());
      while(!pq.empty()) vp.emplace_back(pq.top()),pq.pop();
      return vp;
    };
    for(auto[pos,num]:vectorize(L)) res+=relu(pos-x)*num;
    for(auto[pos,num]:vectorize(R)) res+=relu(x-pos)*num;
    return res;
  }
};



signed main(){
    int m,n;cin>>m>>n;
    vector<ll> a(m),b(n);
    cin>>a>>b;

    struct E{
        ll val;
        bool a;
        E(ll val,bool a):val(val),a(a){}
    };

    vector<E> v;
    rep(i,m) v.emplace_back(a[i],true);
    rep(i,n) v.emplace_back(b[i],false);

    sort(ALL(v),[](E l,E r){return l.val<r.val;});

    for(int k=1;k<=m;k++){
        Slope<ll> f;
        f.add_x_minus_a(0,1000000000);
        f.add_a_minus_x(0,1000000000);

        ll pre=-100000;
        for(int i=0;i<(int)v.size();i++){
            ll nxt=(i==(int)v.size()?INF:v[i+1].val);
            ll d=nxt-v[i].val;
            bool is_a=v[i].a;
            if(is_a){
                f.shift(-1);
                f.apply_cumulative_min();
                f.add_x_minus_a(0,d);
                f.add_a_minus_x(0,d);
            }else{
                f.shift(k);
                f.apply_cumulative_min();
                f.add_x_minus_a(0,d);
                f.add_a_minus_x(0,d);
            }
        }
        cout<<f.get_val(0)<<endl;
    }
    return 0;
}
0