結果

問題 No.808 Kaiten Sushi?
ユーザー addeight2addeight2
提出日時 2019-03-23 16:05:17
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,047 bytes
コンパイル時間 1,690 ms
コンパイル使用メモリ 172,480 KB
実行使用メモリ 20,132 KB
最終ジャッジ日時 2024-09-22 15:04:48
合計ジャッジ時間 7,814 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
using namespace std;
const ll MAX_N=1e5;
typedef pair<ll,ll> P;
ll N,L,X[MAX_N],Y[MAX_N];
ll f(){
	set<ll> st[2];
	REP(i,N)st[0].insert(X[i]);
	REP(i,N)st[1].insert(Y[i]);
	ll x;
	vector<P> vec;
	REP(i,N){
		vec.push_back(P(X[i],0));
	}
	REP(i,N){
		vec.push_back(P(Y[i],1));
	}
	sort(vec.begin(),vec.end());
	{
		ll inx=0;
		while(vec[inx].second==1)inx++;
		while(inx<N*2 && vec[inx].second==0)inx++;
		inx--;
		x=vec[inx].first;
	}
	ll ret=x;
	REP(i,N*2-1){
		ll typ=(i%2==0 ? 0 : 1);
		auto ite=st[typ].upper_bound(x);
		if(ite==st[typ].end()){
			ite=st[typ].begin();
		}
		ll typ2=1-typ;
		auto ite2=st[typ2].upper_bound(*ite);
		if(ite2==st[typ2].begin()){
			ite2=st[typ2].end();
		}
		ite2--;
		st[typ].erase(x);
		ll a=*ite2-x;
		if(a<0){
			a+=L;
		}
		ret+=a;
		x=*ite2;
	}
	return ret;
	
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>N>>L;
	REP(i,N)cin>>X[i];
	REP(i,N)cin>>Y[i];
	cout<<f()<<endl;
}
0