이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
mt19937 rnd(51);
#define ll long long
#define pb push_back
#define ld long double
const ld INF = 1e18;
//a * (a - 1) / 4
ld func(int a) {
return (ld)a * (a - 1) / 4;
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
vector<int> a(n);
map<char, int> mp;
for (int i = 0; i < s.size(); i++) {
if (mp.count(s[i]) == 0) {
mp[s[i]] = mp.size();
}
a[i] = mp[s[i]];
}
int g = mp.size();
vector<ld> dp((1 << g), INF);
vector<vector<int>> pos(g);
vector<vector<vector<ll>>> L(g, vector<vector<ll>>(g, vector<ll>(n))), R(g, vector<vector<ll>>(g, vector<ll>(n)));
for (int i = 0; i < n; i++) {
pos[a[i]].pb(i);
}
for (int i = 0; i < g; i++) {
for (int l = 0; l < g; l++) {
ll cnt = 0, res = 0;
for (int j = 0; j < n; j++) {
if (i == a[j]) {
res += cnt;
L[i][l][j] = res;
} else if (l == a[j]){
cnt++;
}
}
cnt = 0, res = 0;
for (int j = n - 1; j >= 0; j--) {
if (i == a[j]) {
res += cnt;
R[i][l][j] = res;
} else if (l == a[j]){
cnt++;
}
}
}
}
dp[0] = 0;
for (int i = 0; i < (1 << g); i++) {
for (int j = 0; j < g; j++) {
if (!(i & (1 << j))) {
ld bst = func(pos[j].size());
for (int k = 0; k < g; k++) {
if (i & (1 << k)) {
bst += R[j][k][pos[j][0]];
}
}
for (int k = 0; k < pos[j].size(); k++) {
ld val = func(k + 1) + func((int)(pos[j].size()) - k - 1);
for (int l = 0; l < g; l++) {
if (i & (1 << l)) {
val += L[j][l][pos[j][k]];
if (k + 1 < pos[j].size()) {
val += R[j][l][pos[j][k + 1]];
}
}
}
bst = min(bst, val);
}
dp[i + (1 << j)] = min(dp[i + (1 << j)], dp[i] + bst);
}
}
}
cout << setprecision(30) << fixed << dp[(1 << g) - 1] << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
passes.cpp: In function 'int main()':
passes.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
passes.cpp:75:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int k = 0; k < pos[j].size(); k++) {
| ~~^~~~~~~~~~~~~~~
passes.cpp:80:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | if (k + 1 < pos[j].size()) {
| ~~~~~~^~~~~~~~~~~~~~~
# | 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... |