#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
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++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
276 ms |
348 KB |
Output is correct |
13 |
Correct |
275 ms |
368 KB |
Output is correct |
14 |
Correct |
198 ms |
352 KB |
Output is correct |
15 |
Correct |
1455 ms |
364 KB |
Output is correct |
16 |
Correct |
1231 ms |
360 KB |
Output is correct |
17 |
Correct |
508 ms |
380 KB |
Output is correct |
18 |
Correct |
2259 ms |
376 KB |
Output is correct |
19 |
Correct |
1932 ms |
376 KB |
Output is correct |
20 |
Correct |
761 ms |
372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2374 ms |
376 KB |
Output is correct |
2 |
Correct |
2432 ms |
380 KB |
Output is correct |
3 |
Correct |
2317 ms |
376 KB |
Output is correct |
4 |
Correct |
2351 ms |
460 KB |
Output is correct |
5 |
Correct |
2300 ms |
392 KB |
Output is correct |
6 |
Correct |
2296 ms |
380 KB |
Output is correct |
7 |
Correct |
2307 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
276 ms |
348 KB |
Output is correct |
13 |
Correct |
275 ms |
368 KB |
Output is correct |
14 |
Correct |
198 ms |
352 KB |
Output is correct |
15 |
Correct |
1455 ms |
364 KB |
Output is correct |
16 |
Correct |
1231 ms |
360 KB |
Output is correct |
17 |
Correct |
508 ms |
380 KB |
Output is correct |
18 |
Correct |
2259 ms |
376 KB |
Output is correct |
19 |
Correct |
1932 ms |
376 KB |
Output is correct |
20 |
Correct |
761 ms |
372 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |