0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ABC234 D - Prefix K-th Max

Last updated at Posted at 2022-01-19

k番目に大きい値を記録しておき,それよりも大きな数がきた場合はかつてのk番目に大きな値がk+1番目に大きな値となる.そのためk+1番目(かつてのk番目)に大きな値を二分探索し,その一つ前の値を新たなk番目に大きな値として更新する.
計算量$O(nlogn)$

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

int main(){
    int n, k;
    cin >> n >> k;
    vector<int> p(n);
    for(auto &e : p) cin >> e;
    set<int, greater<int>> st;
    for(int i=0; i<k; i++) st.insert(p[i]);
    int t = *prev(st.end());
    cout << t << "\n";

    for(int i=k; i<n; i++){
        st.insert(p[i]);
        if(p[i]>t){
            auto itr = st.lower_bound(t);
            t = *prev(itr);
        }
        cout << t << "\n";
    }

    return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?