結果
| 問題 |
No.2029 Swap Min Max Min
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-08-05 21:25:48 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 2,363 bytes |
| コンパイル時間 | 1,501 ms |
| コンパイル使用メモリ | 135,732 KB |
| 最終ジャッジ日時 | 2025-01-30 17:37:34 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 31 WA * 11 |
ソースコード
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
using namespace std;
//constexpr long long int MOD = 1000000007;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;
//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
class Add_Segment_Tree {
vector<long long int>v;
long long int ret;
int num;
long long int Update(int place) {
if (place >= v.size() / 2) {
return v[place];
}
v[place] = Update(place * 2) + Update(place * 2 + 1);
return v[place];
}
public:
Add_Segment_Tree(int n) {
n++;
num = 1;
while (num < n * 2)num *= 2;
v.resize(num, 0);
}
void Add(int place, long long int num, bool update) {
place += v.size() / 2;
v[place] += num;
if (!update)return;
place /= 2;
while (place) {
v[place] = v[place * 2] + v[place * 2 + 1];
place /= 2;
}
}
void TopDown() {
Update(1);
}
long long int Sum(int a, int b) {
ret = 0;
b++;
for (a += num / 2, b += num / 2; a < b; a >>= 1, b >>= 1) {
if (a & 1)ret += v[a++];
if (b & 1)ret += v[--b];
}
return ret;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<int>v(N);
for (auto& i : v)cin >> i;
auto w = v;
sort(w.begin(), w.end());
cout << w[N / 2 - 1] << " ";
vector<int>a;
vector<int>b;
for (int i = 0; i < N; i++) {
if (v[i] <= N / 2)b.push_back(i);
else a.push_back(i);
}
long long ans = LLONG_MAX;
{
vector < int>x;
for (int i = 0; i < N / 2; i++) {
x.push_back(a[i]);
x.push_back(b[i]);
}
if (x.size() < v.size())x.push_back(a.back());
Add_Segment_Tree asg(N);
long long c = 0;
for (auto i : x) {
c += asg.Sum(i, N - 1);
asg.Add(i, 1, true);
}
ans = min(ans, c);
}
if (N % 2 == 0) {
vector < int>x;
for (int i = 0; i < N / 2; i++) {
x.push_back(b[i]);
x.push_back(a[i]);
}
if (x.size() < v.size())x.push_back(a.back());
Add_Segment_Tree asg(N);
long long c = 0;
for (auto i : x) {
c += asg.Sum(i, N - 1);
asg.Add(i, 1, true);
}
ans = min(ans, c);
}
cout << ans << endl;
}