제출 #892227

#제출 시각아이디문제언어결과실행 시간메모리
892227LCJLYStone Arranging 2 (JOI23_ho_t1)C++14
100 / 100
150 ms21588 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;

void solve(){	
	int n;
	cin >> n;
	int arr[n];
	for(int x=0;x<n;x++){
		cin >> arr[x];
	}
	
	map<int,int>last;
	int next[n];
	memset(next,-1,sizeof(next));
	for(int x=n-1;x>=0;x--){
		if(last.find(arr[x])!=last.end()){
			next[x]=last[arr[x]];
		}
		else last[arr[x]]=x;
	}
	
	//show4(next,next);
	
	int cur=-1;
	int color=0;
	int ans[n];
	for(int x=0;x<n;x++){
		if(next[x]!=-1&&cur<x){
			cur=next[x];
			color=arr[x];
		}
		
		if(cur<x) ans[x]=arr[x];
		else ans[x]=color; 
	}
	
	for(int x=0;x<n;x++){
		cout << ans[x] << " ";
	}
}

int32_t main(){										
	ios::sync_with_stdio(0);	
	cin.tie(0);
	//freopen("in.txt", "r", stdin);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}	
}



		


		
		
	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...