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 int long long
#define double long double
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pii pair<double,double>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(),x.end()
#define de(x) cout << #x << ' ' << x << '\n';
template<class T> void prt(T l,T r){
while(l!=r) cout << *l << ' ',l++;
cout << '\n';
}
const int N=1e6+5,M=1e9+7,INF=1e18;
signed main(){
IO
int n,k;cin >> n >> k;
vector<double> a(n),b(n);
for(int i=0;i<n;i++) cin >> a[i] >> b[i];
int cnt=n;
for(int i=0;i<n;i++) if(b[i]==-1) b[i]=INF,cnt--;
double ans=0;
set<int> A,B;
vector<pii> v(n);
for(int i=0;i<n;i++){
v[i]=mp(a[i],i);
}
sort(all(v));
for(int i=0;i<k;i++){
A.insert(v[i].ss);
ans+=v[i].ff;
}
for(double c=1;c<=min(cnt,k);c++){
double mn=INF;
int mni=-1;
int mx=0,mxi=-1;
for(auto x:A){
if(a[x]>mx){
mx=a[x];
mxi=x;
}
}
for(int i=0;i<n;i++){
if(B.count(i)) continue;
if(A.count(i)){
A.erase(i);
B.insert(i);
vector<int> aa,bb;
for(auto x:A) aa.pb(a[x]);
for(auto x:B) bb.pb(b[x]);
sort(all(aa));
sort(all(bb));
double sum=0,cc=1;
for(int i=0;i<bb.size();i++){
sum+=bb[i]/cc;
cc++;
}
for(int i=0;i<aa.size();i++){
sum+=aa[i]/cc;
}
if(sum<mn){
mn=sum;
mni=i;
}
B.erase(i);
A.insert(i);
}else{
A.erase(mxi);
B.insert(i);
vector<int> aa,bb;
for(auto x:A) aa.pb(a[x]);
for(auto x:B) bb.pb(b[x]);
sort(all(aa));
sort(all(bb));
double sum=0,cc=1;
for(int i=0;i<bb.size();i++){
sum+=bb[i]/cc;
cc++;
}
for(int i=0;i<aa.size();i++){
sum+=aa[i]/cc;
}
if(sum<mn){
mn=sum;
mni=i;
}
A.insert(mxi);
B.erase(i);
}
}
ans=min(ans,mn);
if(A.count(mni)){
A.erase(mni);
B.insert(mni);
}else{
A.erase(mxi);
B.insert(mni);
}
}
cout << setprecision(15) << fixed;
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:64:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i=0;i<bb.size();i++){
| ~^~~~~~~~~~
Main.cpp:68:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i=0;i<aa.size();i++){
| ~^~~~~~~~~~
Main.cpp:86:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i=0;i<bb.size();i++){
| ~^~~~~~~~~~
Main.cpp:90:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(int i=0;i<aa.size();i++){
| ~^~~~~~~~~~
# | 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... |