제출 #1176716

#제출 시각아이디문제언어결과실행 시간메모리
1176716Zbyszek99Boarding Passes (BOI22_passes)C++20
100 / 100
270 ms52228 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define ull unsigned long long #define ff first #define ss second #define pii pair<int,int> #define pll pair<long long, long long> #define vi vector<int> #define vl vector<long long> #define pb push_back #define rep(i, b) for(int i = 0; i < (b); ++i) #define rep2(i,a,b) for(int i = a; i <= (b); ++i) #define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c) #define count_bits(x) __builtin_popcountll((x)) #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define forall(it,x) for(auto& it:(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; //mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} //ll los(ll a, ll b) {return a + (mt() % (b-a+1));} const int INF = 1e9+50; const ll INF_L = 1e18+40; const ll MOD = 1e9+7; ll dp_add[15][(1 << 15)]; ll L_pref[15][100'001]; ll R_pref[15][100'001]; ll L_val[15][100'001]; ll R_val[15][100'001]; ll dp[(1 << 15)]; vi ind[15]; int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //random_start(); string s; cin >> s; int n = siz(s); rep(i,n) { ind[s[i] - 'A'].pb(i); } rep(i,15) { rep(j,15) { //L int p2 = 0; ll cur_sum = 0; ll cur_add = 0; rep(p1,siz(ind[i])) { while(p2 < siz(ind[j]) && ind[j][p2] < ind[i][p1]) { cur_add++; p2++; } cur_sum += cur_add; L_pref[j][p1] = cur_sum; L_val[j][p1] = cur_add; } //R p2 = siz(ind[j])-1; cur_sum = 0; cur_add = 0; for(int p1 = siz(ind[i])-1; p1 >= 0; p1--) { while(p2 >= 0 && ind[j][p2] > ind[i][p1]) { cur_add++; p2--; } cur_sum += cur_add; R_pref[j][p1] = cur_sum; R_val[j][p1] = cur_add; } } rep(mask,(1 << 15)) { int l = 0; int r = siz(ind[i])-1; ll ans = -1; while(l <= r) { int mid = (l+r)/2; ll L_sum = 0; ll R_sum = 0; rep(j,15) { if(mask & (1 << j)) { L_sum += L_val[j][mid]; R_sum += R_val[j][mid]; } } if(L_sum * 2 + mid <= R_sum * 2 + (siz(ind[i])-mid-1)) { ans = mid; l = mid+1; } else { r = mid-1; } } ll Ls = 0; ll Rs = 0; rep(j,15) { if(mask & (1 << j)) { if(ans != -1) Ls += L_pref[j][ans]*2; if(ans != siz(ind[i])-1) Rs += R_pref[j][ans+1]*2; } } dp_add[i][mask] = (ans != -1 ? Ls + ((ans+1) * ans) / 2 : 0) + (ans != siz(ind[i])-1 ? Rs + ((ll)(siz(ind[i])-ans-2 + 1) * (ll)(siz(ind[i])-ans-2))/2 : 0); } } dp[0] = 0; rep2(mask,1,(1 << 15)-1) { ll ans = 1e18; rep(i,15) { if(mask & (1 << i)) { ans = min(ans,dp[mask-(1 << i)] + dp_add[i][mask-(1 << i)]); } } dp[mask] = ans; } cout << dp[(1 << 15)-1]/2; if(dp[(1 << 15)-1] % 2 == 1) cout << ".5\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...