이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn1 = 1e5+10;
const int maxn = 1e5+10;
const int maxg = 17;
int n, a[maxn1];
int pf[(maxg)][maxn], dp[(1<<maxg)], prepl[maxg][maxg][maxn], prepr[maxg][maxg][maxn], pfl[maxg][maxn], sfr[maxg][maxn];
vector<int> pos[maxg];
void solve() {
string s; cin >> s;
n = s.size();
vector<char> cc;
for(auto x : s) cc.pb(x);
sort(all(cc));
cc.erase(unique(all(cc)),cc.end());
for(int i = 1; i <= n; i++) {
a[i] = lower_bound(all(cc),s[i-1])-cc.begin();
pos[a[i]].pb(i);
}
int g = cc.size();
// for(int mask = 1; mask < (1<<g); mask++) {
// pf[mask][0] = 0;
// for(int i = 1; i <= n; i++) {
// pf[mask][i] = pf[mask][i-1];
// if((1<<a[i])&mask) pf[mask][i]++;
// }
// }
for(int i = 0; i < g; i++) {
for(int x = 1; x <= n; x++) {
pf[i][x] = pf[i][x-1];
if(a[x] == i) pf[i][x]++;
}
}
for(int i = 0; i < g; i++) {
for(int j = 0; j < g; j++) {
for(int x = 1; x <= n; x++) {
prepl[i][j][x] = prepl[i][j][x-1];
if(pf[i][x]-pf[i][x-1]) prepl[i][j][x]+= pf[j][x-1];
}
}
for(int x = 1; x <= n; x++) {
pfl[i][x] = pfl[i][x-1];
if(pf[i][x]-pf[i][x-1]) pfl[i][x]+= (pf[i][x-1]);
}
}
for(int i = 0; i < g; i++) {
for(int j = 0; j < g; j++) {
for(int x = n; x >= 1; x--) {
prepr[i][j][x] = prepr[i][j][x+1];
if(pf[i][x]-pf[i][x-1]) prepr[i][j][x]+= pf[j][n]-pf[j][x];
}
}
for(int x = n; x >= 1; x--) {
sfr[i][x] = sfr[i][x+1];
if(pf[i][x]-pf[i][x-1]) sfr[i][x]+= (pf[i][n]-pf[i][x]);
}
}
for(int mask = 1; mask < (1<<g); mask++) {
dp[mask] = inf;
for(int i = 0; i < g; i++) {
if(((1<<i)&mask) == 0) continue;
int mask1 = mask-(1<<i);
int l = 1;
int r = pos[i].size();
int ans = 0;
// I want to find the greatest i such that I going to
// the left if better than i going to the right
while(l <= r) {
int mid = (l+r)/2;
int x = pos[i][mid-1];
int ansl = mid-1;
for(int j = 0; j < g; j++) {
if(((1<<j)&mask1) == 0) continue;
ansl+= 2*(pf[(j)][x]);
}
int ansr = (pos[i].size()-mid);
for(int j = 0; j < g; j++) {
if(((1<<j)&mask1) == 0) continue;
ansr+= 2*(pf[(j)][n]-pf[(j)][x]);
}
if(ansl <= ansr) {
ans = mid;
l = mid+1;
}
else {
r = mid-1;
}
}
int ans1 = dp[mask1];
int x = 0;
if(ans != 0) x = pos[i][ans-1];
if(ans != 0) {
ans1+= pfl[i][x];
for(int j = 0; j < g; j++) {
if(((1<<j)&mask1) == 0) continue;
ans1+= 2*prepl[i][j][pos[i][ans-1]];
}
}
if(ans != pos[i].size()) {
ans1+= sfr[i][x+1];
for(int j = 0; j < g; j++) {
if(((1<<j)&mask1) == 0) continue;
ans1+= 2*prepr[i][j][x+1];
}
}
dp[mask] = min(dp[mask],ans1);
}
}
cout.setf(ios::fixed);
cout.precision(10);
cout << (dbl) dp[(1<<g)-1]/2 << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
passes.cpp: In function 'void solve()':
passes.cpp:123:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | if(ans != pos[i].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... |