이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;
long double exp_inv(int n){
return (long double) n * (n - 1) / 4.0;
}
vector<int> pos[140];
void solve(){
string s;
cin >> s;
int n = (int)s.size();
string alphabet;
for (int i = 0; i < n; i++){
if (pos[s[i]].empty()) alphabet += s[i];
pos[s[i]].push_back(i);
}
sort(all(alphabet));
int m1 = (n - 1) / 2;
int m2 = m1 + 1;
long double ans = 1e18;
do {
long double tot = 0;
vector<bool> used(n, false);
for (char c : alphabet){
ll sum_l = 0;
vector<int> diff;
int sz = (int) pos[c].size();
for (int i : pos[c]){
int l = 0, r = 0;
for (int j = 0; j < i; j++) l += used[j];
for (int j = i + 1; j < n; j++) r += used[j];
sum_l += l;
diff.push_back(r - l);
}
sort(all(diff));
long double cur = 1e18;
for (int half = 0; half <= sz; half++){
if (half > 0) sum_l += diff[half - 1];
cur = min(cur, sum_l + exp_inv(half) + exp_inv(sz - half));
}
for (int i : pos[c]) used[i] = true;
tot += cur;
}
ans = min(ans, tot);
} while (next_permutation(all(alphabet)));
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
passes.cpp: In function 'void solve()':
passes.cpp:23:21: warning: array subscript has type 'char' [-Wchar-subscripts]
23 | if (pos[s[i]].empty()) alphabet += s[i];
| ^
passes.cpp:24:17: warning: array subscript has type 'char' [-Wchar-subscripts]
24 | pos[s[i]].push_back(i);
| ^
passes.cpp:42:32: warning: array subscript has type 'char' [-Wchar-subscripts]
42 | int sz = (int) pos[c].size();
| ^
passes.cpp:44:30: warning: array subscript has type 'char' [-Wchar-subscripts]
44 | for (int i : pos[c]){
| ^
passes.cpp:60:30: warning: array subscript has type 'char' [-Wchar-subscripts]
60 | for (int i : pos[c]) used[i] = true;
| ^
passes.cpp:30:9: warning: unused variable 'm2' [-Wunused-variable]
30 | int m2 = m1 + 1;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |