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;
#define ll long long
#define int ll
#define pii pair<int, int>
#define ff first
#define ss second
#define ld long double
#define pb push_back
pii a[200100];
bool cmp(pii a, pii b){
return a.ss < b.ss;
}
void solve(){
int n, k;
cin>>n>>k;
int dmx = 0;
for(int i=1; i<=n; i++){
cin>>a[i].ff>>a[i].ss;
if(a[i].ss == -1){
a[i].ss = 1e9;
}
}
sort(a + 1, a + 1 + n, cmp);
//for(int i=1; i<=n; i++) cout<<a[i].ff<<' '<<a[i].ss<<'\n';
ld ans = 1e18;
for(ld D=1; D<=k+1; D++){
vector<int> d;
ld res = 0;
int m = k - D+1;
int ok = 1;
for(int i=1; i<=D-1; i++){
d.pb(i);
res += 1.0 * a[i].ss / i;
}
multiset<pii> dd;
for(int i=D; i<=n; i++){
dd.insert({a[i].ff, i});
if(dd.size() > m){
dd.erase(--(dd.end()));
}
}
for(pii x: dd){
res += x.ff / D;
}
//cout<<"________________\n";
//cout<<D<<'\n';
//cout<<res<<'\n';
//for(auto x: dd) cout<<x.ff<<' '<<x.ss<<'\n';
//for(int x: d) cout<<x<<' ';
//cout<<'\n';
ans = min(ans, res);
for(int i=D; i<=n; i++){
//cout<<i<<'\n';
pair<ld, int> mn = {1e18, -1};
res += a[i].ss * 1.0 / (D);
int mx = (dd.size() ?(*dd.begin()).ss :0);
if(dd.find({a[i].ff, i}) != dd.end()){
mx = 0;
dd.erase({a[i].ff, i});
res -= a[i].ff / D;
}
ld g = 0;
d.pb(i);
for(int i=d.size() - 1; i>=0; i--){
int j = d[i];
mn = min(mn, {-a[j].ss * 1.0 / (i + 1) + g - a[mx].ff/D + a[j].ff/D, j});
if(i > 0)g += -a[j].ss * 1.0 / (i + 1) + a[j].ss * 1.0 / i;
}
res += mn.ff;
dd.insert({a[mn.ss].ff, mn.ss});
if(dd.size() > m) dd.erase(--(dd.end()));
vector<int> dg;
for(int x: d){
if(x != mn.ss) dg.pb(x);
}
d = dg;
ans = min(ans, res);
}
}
cout<<fixed<<setprecision(6)<<ans;
}
signed main(){
solve();
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:45:26: warning: comparison of integer expressions of different signedness: 'std::multiset<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
45 | if(dd.size() > m){
| ~~~~~~~~~~^~~
Main.cpp:80:26: warning: comparison of integer expressions of different signedness: 'std::multiset<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
80 | if(dd.size() > m) dd.erase(--(dd.end()));
| ~~~~~~~~~~^~~
Main.cpp:37:13: warning: unused variable 'ok' [-Wunused-variable]
37 | int ok = 1;
| ^~
Main.cpp:23:9: warning: unused variable 'dmx' [-Wunused-variable]
23 | int dmx = 0;
| ^~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |