結果
| 問題 |
No.1467 Selling Cars
|
| コンテスト | |
| ユーザー |
mugen_1337
|
| 提出日時 | 2021-04-08 12:44:13 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 4,553 bytes |
| コンパイル時間 | 2,519 ms |
| コンパイル使用メモリ | 211,016 KB |
| 最終ジャッジ日時 | 2025-01-20 13:01:37 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
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;
}
// https://yukicoder.me/submissions/644671
// https://ei1333.github.io/library/structure/others/slope-queue.cpp
// https://maspypy.com/slope-trick-1-%e8%a7%a3%e8%aa%ac%e7%b7%a8
// ex)
// f(x) <- min_{y<=x+k} f(y)
// 1. f.shift(k)
// 2. f.apply_cumulative_min()
template<typename T>
struct Slope{
// private:
using P=pair<T,T>;
priority_queue<P,vector<P>,less<P>> L;
priority_queue<P,vector<P>,greater<P>> R;
T add_L,add_R,min_f;
void push_R(T a,T cnt){if(cnt>0) R.emplace(a-add_R,cnt);}
void push_L(T a,T cnt){if(cnt>0) L.emplace(a-add_L,cnt);}
P top_R(){return P(R.top().first+add_R,R.top().second);}
P top_L(){return P(L.top().first+add_L,L.top().second);}
P pop_R(){
P ret=top_R();
R.pop();
return ret;
}
P pop_L(){
P ret=top_L();
L.pop();
return ret;
}
public:
Slope():add_L(0),add_R(0),min_f(0){}
void add_all(T a){min_f+=a;}
void add_x_minus_a(T a,T cnt=1){
while(cnt>0 and !L.empty() and a<top_L().first){
auto [pos,num]=pop_L();
T mv=std::min(cnt,num);
push_R(pos,mv);
push_L(pos,num-mv);
push_L(a,mv);
min_f+=(pos-a)*mv;
cnt-=mv;
}
push_R(a,cnt);
}
void add_a_minus_x(T a,T cnt=1){
while(cnt>0 and !R.empty() and a>top_R().first){
auto [pos,num]=pop_R();
T mv=std::min(cnt,num);
push_L(pos,mv);
push_R(pos,num-mv);
push_R(a,mv);
min_f+=(a-pos)*mv;
cnt-=mv;
}
push_L(a,cnt);
}
void add_abs(T a,T cnt=1){
add_a_minus_x(a,cnt);
add_x_minus_a(a,cnt);
}
void clear_right(){while(!R.empty()) R.pop();}
void clear_left(){ while(!L.empty()) L.pop();}
// f <- min{x-b<=y<=x-a} f(y)
void shift(T a,T b){
assert(a<=b);
add_L+=a,add_R+=b;
}
// f(x) <- f(x-a)
void shift(T a){shift(a,a);}
// get value O(N)
T operator()(T x){
T ret=min_f;
auto vec=[](auto pq,T shift){
vector<P> v;
while(!pq.empty()){
v.emplace_back(pq.top().first+shift,pq.top().second);
pq.pop();
}
return v;
};
for(auto [pos,num]:vec(L,add_L)) ret+=max(pos-x,T(0))*num;
for(auto [pos,num]:vec(R,add_R)) ret+=max(x-pos,T(0))*num;
return ret;
}
T min(){return min_f;}
pair<T,T> argmin(){return make_pair(P(top_L().first,top_R().first));}
};
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.clear_right();
f.add_x_minus_a(0,d);
f.add_a_minus_x(0,d);
}else{
f.shift(-k);
f.clear_right();
f.add_x_minus_a(0,d);
f.add_a_minus_x(0,d);
}
}
cout<<f(0)<<endl;
}
return 0;
}
mugen_1337