結果

問題 No.808 Kaiten Sushi?
ユーザー heno239
提出日時 2019-03-22 21:52:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 467 ms / 2,000 ms
コード長 2,248 bytes
コンパイル時間 1,178 ms
コンパイル使用メモリ 118,564 KB
実行使用メモリ 23,936 KB
最終ジャッジ日時 2024-09-19 05:15:07
合計ジャッジ時間 10,446 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = 1e+14;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
int n; ll l;
ll x[1 << 17], y[1 << 17];
ll dp[1 << 17];

set<ll> sx,sy;
void solve() {
	cin >> n >> l;
	rep(i, n) {
		cin >> x[i]; sx.insert(x[i]);
	}
	rep(i, n) {
		sx.insert(x[i] + l);
	}
	rep(i, n)cin >> y[i];
	rep(i, n) {
		sy.insert(y[i]); sy.insert(y[i] + l);
	}
	ll ans = 0; ll loc = 0;
	rep(i, n) {
		sx.insert(loc);
		auto itr = sx.find(loc);
		itr++;
		ll nex = *itr;
		ans += nex - loc;
		if (nex >= l)nex -= l;
		sx.erase(nex); sx.erase(nex + l); sx.erase(loc);
		loc = nex;
		//cout << ans << " a "<<loc<<endl;

		if (i == n - 1) {
			sy.insert(loc);
			itr = sy.find(loc);
			itr++;
			nex = *itr;
			ans += nex - loc;
			break;
		}
		sy.insert(loc);
		itr = sy.find(loc);
		itr++;
		nex = *itr; sy.erase(loc);

		if (nex >= l) {
			nex -= l; ans += l;
		}
		sx.insert(nex);
		auto itr2 = sx.find(nex);
		itr2++;
		//cout << "!!" << endl;
		ll nenex = *itr2; sx.erase(nex);
		//cout << "!?" << endl;
		if (nenex < l) {
			nenex += l; ans -= l;
		}
		sy.insert(nenex);
		auto itr3 = sy.find(nenex);
		//cout << "?" << endl;
		itr3--;
		nex = *itr3; sy.erase(nenex);

		ans += nex - loc;
		if (nex >= l)nex -= l;
		sy.erase(nex); sy.erase(nex + l);
		loc = nex;
		//cout << ans << " a " << loc << endl;
	}
	/*if (ans < l) {
		if (y[n - 1] > ans) {
			ans += y[n - 1];
		}
	}*/
	cout << ans << endl;
}
int main() {
	solve();
	//stop
	return 0;
}
0