洛谷 P7072 [CSP-J2020] 直播获奖
当年比赛不会STL
手写堆确实麻烦
#include<bits/stdc++.h> using namespace std; #define N 100005 int n , k ; int a[N]; int maxn[205]; int heap[N] , len ; int ans [N] ; int cnt ; void put (int x) { int now ; heap[++len] = x ; now = len ; while(now > 1) { int net = now >> 1; if(heap[net] <= heap[now]) break; swap(heap[net] , heap[now]); now = net; } } void get() { int now ; heap [1] = heap[len--]; now = 1; while( (now<<1) <= len) { int net = now << 1; if(net < len && heap[net+1] < heap[net]) ++net; if(heap [net] >= heap[now] ) break; swap(heap[net] , heap[now]); now = net; } } int find() { return heap [1]; } int main() { //freopen("live.in","r",stdin); //freopen("live.out","w",stdout); scanf("%d%d", &n , &k) ; int t = 0 ; for(int i= 1 ; i<= n ;++i) { scanf("%d" , &a[i]); int s = max(1, (i * k) / 100); if(s > t ) { if(a[i] >= maxn[1] ) put(a[i]); else { put(maxn[1]); for(int j= 1; j<= 200 ; ++j) { if(a[i] > maxn[j+1] ) { maxn[j] = a[i]; break; } maxn[j] = maxn[j+1]; } } } else { if(a[i] > find() ) { for(int j = 200 ; j >= 2 ;--j) maxn[j] = maxn[j-1]; maxn[1] = find(); get() ; put(a[i]) ; } else { for(int j = 1; j <= 200 ; ++j ) { if(a[i] > maxn[j]) { for(int q = 200 ; q >= j + 1 ; --q ) maxn[q] = maxn [ q - 1] ; maxn[j] = a[i] ; break; } } } } t = s; ans[++cnt] = find(); } for(int i= 1;i<= cnt; ++i) printf("%d%c",ans[i],i==cnt ? '\n' : ' '); return 0; }

浙公网安备 33010602011771号