結果
| 問題 |
No.1467 Selling Cars
|
| コンテスト | |
| ユーザー |
mugen_1337
|
| 提出日時 | 2021-04-06 12:01:34 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 3,609 bytes |
| コンパイル時間 | 9,979 ms |
| コンパイル使用メモリ | 280,276 KB |
| 最終ジャッジ日時 | 2025-01-20 12:24:16 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 30 TLE * 1 |
ソースコード
#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;
}
mugen_1337