結果

問題 No.318 学学学学学
ユーザー izuru_matsuura
提出日時 2016-09-01 22:22:51
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 1,566 bytes
コンパイル時間 1,960 ms
コンパイル使用メモリ 175,852 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-06-22 17:49:39
合計ジャッジ時間 4,990 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        os << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os;
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N;
    vector<int> A;
    void input() {
        cin >> N;
        A.resize(N); cin >> A;
    }

    void dump(set<int>& s) {
        cout << "set: ";
        for (auto e: s) {
            cout << e << " " << endl;
        }
    }

    void solve() {
        map<int, pair<int,int>> m;
        for (int i = 0; i < N; i++) {
            int a = A[i];
            if (not m.count(a)) m[a] = make_pair(i, -1);
            else m[a].second = i;
        }
        vector<int> e(N, 0);
        for (auto it = m.begin(); it != m.end(); it++) {
            if (it->second.second < 0) continue;
            e[it->second.first] = 1;
            e[it->second.second] = -1;
        }
        set<int> s;
        vector<int> B(N, -1);
        for (int i = 0; i < N; i++) {
            if (e[i] == 1) s.insert(A[i]);
            if (s.empty()) B[i] = A[i];
            else {
                int b = max(A[i], *s.rbegin());
                B[i] = b;
            }
            if (e[i] == -1) s.erase(A[i]);
        }
        cout << B << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}

0