結果

問題 No.808 Kaiten Sushi?
ユーザー yyyuuuummmmaaa1yyyuuuummmmaaa1
提出日時 2019-07-24 03:57:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 401 ms / 2,000 ms
コード長 2,322 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 171,040 KB
実行使用メモリ 31,620 KB
最終ジャッジ日時 2023-09-09 09:53:53
合計ジャッジ時間 11,905 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 91 ms
14,124 KB
testcase_24 AC 71 ms
12,132 KB
testcase_25 AC 79 ms
13,440 KB
testcase_26 AC 71 ms
12,932 KB
testcase_27 AC 254 ms
26,516 KB
testcase_28 AC 67 ms
10,408 KB
testcase_29 AC 239 ms
25,452 KB
testcase_30 AC 144 ms
17,644 KB
testcase_31 AC 279 ms
27,652 KB
testcase_32 AC 319 ms
30,968 KB
testcase_33 AC 142 ms
17,568 KB
testcase_34 AC 172 ms
19,756 KB
testcase_35 AC 210 ms
23,224 KB
testcase_36 AC 146 ms
17,432 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 347 ms
28,920 KB
testcase_40 AC 70 ms
9,648 KB
testcase_41 AC 68 ms
10,288 KB
testcase_42 AC 258 ms
24,600 KB
testcase_43 AC 96 ms
12,664 KB
testcase_44 AC 192 ms
19,696 KB
testcase_45 AC 99 ms
12,732 KB
testcase_46 AC 48 ms
8,504 KB
testcase_47 AC 401 ms
31,620 KB
testcase_48 AC 392 ms
31,528 KB
testcase_49 AC 395 ms
31,424 KB
testcase_50 AC 400 ms
31,428 KB
testcase_51 AC 396 ms
31,424 KB
testcase_52 AC 186 ms
23,484 KB
testcase_53 AC 260 ms
30,856 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 1 ms
4,380 KB
testcase_57 AC 77 ms
13,052 KB
testcase_58 AC 136 ms
18,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;


vector<int>solve2(const vector<int>&v,int from){
	vector<int>que;
	map<int,int>mp;
	for(int x=from;x<v.size();++x){
		if(mp.find(v[x])==mp.end()){
			que.emplace_back(v[x]);
			mp[v[x]]=que.size()-1;
		}else{
			while(true){
				int aback=que.back();
				
				mp.erase(aback);
				que.pop_back();
				if(aback==v[x])break;
			}
		}
	}
	return que;
}
vector<int> solve(int N,long long int K,vector<int>v,int start){
	
	vector<int>rs(N);

	{
		map<int,int>mp;
		for(int i=N-1;i>=0;--i){
			mp[v[i]]=N+i;
		}
		for(int i=N-1;i>=0;--i){
			rs[i]=mp[v[i]];
			mp[v[i]]=i;
		}
	}
	

	long long int ny=0,nx=start;
	map<int,long long int>mp;
	mp[start]=0;
	while(true){
		//cout<<nx<<ny<<endl;
		auto next_x=rs[nx];
		long long next_y=ny;
		//cout<<next_x<<' '<<next_y<<endl;
		if(next_x>=N){
			next_y++;
			next_x-=N;
		}
		next_x++;
		if(next_x>=N){
			next_y++;
			next_x-=N;
		}
		if(next_y>=K){
			
			if(K-1==next_y){
				v.insert(v.end(),v.begin(),v.end());
			}
			return solve2(v,nx);
		}else{
			ny=next_y;
			nx=next_x;

			if(mp.find(nx)==mp.end()){
				mp[nx]=ny;
			}else{
				long long sa=-(mp[nx]-ny);
				long long int rest=K-ny;
				rest%=sa;
				//cout<<rest<<endl;
				if(rest==0)rest=sa;

				long long xx=K%sa;
				if(xx==0)xx=sa;
				assert(nx==0);
				return solve(N,xx,v,0);
			}
		}
	}
	assert(false);
	
}
#define WHATS(var) cout<<#var<<"="<<var<<endl;
int main() {
	ios::sync_with_stdio(false);
	int N,M;cin>>N>>M;
	set<long long int>sushis,teas;
	for(int i=0;i<N;++i){
		int a;cin>>a;
		sushis.emplace(a);
		sushis.emplace(a+M);
		sushis.emplace(a+2*M);

	}
	for(int i=0;i<N;++i){
		int a;cin>>a;
		teas.emplace(a);
		teas.emplace(a+M);
		teas.emplace(a+2*M);

	}
	long long int answer=0;
	int now=0;
	for(int i=0;i<N;++i){
		auto it=sushis.lower_bound(now);

		answer+=*it-now;
		now=*it;
		now%=M;
		for(int j=0;j<3;++j){
			sushis.erase(now+j*M);
		}



		auto jt=teas.lower_bound(now);
		auto lt=jt;
		if(!sushis.empty()){
			
			auto kt=sushis.lower_bound(*jt);
			//WHATS(*kt);
			//WHATS(*teas.upper_bound(*kt));
			//WHATS(*prev(teas.upper_bound(*kt)));
			lt=prev(teas.upper_bound(*kt));
		}

		answer+=*lt-now;
		now=*lt;
		now%=M;
		for(int j=0;j<3;++j){
			teas.erase(now+M*j);
		}
	}
	cout<<answer<<endl;
	return 0;
}	
0