結果
問題 | No.1467 Selling Cars |
ユーザー | beet |
提出日時 | 2021-04-04 22:30:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,607 ms / 4,000 ms |
コード長 | 3,552 bytes |
コンパイル時間 | 3,004 ms |
コンパイル使用メモリ | 233,616 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-09 05:24:39 |
合計ジャッジ時間 | 17,563 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,736 ms
6,812 KB |
testcase_01 | AC | 1,978 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 17 ms
6,940 KB |
testcase_04 | AC | 17 ms
6,944 KB |
testcase_05 | AC | 18 ms
6,940 KB |
testcase_06 | AC | 17 ms
6,940 KB |
testcase_07 | AC | 19 ms
6,940 KB |
testcase_08 | AC | 19 ms
6,944 KB |
testcase_09 | AC | 2 ms
6,948 KB |
testcase_10 | AC | 3 ms
6,944 KB |
testcase_11 | AC | 16 ms
6,940 KB |
testcase_12 | AC | 16 ms
6,944 KB |
testcase_13 | AC | 14 ms
6,944 KB |
testcase_14 | AC | 5 ms
6,940 KB |
testcase_15 | AC | 8 ms
6,940 KB |
testcase_16 | AC | 8 ms
6,944 KB |
testcase_17 | AC | 17 ms
6,940 KB |
testcase_18 | AC | 15 ms
6,940 KB |
testcase_19 | AC | 2,607 ms
6,940 KB |
testcase_20 | AC | 551 ms
6,940 KB |
testcase_21 | AC | 123 ms
6,940 KB |
testcase_22 | AC | 2,316 ms
6,940 KB |
testcase_23 | AC | 1,180 ms
6,940 KB |
testcase_24 | AC | 261 ms
6,940 KB |
testcase_25 | AC | 65 ms
6,940 KB |
testcase_26 | AC | 127 ms
6,944 KB |
testcase_27 | AC | 29 ms
6,944 KB |
testcase_28 | AC | 1,282 ms
6,940 KB |
testcase_29 | AC | 42 ms
6,940 KB |
testcase_30 | AC | 11 ms
6,940 KB |
testcase_31 | AC | 2 ms
6,940 KB |
testcase_32 | AC | 2 ms
6,940 KB |
ソースコード
#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>; 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);} void pushL(T pos,T num){if(num>T(0)) L.emplace(pos-offset,num);} void pushR(T pos,T num){if(num>T(0)) R.emplace(pos-offset,num);} T posL(){return L.top().first+offset;} T posR(){return R.top().first+offset;} 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.top().second; L.pop(); 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.top().second; R.pop(); 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){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(){ 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; ll last=-1; 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(); } ll ans=S.get_val(0); if(last==ans){ for(int t=k;t<=m;t++) cout<<ans<<newl; break; } cout<<(last=ans)<<newl; } return 0; }