#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ld long double
using namespace std ;
const int N = 500 ;
bool flag1 ;
int n, k ;
bool us[N + 1] ;
ld abu = 1e18 ;
pair<pair<ld, ld>, int> p[N + 1] ;
void rec(int now, ld sum, ld cnt)
{
if(now == k)
{
abu = min(abu, sum) ;
return ;
}
for(int i = 1 ; i <= n ; i++)
{
if(us[i])
continue ;
us[i] ^= 1 ;
rec(now + 1, sum + p[i].fi.fi / cnt, cnt) ;
if(p[i].fi.se != -1)rec(now + 1, sum + p[i].fi.se / cnt, cnt + 1) ;
us[i] ^= 1 ;
}
}
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n ;
cin >> k ;
for(int i = 1 ; i <= n ; i++)
{
cin >> p[i].fi.fi >> p[i].fi.se ;
p[i].se = i ;
if(p[i].fi.se != -1 && p[i].fi.se != p[i].fi.fi)
flag1 = 1 ;
}
if(k == n)
{
ld ans = 1e18 ;
vector<pair<pair<ld, ld>, int>> v_pos, v_neg ;
for(int i = 1 ; i <= n ; i++)
{
if(p[i].fi.se == -1)
v_neg.push_back(p[i]) ;
else
v_pos.push_back({{p[i].fi.se, p[i].fi.fi}, i}) ;
}
sort(v_pos.begin(), v_pos.end()) ;
sort(v_neg.begin(), v_neg.end()) ;
for(int i = -1 ; i < min(k, (int)v_pos.size()) ; i++)
{
ld sum = 0, cnt = 1 ;
for(int j = 0 ; j <= i ; j++)
{
sum += v_pos[j].fi.fi / cnt ;
cnt++ ;
}
for(int j = i + 1 ; j < v_pos.size() ; j++)
sum += v_pos[j].fi.se / cnt ;
for(int j = 0 ; j < v_neg.size() ; j++)
sum += v_neg[j].fi.fi / cnt ;
ans = min(ans, sum) ;
}
cout << fixed << setprecision(11) << ans ;
return 0 ;
}
if(n <= 7)
{
rec(0, 0, 1) ;
cout << fixed << setprecision(11) << abu ;
return 0 ;
}
if(!flag1)
{
ld ans = 1e18 ;
vector<pair<pair<ld, ld>, int>> v_pos, v_neg ;
for(int i = 1 ; i <= n ; i++)
{
if(p[i].fi.se == -1)
v_neg.push_back(p[i]) ;
else
v_pos.push_back({{p[i].fi.se, p[i].fi.fi}, i}) ;
}
sort(v_pos.begin(), v_pos.end()) ;
sort(v_neg.begin(), v_neg.end()) ;
for(int i = -1 ; i < min(k, (int)v_pos.size()) ; i++)
{
int now = i + 1, uk1 = i + 1, uk2 = 0 ;
ld sum = 0, cnt = 1 ;
for(int j = 0 ; j <= i ; j++)
{
sum += v_pos[j].fi.fi / cnt ;
cnt++ ;
}
while(now < k)
{
ld mn = 1e18 ;
if(uk1 < v_pos.size())
mn = v_pos[uk1].fi.se ;
if(uk2 < v_neg.size())
mn = min(mn, v_neg[uk2].fi.fi) ;
sum += mn / cnt ;
if(uk1 < v_pos.size() && mn == v_pos[uk1].fi.se)
uk1++ ;
else
uk2++ ;
now++ ;
}
ans = min(ans, sum) ;
}
cout << fixed << setprecision(11) << ans ;
return 0 ;
}
return 0 ;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:65:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long double, long double>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int j = i + 1 ; j < v_pos.size() ; j++)
| ~~^~~~~~~~~~~~~~
Main.cpp:67:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long double, long double>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j = 0 ; j < v_neg.size() ; j++)
| ~~^~~~~~~~~~~~~~
Main.cpp:105:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long double, long double>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | if(uk1 < v_pos.size())
| ~~~~^~~~~~~~~~~~~~
Main.cpp:107:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long double, long double>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | if(uk2 < v_neg.size())
| ~~~~^~~~~~~~~~~~~~
Main.cpp:110:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long double, long double>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | if(uk1 < v_pos.size() && mn == v_pos[uk1].fi.se)
| ~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
324 KB |
Output is correct |
6 |
Correct |
6 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
212 KB |
Output is correct |
14 |
Incorrect |
0 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
324 KB |
Output is correct |
6 |
Correct |
6 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
212 KB |
Output is correct |
14 |
Incorrect |
0 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
3 ms |
324 KB |
Output is correct |
6 |
Correct |
6 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
212 KB |
Output is correct |
14 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
3 ms |
324 KB |
Output is correct |
26 |
Correct |
6 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
2 ms |
212 KB |
Output is correct |
29 |
Correct |
2 ms |
212 KB |
Output is correct |
30 |
Correct |
2 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
212 KB |
Output is correct |
33 |
Correct |
2 ms |
212 KB |
Output is correct |
34 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |