結果
| 問題 |
No.1467 Selling Cars
|
| コンテスト | |
| ユーザー |
LayCurse
|
| 提出日時 | 2021-04-02 23:01:03 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,043 ms / 4,000 ms |
| コード長 | 8,730 bytes |
| コンパイル時間 | 2,828 ms |
| コンパイル使用メモリ | 217,452 KB |
| 最終ジャッジ日時 | 2025-01-20 10:01:32 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 31 |
ソースコード
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class T> struct cLtraits_identity{
using type = T;
}
;
template<class T> using cLtraits_try_make_signed =
typename conditional<
is_integral<T>::value,
make_signed<T>,
cLtraits_identity<T>
>::type;
template <class S, class T> struct cLtraits_common_type{
using tS = typename cLtraits_try_make_signed<S>::type;
using tT = typename cLtraits_try_make_signed<T>::type;
using type = typename common_type<tS,tT>::type;
}
;
void*wmem;
char memarr[96000000];
template<class S, class T> inline auto min_L(S a, T b)
-> typename cLtraits_common_type<S,T>::type{
return (typename cLtraits_common_type<S,T>::type) a <= (typename cLtraits_common_type<S,T>::type) b ? a : b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
walloc1d(arr, x2-x1, mem);
(*arr) -= x1;
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
sort(a, a+N);
}
inline int my_getchar_unlocked(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread_unlocked(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline void rd(long long &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite_unlocked(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar_unlocked(a);
}
inline void wt_L(long long x){
int s=0;
int m=0;
char f[20];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar_unlocked('-');
}
while(s--){
my_putchar_unlocked(f[s]+'0');
}
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
a[k] = aval;
b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
for(i=sz-1;i>k;i--){
d[i] = d[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
d[k] = dval;
}
int M;
int N;
long long A[2000];
long long B[2000];
int nn;
int mtt;
long long bb[4000];
long long bt[4000];
long long bf[2000];
int nx[4001];
long long dp[2001][4001];
int vis[2001][4001];
int mt[2001];
int us[4001];
int cur;
long long calc(int d, int s){
long long res = 4611686016279904256LL;
long long tmp1;
long long tmp2;
if(vis[d][s] == cur){
return dp[d][s];
}
vis[d][s] = cur;
if(M-d > nn-s){
return dp[d][s] = res;
}
if(d==M){
return dp[d][s] = 0;
}
if(0 && abs(A[d] - bb[s]) > bf[d]){
tmp1 = 4611686016279904256LL;
tmp2 = calc(d, nx[s]);
}
else{
tmp1 = calc(d+1, s+1) + abs(A[d] - bb[s]);
tmp2 = calc(d, nx[s]);
}
res =min_L(tmp1, tmp2);
return dp[d][s] = res;
}
long long solve(int d, int s){
long long res;
long long tmp1;
long long tmp2;
res = calc(d,s);
if(d==M){
return 0;
}
if(0 && abs(A[d] - bb[s]) > bf[d]){
tmp1 = 4611686016279904256LL;
tmp2 = calc(d, nx[s]);
}
else{
tmp1 = calc(d+1, s+1) + abs(A[d] - bb[s]);
tmp2 = calc(d, nx[s]);
}
if(tmp1 <= tmp2){
mt[d] = s;
solve(d+1, s+1);
}
else{
solve(d, nx[s]);
}
return res;
}
int main(){
int m;
wmem = memarr;
long long res;
long long pre = 4611686016279904256LL;
rd(M);
rd(N);
{
int KL2GvlyY;
for(KL2GvlyY=(0);KL2GvlyY<(M);KL2GvlyY++){
rd(A[KL2GvlyY]);
}
}
{
int cTE1_r3A;
for(cTE1_r3A=(0);cTE1_r3A<(N);cTE1_r3A++){
rd(B[cTE1_r3A]);
}
}
sortA_L(M,A);
sortA_L(N,B);
for(m=(0);m<(M);m++){
int i;
if(m==0){
int i;
for(i=(0);i<(N);i++){
bf[i] = 4611686016279904256LL;
}
for(i=(0);i<(N);i++){
arrInsert(nn, nn, bb, B[i]);
}
}
else{
int i;
for(i=(0);i<(N);i++){
bf[i] = abs(A[i] - bb[mt[i]]);
}
for(i=(0);i<(nn);i++){
us[i] = 0;
}
for(i=(0);i<(N);i++){
us[mt[i]] = 1;
}
mtt = 0;
for(i=(0);i<(nn);i++){
if(us[i]){
bt[mtt++] = bb[i];
}
}
nn = 0;
for(i=(0);i<(mtt);i++){
if(nn==0 || bb[nn-1]!=bt[i]){
bb[nn++] = bt[i];
}
bb[nn++] = bt[i];
}
}
for(i=(nn)-1;i>=(0);i--){
nx[i] = i + 1;
if(i + 1 < nn && bb[i] == bb[i+1]){
nx[i] = nx[i+1];
}
}
cur++;
res = solve(0, 0);
wt_L(res);
wt_L('\n');
if(res == pre){
m++;
while(m < M){
wt_L(res);
wt_L('\n');
m++;
}
break;
}
pre = res;
}
return 0;
}
// cLay version 20210328-1 [beta]
// --- original code ---
// int M, N; ll A[2000], B[];
//
// int nn, mtt; ll bb[4000], bt[4000], bf[2000]; int nx[4001];
// ll dp[2001][4001]; int vis[2001][4001], mt[2001], us[4001], cur;
//
// ll calc(int d, int s){
// ll res = ll_inf, tmp1, tmp2;
//
// if(vis[d][s] == cur) return dp[d][s];
// vis[d][s] = cur;
//
// if(M-d > nn-s) return dp[d][s] = res;
// if(d==M) return dp[d][s] = 0;
//
// if(0 && abs(A[d] - bb[s]) > bf[d]){
// tmp1 = ll_inf;
// tmp2 = calc(d, nx[s]);
// } else {
// tmp1 = calc(d+1, s+1) + abs(A[d] - bb[s]);
// tmp2 = calc(d, nx[s]);
// }
//
// res = min(tmp1, tmp2);
//
// return dp[d][s] = res;
// }
//
// ll solve(int d, int s){
// ll res, tmp1, tmp2;
// res = calc(d,s);
//
// if(d==M) return 0;
//
// if(0 && abs(A[d] - bb[s]) > bf[d]){
// tmp1 = ll_inf;
// tmp2 = calc(d, nx[s]);
// } else {
// tmp1 = calc(d+1, s+1) + abs(A[d] - bb[s]);
// tmp2 = calc(d, nx[s]);
// }
//
// if(tmp1 <= tmp2){
// mt[d] = s;
// solve(d+1, s+1);
// } else {
// solve(d, nx[s]);
// }
//
// return res;
// }
//
// {
// ll res, pre = ll_inf;
// rd(M,N,A(M),B(N));
// sortA(M,A);
// sortA(N,B);
//
// rep(m,M){
// if(m==0){
// rep(i,N) bf[i] = ll_inf;
// rep(i,N) arrInsert(nn, nn, bb, B[i]);
// } else {
// rep(i,N) bf[i] = abs(A[i] - bb[mt[i]]);
// rep(i,nn) us[i] = 0;
// rep(i,N) us[mt[i]] = 1;
// mtt = 0;
// rep(i,nn) if(us[i]) bt[mtt++] = bb[i];
// nn = 0;
// rep(i,mtt){
// if(nn==0 || bb[nn-1]!=bt[i]) bb[nn++] = bt[i];
// bb[nn++] = bt[i];
// }
// }
// rrep(i,nn){
// nx[i] = i + 1;
// if(i + 1 < nn && bb[i] == bb[i+1]) nx[i] = nx[i+1];
// }
// cur++;
// res = solve(0, 0);
// wt(res);
// if(res == pre){
// m++;
// while(m < M) wt(res), m++;
// break;
// }
// pre = res;
// }
// }
LayCurse