Submission #850423

#TimeUsernameProblemLanguageResultExecution timeMemory
850423Ahmed57Boarding Passes (BOI22_passes)C++17
60 / 100
599 ms40624 KiB
#include <bits/stdc++.h> using namespace std; int c = 0; vector<int> arr[15]; short pref[10001][1024],suf[10001][1024]; long double dp[1024]; int sz; long double ge(long double x){ if(x<=1)return 0; return (x*(x-1))/4.0; } int main(){ string s;cin>>s; map<char,int> x; int ind = -1; sz = s.size(); for(auto i:s){ ind++; if(x[i]==0){ x[i] = ++c; arr[c-1].push_back(ind); }else{ arr[x[i]-1].push_back(ind); } } if(c==1){ long long x = s.size(); if(x<=2){ cout<<0.0<<endl;return 0; } else if(x==3){ cout<<0.5<<endl; return 0; } long double v1 = x/2 , v2 = x-v1; long double ans = ((v1*(v1-1)/2)*(v1*(v1-1)/2))/(v1*(v1-1)); swap(v1,v2); ans+= ((v1*(v1-1)/2)*(v1*(v1-1)/2))/(v1*(v1-1)); cout<<setprecision(3)<<fixed<<ans<<endl; return 0; } for(int mask = 0;mask<(1<<c);mask++){ for(int i = 0;i<sz;i++){ pref[i][mask] = (mask&(1<<(x[s[i]]-1))?1:0); if(i)pref[i][mask]+=pref[i-1][mask]; } for(int i = sz-1;i>=0;i--){ suf[i][mask] =(mask&(1<<(x[s[i]]-1))?1:0); if(i<sz-1)suf[i][mask]+=suf[i+1][mask]; } } for(int mask = (1<<c)-1;mask>=0;mask--){ if(mask==(1<<c)-1){ dp[mask] = 0; continue; } dp[mask] = 1000000000.0; for(int j = 0;j<c;j++){ if(!(mask&(1<<j))){ long long all = 0 , xd = 0; for(int e = 0;e<arr[j].size();e++){ all+=suf[arr[j][e]][mask]; } dp[mask] = min(dp[mask],dp[mask^(1<<j)]+all+xd+ge(arr[j].size())); for(int e = 0;e<arr[j].size();e++){ all-=suf[arr[j][e]][mask]; xd+=pref[arr[j][e]][mask]; dp[mask] = min(dp[mask],dp[mask^(1<<j)]+all+xd+ge(e+1)+ge(arr[j].size()-e-1)); } } } } cout<<setprecision(4)<<fixed<<dp[0]<<endl; return 0; }

Compilation message (stderr)

passes.cpp: In function 'int main()':
passes.cpp:61:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |             for(int e = 0;e<arr[j].size();e++){
      |                           ~^~~~~~~~~~~~~~
passes.cpp:65:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             for(int e = 0;e<arr[j].size();e++){
      |                           ~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...