結果
問題 | No.1467 Selling Cars |
ユーザー | beet |
提出日時 | 2021-09-05 12:06:08 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3,838 ms / 4,000 ms |
コード長 | 3,730 bytes |
コンパイル時間 | 2,929 ms |
コンパイル使用メモリ | 233,676 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-01 11:41:29 |
合計ジャッジ時間 | 55,816 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,704 ms
5,248 KB |
testcase_01 | AC | 1,893 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2,303 ms
5,376 KB |
testcase_04 | AC | 2,267 ms
5,376 KB |
testcase_05 | AC | 2,310 ms
5,376 KB |
testcase_06 | AC | 2,325 ms
5,376 KB |
testcase_07 | AC | 2,330 ms
5,376 KB |
testcase_08 | AC | 2,346 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 1,486 ms
5,376 KB |
testcase_12 | AC | 1,781 ms
5,376 KB |
testcase_13 | AC | 1,673 ms
5,376 KB |
testcase_14 | AC | 171 ms
5,376 KB |
testcase_15 | AC | 367 ms
5,376 KB |
testcase_16 | AC | 436 ms
5,376 KB |
testcase_17 | AC | 1,364 ms
5,376 KB |
testcase_18 | AC | 1,655 ms
5,376 KB |
testcase_19 | AC | 2,614 ms
5,376 KB |
testcase_20 | AC | 3,225 ms
5,376 KB |
testcase_21 | AC | 3,248 ms
5,376 KB |
testcase_22 | AC | 3,838 ms
5,376 KB |
testcase_23 | AC | 3,421 ms
5,376 KB |
testcase_24 | AC | 3,294 ms
5,376 KB |
testcase_25 | AC | 56 ms
5,376 KB |
testcase_26 | AC | 977 ms
5,376 KB |
testcase_27 | AC | 1,047 ms
5,376 KB |
testcase_28 | AC | 1,303 ms
5,376 KB |
testcase_29 | AC | 277 ms
5,376 KB |
testcase_30 | AC | 313 ms
5,376 KB |
testcase_31 | AC | 2 ms
5,376 KB |
testcase_32 | AC | 3 ms
5,376 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{ using P = pair<T, T>; template<template<typename> typename Comp_> struct PQ{ template<typename X> using Comp = Comp_<X>; priority_queue<P, vector<P>, Comp<P>> pq; bool empty()const{return pq.empty();} T offset; PQ():offset(0){} // 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 top() const{ auto[pos,num]=pq.top(); return P(pos+offset,num); } void pop(){pq.pop();} }; 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){ auto comp=typename From::Comp<T>(); 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); 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(a); R.shift(b); } // 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_val(T x){ T res=entire; auto vectorize=[](auto pq){ vector<P> vp; vp.reserve(pq.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; } T get_min(){return entire;} }; #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; }