Submission #789863

#TimeUsernameProblemLanguageResultExecution timeMemory
789863cig32Stone Arranging 2 (JOI23_ho_t1)C++17
100 / 100
412 ms160844 KiB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
void solve(int tc) {
  int n; cin >> n;
  int a[n+1];
  for(int i=1; i<=n; i++) cin >> a[i];
  vector<pair<int,int>> add[n+1],sub[n+2];// {time, color}
  map<int, stack<int> >index; // list of elements, essentially
  stack<int> good; // can be considered the last element
  for(int i=1; i<=n; i++) {
    if(index[a[i]].size()) {
      int from = index[a[i]].top() + 1, to = i - 1;
      if(from <= to) {
        add[from].push_back({i, a[i]});
        sub[to+1].push_back({i, a[i]});
      }
      while(good.size() && good.top() >= from) {
        int x = good.top();
        index[a[x]].pop();
        good.pop();
      }
    }
    index[a[i]].push(i);
    good.push(i);

  }
  multiset<pair<int,int>>mst;
  for(int i=1;i<=n;i++){
    for(pair<int,int>x:add[i]) mst.insert(x);
    for(pair<int,int>x:sub[i]) mst.erase(x);
    if(mst.size()) cout << (*mst.rbegin()).second << "\n";
    else cout << a[i] << "\n";
  }
}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷

/*
7 1 2
1 2 5 8 11 12 14
11 16 12 18 2 7 8
4 4 11 20 1 20 13
14 9 3 16 9 19 16
9 10 15 10 5 4 19



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