# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
973584 | berr | Railway Trip 2 (JOI22_ho_t4) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
array<float, 2> dp[N][N];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
vector<array<int, 2>> a(n);
for(auto &[i, l]: a) cin >> i >> l;
sort(a.begin(), a.end(), [&](auto x, auto y){
if(x[1]==y[1]) return x[0] < y[0];
else if(y[1]==-1) return true;
else if(x[1]==-1) return false;
return x[1]<y[1];
});
for(int i=0; i<=n; i++){
for(int l=0; l<=n; l++){
dp[i][l]={1e9, 1e9};
}
}
dp[0][0]={0, 0};
for(int i=0; i<n; i++){
for(int l=n; l>=0; l--){
for(int x=n-1; x>=0; x--){
// cout<<(float)a[i][1]/(float)(l+1)<<"\n";
if(a[i][1]!=-1)
dp[l+1][x]=min(dp[l+1][x], {dp[l][x][0]+(float)a[i][1]/(float)(l+1), dp[l][x][1]});
dp[l][x+1]=min(dp[l][x+1], {dp[l][x][0], dp[l][x][1]+a[i][0]});
}
}
}
float ans=1e9;
for(int i=0; i<=k; i++){
ans=min(ans, dp[i][k-i][0]+(float)dp[i][k-i][1]/(float)(i+1));
}
cout<<setprecision(9)<<fixed<<<<ans<<"\n";
}