이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define double long double
#define rep(i,n) for(int i =0; i<n; i++)
#define all(a) a.begin(),a.end()
using namespace std;
string s;
int n;
double f(int n)
{
return ((double) n)*((double) (n-1))/4.0;
}
string ord;
vector<double>mem;
vector<vector<int>>pos;
int g;
// cost to add the group gr with all members of that group with and index <= to i boarding on the left
double cost(int bor, int gr, int i)
{
vector<int>pref;
double sum = 0;
int firstr = upper_bound(all(pos[gr]),i) - pos[gr].begin();
sum+=f(pos[gr].size()-firstr) + f(firstr);
return sum;
}
double dfs(int bor)
{
if(mem[bor]>-0.9) return mem[bor];
int tmp = 0;
rep(i,g) if((1<<i)&bor) tmp++;
if(tmp==1) return f(n/2) + f((n+1)/2);
double sol = 1e18;
rep(i,g)
{
if(!((1<<i)&bor)) continue;
int nbor = bor^(1<<i);
int start = 0, end = n;
while (end-start>1)
{
int c = (start+end)/2;
if(cost(nbor,i,c)>cost(nbor,i,c+1)) start=c;
else end=c;
}
sol=min(sol, cost(nbor,i,start));
}
return mem[bor] = sol;
}
signed main()
{
cin>>s;
s.size();
n=s.size();
double sum = f(n/2) + f((n+1)/2);
cout.precision(300);
char maxx=0;
rep(i,n) maxx=max(maxx, s[i]);
g='A'-maxx+1;
pos.resize(g);
mem.assign(1<<g,-1.0);
rep(i,n) pos[(s[i]-'A')].push_back(i);
cout<<dfs((1<<g)-1)<<'\n';
}
컴파일 시 표준 에러 (stderr) 메시지
passes.cpp: In function 'int main()':
passes.cpp:65:12: warning: unused variable 'sum' [-Wunused-variable]
65 | double sum = f(n/2) + f((n+1)/2);
| ^~~
# | 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... |