結果

問題 No.1467 Selling Cars
ユーザー mugen_1337mugen_1337
提出日時 2021-04-06 11:33:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,760 bytes
コンパイル時間 2,782 ms
コンパイル使用メモリ 224,276 KB
実行使用メモリ 8,752 KB
最終ジャッジ日時 2023-08-30 22:51:50
合計ジャッジ時間 13,798 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,718 ms
4,376 KB
testcase_01 AC 1,862 ms
4,376 KB
testcase_02 AC 2 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;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#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);
  }

  void add_x_minus_a(T a,T cnt=T(1)){
    a-=offset;
    T use(0);
    while(use<cnt and not L.empty() and a<=L.top().first){
      auto[pos,num]=L.top();L.pop();

      T tmp=min(cnt-use,num);
      push(R,pos,tmp);
      push(L,a,tmp);
      push(L,pos,relu(num-tmp));
      entire+=relu(pos-a)*tmp;
      use+=tmp;
    }
    push(R,a,cnt-use);
  }

  void add_a_minus_x(T a,T cnt=T(1)){
    a-=offset;
    T use(0);
    while(use<cnt and not R.empty() and R.top().first<=a){
      auto[pos,num]=R.top();R.pop();

      T tmp=min(cnt-use,num);
      push(L,pos,tmp);
      push(R,a,tmp);
      push(R,pos,relu(num-tmp));
      entire+=relu(a-pos)*tmp;
      use+=tmp;
    }
    push(L,a,cnt-use);
  }

  // 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)<<"\n";
    }
    return 0;
}
0