제출 #1343368

#제출 시각아이디문제언어결과실행 시간메모리
1343368thelegendary08Explosives (NOI24_explosives)C++17
16 / 100
1 ms580 KiB
#include<bits/stdc++.h>
// #include<atcoder/modint>
// #include<atcoder/convolution>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define rs resize
#define ft front()
#define bk back()
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define r0f(i,n) for(int i = n-1; i >= 0; i--)
#define R0F(i,k,n) for(int i = n-1; i >= k; i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SUM(v) (accumulate(all(v), 0LL))
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define pout(a) cout<<a.first<<' '<<a.second<<'\n'
#define vout(v) for(auto u : v){cout<<u<<' ';} cout<<endl
#define dout(a) cout<<a<<' '<<#a<<endl
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<endl
#define YN(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
#define yn(x); if(x){cout<<"Yes"<<'\n';}else{cout<<"No"<<'\n';}
#define p2(x) (1LL << (x))
#define pct(x) (__builtin_popcountll((x)))
#define ist(x,y) (((x) >> (y)) & 1)
template<class T, class U> bool ckmin(T& a, const U& b) { return (T)b < a ? a = (T)b, 1 : 0; } 
template<class T, class U> bool ckmax(T& a, const U& b) { return a < (T)b ? a = (T)b, 1 : 0; } 
const int leg = 1e9 + 7;
const int mod = 998244353;
using namespace std;
// using namespace atcoder; 
// using mint = modint998244353;
// using mint = modint1000000007;
const int mxp = 1e4 + 5; 
// mint fact[mxn], inv[mxn];
// mint C(int n, int r){return fact[n] * inv[n-r] * inv[r];}
// mint snb(int n, int r){return C(n + r, n);}
vi g; 
struct info{
	int a,b,c;
	bool operator <(const info &x) const{
		return a < x.a;
	}
};
void solve(vi a, vi b, int c){
	bool f = 0; int n = a.size(); if(b[0]<a[0]){f0r(i,n)a[i]=-a[i],b[i]=-b[i]; reverse(all(a)); reverse(all(b)); f=1;}
	int dist[n][c+1][2]; info from[n][c+1][2]; 
	f0r(i,n)f0r(j,c+1)f0r(k,2)dist[i][j][k]=4e18; dist[0][1][0]=0; from[0][1][0]={-1,-1,-1};
	priority_queue<pair<int,info>>q; q.push(mp(0,info{0,1,0}));
	while(!q.empty()){
		auto [i,s,t] = q.top().S; q.pop(); if(t==0){
			//i-s+1,i-s+2,...,i
			if(i<n-1&&s<c){
				if(dist[i+1][s+1][0] > dist[i][s][t] + a[i+1]-a[i]){
					dist[i+1][s+1][0] = dist[i][s][t] + a[i+1]-a[i]; from[i+1][s+1][0]={i,s,t};
					q.push(mp(-dist[i+1][s+1][0],info{i+1,s+1,0}));
				}
			}
			if(dist[i-s+1][s-1][1] > dist[i][s][t] + abs(a[i]-b[i-s+1])){
				dist[i-s+1][s-1][1] = dist[i][s][t] + abs(a[i]-b[i-s+1]); from[i-s+1][s-1][1]={i,s,t};
				q.push(mp(-dist[i-s+1][s-1][1],info{i-s+1,s-1,1}));
			}
		}
		else{
			//i+1,i+2,...,i+s
			if(i+s+1<n && dist[i+s+1][s+1][0] > dist[i][s][t] + abs(b[i] - a[i+s+1]) * (s != 0)){
				dist[i+s+1][s+1][0] = dist[i][s][t] + abs(b[i] - a[i+s+1]) * (s != 0); from[i+s+1][s+1][0]={i,s,t};
				q.push(mp(-dist[i+s+1][s+1][0],info{i+s+1,s+1,0}));
			}
			if(s>0 && dist[i+1][s-1][1] > dist[i][s][t] + abs(b[i] - b[i+1])){
				dist[i+1][s-1][1] = dist[i][s][t] + abs(b[i] - b[i+1]); from[i+1][s-1][1]={i,s,t};
				q.push(mp(-dist[i+1][s-1][1],info{i+1,s-1,1}));
			}
		}
	}
	// dout(dist[n-1][0][1]);
	// vout(a); vout(b);
	info cur = {n-1,0,1}; vi tmp; while(cur.a!=-1){
		if(cur.c)tmp.pb(b[cur.a]); else tmp.pb(a[cur.a]); cur = from[cur.a][cur.b][cur.c]; 
	} reverse(all(tmp)); if(f){for(auto &u : tmp)u=-u;} for(auto u : tmp)g.pb(u); 
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	//ifstream cin(".in");
	//ofstream cout(".out");
	// fact[0]=1;FOR(i,1,mxn)fact[i]=fact[i-1]*i;inv[mxn-1]=fact[mxn-1].inv();r0f(i,mxn-1)inv[i]=inv[i+1]*(i+1);
	in2(n,c); vin(a,n); vin(b,n); sort(all(a)); sort(all(b)); vi d(mxp); f0r(i,n){
		if(a[i] < b[i])d[a[i]]++, d[b[i]]--; else d[b[i]]++,d[a[i]]--;  
	} 
	int ans = 0, cur = 0; f0r(i,mxp){
		cur += d[i]; ans += (cur + c - 1) / c; 
	} out(ans);
	vi x, y; int p1 = 0, p2 = 0; while(p1 < n || p2 < n){
		if(p1 == n)y.pb(b[p2]),p2++;
		else if(p2==n)x.pb(a[p1]),p1++;
		else if(a[p1] < b[p2])x.pb(a[p1]),p1++;
		else y.pb(b[p2]),p2++; 
		if(x.size()==y.size()){
			solve(x,y,c); x.clear();y.clear();
		}
	}
	vout(g); 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...