#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Region {
int a, b, id, val = 0;
bool operator <(const Region& other) const {
return val < other.val;
}
};
const int N = 5e2 + 42, INF = 1e7 + 42;
int n, k, nb, id = 0;
long double ans = INF;
Region reg[3][N], act[N];
bool chosen[N], pos[N][2];
void solve() {
int x = 0;
for(int i = 0; i < n; i++)
if(pos[reg[0][i].id][0] || pos[reg[0][i].id][1]) {
act[x] = reg[0][i];
x++;
}
for(int i = 0; i <= k; i++) {
long double cost = 0;
if(i != 0)
for(int j = 0; j < k; j++)
act[j].val = (i+1) * act[j].b - i * act[j].a;
sort(act, act + k);
sort(act, act + i,
[](Region a, Region b) {
return a.b < b.b;
});
for(int j = 0; j < i; j++)
cost += (long double)act[j].b / (j+1);
for(int j = i; j < k; j++)
cost += (long double)act[j].a / (i+1);
ans = min(ans, cost);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
nb = n;
for(int i = 0; i < n; i++) {
reg[0][i].a = reg[0][i].b = 1000;//cin >> reg[0][i].a >> reg[0][i].b;
pos[i][1] = true;
if(reg[0][i].b == -1)
reg[0][i].b = INF;
reg[0][i].id = i;
reg[2][i] = reg[1][i] = reg[0][i];
}
sort(reg[1], reg[1] + n,
[](Region a, Region b) {
if(a.b == b.b)
return a.a < b.a;
return a.b < b.b;
});
sort(reg[2], reg[2] + n,
[](Region a, Region b) {
if(a.a == b.a)
return a.b > b.b;
return a.a > b.a;
});
for(int i = 0; i < n; i++) {
bool change = false;
pos[reg[1][i].id][0] = true;
if(!pos[reg[1][i].id][1]) {
change = true;
nb++;
}
while(id < n && nb > k) {
pos[reg[2][id].id][1] = false;
if(!pos[reg[2][id].id][0])
nb--;
id++;
}
if(nb == k)
solve();
}
cout << setprecision(10) << fixed << ans;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:76:14: warning: variable 'change' set but not used [-Wunused-but-set-variable]
76 | bool change = false;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1812 ms |
372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |