# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1178467 | _dtq_ | 중앙값 배열 (balkan11_medians) | C++17 | 28 ms | 8516 KiB |
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (long long)(x.size())
using namespace std;
const ll N = 1e5 + 9;
ll n, i, a[N], b[N], bit[N];
bool check[N];
void up( ll x )
{
while(x <= 2 * n - 1)
{
bit[x] ++;
x += (x & (-x));
}
}
ll get( ll x )
{
ll sum = 0;
while(x > 0)
{
sum += bit[x];
x -= (x & (-x));
}
return sum;
}
int main()
{
#define TN "medians"
if(fopen(TN ".in", "r"))
{
freopen(TN ".in", "r", stdin);
freopen(TN ".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for( i = 1; i <= n; i ++ ) cin >> b[i];
a[1] = b[1];
check[b[1]] = 1;
up(b[1]);
set<ll>ans;
for( i = 2; i <= n; i ++ )
{
if(check[b[i]]) continue;
check[b[i]] = 1;
a[2 * i - 1] = b[i];
}
for( i = 1; i <= 2 * n - 1; i ++ )
if(check[i] == 0) ans.insert(i);
ll vt = 2;
for( i = 2; i <= n; i ++ )
{
while(vt <= 2 * i - 1)
{
if(a[vt])
{
up(a[vt]);
vt ++;
continue;
}
ll res1 = get(b[i] - 1);
ll res2 = get(2 * n - 1) - get(b[i]);
if(res1 < res2)
{
a[vt] = *ans.begin();
ans.erase(ans.begin());
}
else
{
a[vt] = *ans.rbegin();
ans.erase(--ans.end());
}
up(a[vt]);
vt ++;
}
}
for( i = 1; i <= 2 * n - 1; i ++ ) cout << a[i] << " ";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |