結果
問題 | No.1467 Selling Cars |
ユーザー | beet |
提出日時 | 2021-09-05 12:21:45 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3,970 ms / 4,000 ms |
コード長 | 3,757 bytes |
コンパイル時間 | 2,892 ms |
コンパイル使用メモリ | 235,212 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-01 11:42:50 |
合計ジャッジ時間 | 57,217 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,757 ms
6,812 KB |
testcase_01 | AC | 1,968 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2,353 ms
6,940 KB |
testcase_04 | AC | 2,319 ms
6,940 KB |
testcase_05 | AC | 2,350 ms
6,944 KB |
testcase_06 | AC | 2,367 ms
6,944 KB |
testcase_07 | AC | 2,354 ms
6,940 KB |
testcase_08 | AC | 2,386 ms
6,944 KB |
testcase_09 | AC | 3 ms
6,940 KB |
testcase_10 | AC | 3 ms
6,944 KB |
testcase_11 | AC | 1,513 ms
6,944 KB |
testcase_12 | AC | 1,800 ms
6,940 KB |
testcase_13 | AC | 1,711 ms
6,944 KB |
testcase_14 | AC | 178 ms
6,940 KB |
testcase_15 | AC | 370 ms
6,944 KB |
testcase_16 | AC | 445 ms
6,940 KB |
testcase_17 | AC | 1,398 ms
6,940 KB |
testcase_18 | AC | 1,681 ms
6,944 KB |
testcase_19 | AC | 2,698 ms
6,944 KB |
testcase_20 | AC | 3,363 ms
6,940 KB |
testcase_21 | AC | 3,350 ms
6,940 KB |
testcase_22 | AC | 3,970 ms
6,940 KB |
testcase_23 | AC | 3,534 ms
6,944 KB |
testcase_24 | AC | 3,404 ms
6,944 KB |
testcase_25 | AC | 57 ms
6,940 KB |
testcase_26 | AC | 1,007 ms
6,940 KB |
testcase_27 | AC | 1,065 ms
6,944 KB |
testcase_28 | AC | 1,348 ms
6,940 KB |
testcase_29 | AC | 286 ms
6,944 KB |
testcase_30 | AC | 315 ms
6,944 KB |
testcase_31 | AC | 2 ms
6,940 KB |
testcase_32 | AC | 2 ms
6,944 KB |
ソースコード
#define PROBLEM "https://yukicoder.me/problems/5061" #include <bits/stdc++.h> using namespace std; #define call_from_test 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 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; } 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> vector<T> compressed(vector<T> vs){ auto dc=dict(compress(vs)); for(auto &v:vs) v=dc[v]; return vs; } // https://maspypy.com/slope-trick-1-%e8%a7%a3%e8%aa%ac%e7%b7%a8 template<typename T> struct Slope{ template<template<typename> typename Comp_> struct PQ{ template<typename X> using Comp = Comp_<X>; inline static constexpr Comp<T> comp{}; using P = pair<T, T>; priority_queue<P, vector<P>, Comp<P>> pq; bool empty()const{return pq.empty();} T offset; PQ():offset(0){} bool compare(T a){return comp(a,pq.top().first+offset);} // f_{new}(x) = f_{old}(x - diff) void shift(T diff){offset+=diff;} void push(T pos,T num){ if(num!=T(0)) pq.emplace(pos-offset,num); } P pop(){ auto[pos,num]=pq.top();pq.pop(); return P(pos+offset,num); } }; PQ<less> L; PQ<greater> R; T entire; Slope():entire(0){} inline T relu(T x){return max<T>(0,x);} template<typename From,typename To> void fix(T a,T cnt,From &from,To &to){ T use(0); while(use<cnt and not from.empty() and from.compare(a)){ auto[pos,num]=from.pop(); T tmp=min(cnt-use,num); to.push(pos,tmp); from.push(pos,relu(num-tmp)); from.push(a,tmp); entire+=max(a-pos,pos-a)*tmp; use+=tmp; } to.push(a,cnt-use); } // _/ void add_x_minus_a(T a,T cnt=T(1)){ fix(a,cnt,L,R); } // \_ void add_a_minus_x(T a,T cnt=T(1)){ fix(a,cnt,R,L); } // f_{new}(x) = \min_{x-b<=y<=x-a} f_{old}(y) void shift(T a,T b){ assert(a<=b); L.shift(b); R.shift(a); } // f_{new}(x) = f_{old}(x - a) void shift(T a){shift(a,a);} // f_{new}(x) = min_{y<=x} f_{old}(y) void apply_cumulative_min(){ while(!R.empty()) R.pop(); } T get_min(){return entire;} T get_val(T x){ T res=entire; auto vectorize=[](auto pq){ vector<pair<T, T>> vp; vp.reserve(pq.pq.size()); while(!pq.empty()) vp.emplace_back(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; } }; #undef call_from_test #ifdef SANITIZE #define IGNORE #endif 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)); auto dc=dict(cs); for(int &a:as) a=dc[a]; for(int &b:bs) b=dc[b]; const int sz = cs.size(); vector<int> num(sz,0); for(int a:as) num[a]--; using ll = long long; for(int k=1;k<=m;k++){ for(int b:bs) num[b]++; int pos=0; Slope<ll> S; S.add_a_minus_x(pos,1e9); for(int i=0;i<sz;i++){ if(num[i]==0) continue; S.add_a_minus_x(0,cs[i]-pos); S.add_x_minus_a(0,cs[i]-pos); pos=cs[i]; S.shift(-num[i]); S.apply_cumulative_min(); } cout<<S.get_val(0)<<'\n'; } return 0; }